• 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
18native function TypeAPIMethodInvoke(methodDesc: string, recv: NullishType, args: NullishType[]): NullishType
19native function TypeAPIMethodInvokeConstructor(methodDesc: string, args: NullishType[]): Object
20
21/**
22 * Represents method of class or interface
23 */
24export final class Method extends Object {
25    private td: TypeDesc = new TypeDesc()
26    private name: string = ""
27    private attributes: int
28    private accessMod: byte
29
30    private constructor () {}
31
32    /**
33     * Returns function type of this method
34     *
35     * @returns method's {@link MethodType}
36     */
37    public getType(): MethodType {
38        return Type.resolve(this.td)! as MethodType
39    }
40
41    public getName(): string {
42        return this.name
43    }
44
45    internal getTD(): TypeDesc {
46        return this.td
47    }
48
49    public isInherited(): boolean {
50        return (this.attributes & Attributes.INHERITED) != 0
51    }
52
53    public isStatic(): boolean {
54        return (this.attributes & Attributes.STATIC) != 0
55    }
56
57    public isFinal(): boolean {
58        // TODO(shumilov-petr): not implemented in front-end
59        return (this.attributes & Attributes.FINAL) != 0
60    }
61
62    public isAbstract(): boolean {
63        return (this.attributes & Attributes.ABSTRACT) != 0
64    }
65
66    public isConstructor(): boolean {
67        return (this.attributes & Attributes.CONSTRUCTOR) != 0
68    }
69
70    public isGetter(): boolean {
71        return (this.attributes & Attributes.GETTER) != 0
72    }
73
74    public isSetter(): boolean {
75        return (this.attributes & Attributes.SETTER) != 0
76    }
77
78    public override toString(): string {
79        return this.getName() + ": " + this.getType().toString()
80    }
81
82    public invoke(recv: NullishType, args: NullishType[]): NullishType {
83        const isCtor = this.isConstructor()
84        const isStatic = this.isStatic()
85        if ((isStatic || isCtor) != (recv == null)) {
86            throw new Error("reciever may be null only for static/ctor methods")
87        }
88        const thisType = this.getType()
89        if (thisType.getParametersNum() != args.length) {
90            throw new Error("arguments length mismatch " + thisType.getParametersNum() + " != " + args.length)
91        }
92        let convertedArgs: NullishType[] = new NullishType[args.length]
93        for (let i = 0; i < convertedArgs.length; i++) {
94            convertedArgs[i] = thisType.getParameter(i).getType().convertObject(args[i])
95        }
96        if (isCtor) {
97            return TypeAPIMethodInvokeConstructor(this.td, convertedArgs)
98        } else {
99            if (!isStatic) {
100                recv = thisType.getReceiverType().convertObject(recv)
101            }
102            return TypeAPIMethodInvoke(this.td, recv, convertedArgs)
103        }
104    }
105
106    public getAttributes(): int {
107        return this.attributes
108    }
109
110    public getAccessModifier(): int {
111        return this.accessMod
112    }
113
114    public equals(oth: NullishType): boolean {
115        return oth instanceof Method &&
116                this.td == (oth as Method).td &&
117                this.name == (oth as Method).name &&
118                this.accessMod == (oth as Method).accessMod &&
119                this.attributes == (oth as Method).attributes
120    }
121}
122