100
101 // Wrap target into LF (convert) to get "target" referenced from LF
102 MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class));
103
104 // Invoke enough times to provoke LF compilation to bytecode.
105 for (int i = 0; i<100; i++) {
106 Object r = wrappedMH.invokeExact((Integer)1);
107 }
108 }
109
110 /*
111 * Constructs bytecode for the following class:
112 * public class pkg.MyClass {
113 * MyClass() {}
114 * public Object get(int i) { return null; }
115 * }
116 */
117 public static byte[] dumpClass(String pkg) {
118 return ClassFile.of().build(ClassDesc.of(pkg.replace('/', '.'), "MyClass"), clb -> {
119 clb.withSuperclass(CD_Object);
120 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
121 clb.withMethodBody(INIT_NAME, MTD_void, 0, cob -> {
122 cob.aload(0);
123 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
124 cob.return_();
125 });
126 clb.withMethodBody("get", MethodTypeDesc.of(CD_Object, CD_int),
127 ACC_PUBLIC | ACC_STATIC, cob -> {
128 cob.aconst_null();
129 cob.areturn();
130 });
131 });
132 }
133 }
|
100
101 // Wrap target into LF (convert) to get "target" referenced from LF
102 MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class));
103
104 // Invoke enough times to provoke LF compilation to bytecode.
105 for (int i = 0; i<100; i++) {
106 Object r = wrappedMH.invokeExact((Integer)1);
107 }
108 }
109
110 /*
111 * Constructs bytecode for the following class:
112 * public class pkg.MyClass {
113 * MyClass() {}
114 * public Object get(int i) { return null; }
115 * }
116 */
117 public static byte[] dumpClass(String pkg) {
118 return ClassFile.of().build(ClassDesc.of(pkg.replace('/', '.'), "MyClass"), clb -> {
119 clb.withSuperclass(CD_Object);
120 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
121 clb.withMethodBody(INIT_NAME, MTD_void, 0, cob -> {
122 cob.aload(0);
123 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
124 cob.return_();
125 });
126 clb.withMethodBody("get", MethodTypeDesc.of(CD_Object, CD_int),
127 ACC_PUBLIC | ACC_STATIC, cob -> {
128 cob.aconst_null();
129 cob.areturn();
130 });
131 });
132 }
133 }
|