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