• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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
16package std.core;
17
18native function TypeAPIGetStaticFieldValue(ownerType: Type, name: string): NullishType
19native function TypeAPISetStaticFieldValue(ownerType: Type, name: string, val: NullishType): void
20
21/**
22 * Represents field of class or interface
23 *
24 */
25export final class Field extends Object {
26    private fieldType: Type | undefined // not undefined if already created
27    private ownerType: Type | undefined // not undefined if already created
28    private name: string = ""
29    private attributes: int
30    private accessMod: byte
31
32    private constructor () {}
33
34    /**
35     * Returns type of this field
36     *
37     * @returns {@link Type} of this field
38     */
39    public getType(): Type {
40        return this.fieldType!
41    }
42
43    public getOwnerType(): Type {
44        return this.ownerType!
45    }
46
47    public getName(): String {
48        return this.name
49    }
50
51    public getAttributes(): int {
52        return this.attributes
53    }
54
55    public getAccessModifier(): int {
56        return this.accessMod
57    }
58
59    public getStaticValue(): NullishType {
60        if (!this.isStatic()) {
61            throw new AssertionError("Field value is not static!")
62        }
63        return TypeAPIGetStaticFieldValue(this.ownerType!, this.name)
64    }
65
66    public setStaticValue(v: NullishType): void {
67        if (!this.isStatic()) {
68            throw new AssertionError("Field value is not static!")
69        }
70        if (!this.getType().assignableFrom(Type.of(v))) {
71            throw new AssertionError("Cannot set value " + v + " to " + this.getType())
72        }
73        TypeAPISetStaticFieldValue(this.ownerType!, this.name, this.getType().convertObject(v))
74    }
75
76    public isInherited(): boolean {
77        return (this.attributes & Attributes.INHERITED) != 0
78    }
79
80    public isStatic(): boolean {
81        return (this.attributes & Attributes.STATIC) != 0
82    }
83
84    public isReadonly(): boolean {
85        // TODO(shumilov-petr): allways false due to extra type info dumping required
86        return (this.attributes & Attributes.READONLY) != 0
87    }
88
89    public override toString(): string {
90        return this.getName() + ": " + this.getType().toString()
91    }
92
93    public equals(oth: NullishType): boolean {
94        return oth instanceof Field &&
95                this.fieldType!.equals((oth as Field).fieldType!) &&
96                this.name == (oth as Field).name &&
97                this.accessMod == (oth as Field).accessMod &&
98                this.attributes == (oth as Field).attributes
99    }
100}
101