• 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
16class Foo {
17    fld: int = 11
18
19    public bar(): int {
20        return this.fld
21    }
22}
23
24function fooType(): ClassType {
25    return Type.of(new Foo()) as ClassType // Type.resolve("Lpackage/Foo;") as ClassType
26}
27
28function main(): void throws {
29    const fooClass = fooType()
30    const creator = new ClassTypeCreator("MyClass").addBaseType(fooClass)
31    assertEQ( fooClass.getConstructorsNum(), 1)
32    const ctor = fooClass.getConstructor(0)
33    creator.addMethod(
34        new MethodCreator("constructor")
35            .addConstructor()
36            .addBody(new CallableBodyMethod(ctor))
37    )
38    let body: (x: Foo) => int = (x: Foo): int => { return x.fld + 30 }
39    creator.addMethod(
40        new MethodCreator("bar")
41        .addResult(IntType.VAL)
42        .addBody(new CallableBodyFunction(body as Object))
43    )
44
45    const cls = creator.create()
46
47    const f = cls.make() as Foo
48    assertEQ( f.bar(), 41)
49}
50