1/* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { LineColPosition } from '../base/Position'; 17import { Stmt } from '../base/Stmt'; 18import { ArkClass, ClassCategory } from './ArkClass'; 19import { FieldSignature } from './ArkSignature'; 20import { Type } from '../base/Type'; 21import { ArkBaseModel, ModifierType } from './ArkBaseModel'; 22import { ArkError } from '../common/ArkError'; 23import { Language } from './ArkFile'; 24 25export enum FieldCategory { 26 PROPERTY_DECLARATION = 0, 27 PROPERTY_ASSIGNMENT = 1, 28 SHORT_HAND_PROPERTY_ASSIGNMENT = 2, 29 SPREAD_ASSIGNMENT = 3, 30 PROPERTY_SIGNATURE = 4, 31 ENUM_MEMBER = 5, 32 INDEX_SIGNATURE = 6, 33 GET_ACCESSOR = 7, 34 PARAMETER_PROPERTY = 8, 35} 36 37/** 38 * @category core/model 39 */ 40export class ArkField extends ArkBaseModel { 41 private code: string = ''; 42 private category!: FieldCategory; 43 44 private declaringClass!: ArkClass; 45 private questionToken: boolean = false; 46 private exclamationToken: boolean = false; 47 48 private fieldSignature!: FieldSignature; 49 private originPosition?: LineColPosition; 50 51 private initializer: Stmt[] = []; 52 53 constructor() { 54 super(); 55 } 56 57 /** 58 * Returns the program language of the file where this field's class defined. 59 */ 60 public getLanguage(): Language { 61 return this.getDeclaringArkClass().getLanguage(); 62 } 63 64 public getDeclaringArkClass(): ArkClass { 65 return this.declaringClass; 66 } 67 68 public setDeclaringArkClass(declaringClass: ArkClass): void { 69 this.declaringClass = declaringClass; 70 } 71 72 /** 73 * Returns the codes of field as a **string.** 74 * @returns the codes of field. 75 */ 76 public getCode(): string { 77 return this.code; 78 } 79 80 public setCode(code: string): void { 81 this.code = code; 82 } 83 84 public getCategory(): FieldCategory { 85 return this.category; 86 } 87 88 public setCategory(category: FieldCategory): void { 89 this.category = category; 90 } 91 92 public getName(): string { 93 return this.fieldSignature.getFieldName(); 94 } 95 96 public getType(): Type { 97 return this.fieldSignature.getType(); 98 } 99 100 public getSignature(): FieldSignature { 101 return this.fieldSignature; 102 } 103 104 public setSignature(fieldSig: FieldSignature): void { 105 this.fieldSignature = fieldSig; 106 } 107 108 /** 109 * Returns an array of statements used for initialization. 110 * @returns An array of statements used for initialization. 111 */ 112 public getInitializer(): Stmt[] { 113 return this.initializer; 114 } 115 116 public setInitializer(initializer: Stmt[]): void { 117 this.initializer = initializer; 118 } 119 120 public setQuestionToken(questionToken: boolean): void { 121 this.questionToken = questionToken; 122 } 123 124 public setExclamationToken(exclamationToken: boolean): void { 125 this.exclamationToken = exclamationToken; 126 } 127 128 public getQuestionToken(): boolean { 129 return this.questionToken; 130 } 131 132 public getExclamationToken(): boolean { 133 return this.exclamationToken; 134 } 135 136 public setOriginPosition(position: LineColPosition): void { 137 this.originPosition = position; 138 } 139 140 /** 141 * Returns the original position of the field at source code. 142 * @returns The original position of the field at source code. 143 */ 144 public getOriginPosition(): LineColPosition { 145 return this.originPosition ?? LineColPosition.DEFAULT; 146 } 147 148 public validate(): ArkError { 149 return this.validateFields(['category', 'declaringClass', 'fieldSignature']); 150 } 151 152 // For class field, it is default public if there is not any access modify 153 public isPublic(): boolean { 154 if ( 155 !this.containsModifier(ModifierType.PUBLIC) && 156 !this.containsModifier(ModifierType.PRIVATE) && 157 !this.containsModifier(ModifierType.PROTECTED) && 158 this.getDeclaringArkClass().getCategory() === ClassCategory.CLASS 159 ) { 160 return true; 161 } 162 return this.containsModifier(ModifierType.PUBLIC); 163 } 164} 165