forked from quarkusio/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstanceExecutableCreatorTest.java
56 lines (49 loc) · 2.12 KB
/
InstanceExecutableCreatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package io.quarkus.gizmo2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.constant.ClassDesc;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import io.github.dmlloyd.classfile.Signature.ClassTypeSig;
import io.github.dmlloyd.classfile.Signature.TypeArg;
import io.github.dmlloyd.classfile.extras.reflect.AccessFlag;
import io.quarkus.gizmo2.desc.ConstructorDesc;
import io.quarkus.gizmo2.desc.MethodDesc;
public class InstanceExecutableCreatorTest {
@SuppressWarnings("unchecked")
@Test
public void testThis() {
TestClassMaker tcm = new TestClassMaker();
Gizmo g = Gizmo.create(tcm);
g.class_(ClassDesc.of("io.quarkus.gizmo2.TestFun"), cc -> {
cc.implements_(ClassTypeSig.of(Function.class.getName(), TypeArg.of(ClassTypeSig.of(String.class.getName()))));
cc.withFlag(AccessFlag.PROTECTED);
cc.constructor(con -> {
con.withFlag(AccessFlag.PUBLIC);
con.body(bc -> {
bc.invokeSpecial(ConstructorDesc.of(Object.class), con.this_());
bc.return_();
});
});
MethodDesc convert = cc.method("convert", mc -> {
ParamVar p = mc.parameter("value", String.class);
mc.returning(String.class);
mc.body(bc -> {
// return val.toLowerCase();
Expr ret = bc.invokeVirtual(MethodDesc.of(String.class, "toLowerCase", String.class), p);
bc.return_(ret);
});
});
cc.method("apply", mc -> {
ParamVar p = mc.parameter("t", Object.class);
mc.returning(Object.class);
mc.withFlag(AccessFlag.PUBLIC);
mc.body(bc -> {
// return convert((String)t);
Expr strVal = bc.cast(p, String.class);
bc.return_(bc.invokeVirtual(convert, mc.this_(), strVal));
});
});
});
assertEquals("foo", tcm.noArgsConstructor(Function.class).apply("Foo"));
}
}