103 }
104
105 @Test
106 public void testConstant() throws Throwable {
107 MethodHandle mh = (MethodHandle)t3.getDeclaredMethod("getMethodHandle").invoke(null);
108 int result = (int)mh.invoke(t3.newInstance());
109 assertEquals(result, 1); // T1.m should be invoked.
110 }
111
112 @Test
113 public void testFindSpecial() throws Throwable {
114 MethodHandles.Lookup lookup = (MethodHandles.Lookup)t3.getDeclaredMethod("getLookup").invoke(null);
115 MethodHandle mh = lookup.findSpecial(t1, "m", MethodType.methodType(int.class), t3);
116 int result = (int)mh.invoke(t3.newInstance());
117 assertEquals(result, 1); // T1.m should be invoked.
118 }
119
120 public static byte[] dumpT1() {
121 return ClassFile.of().build(CD_T1, clb -> {
122 clb.withSuperclass(CD_Object);
123 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
124 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
125 cob.aload(0);
126 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
127 cob.return_();
128 });
129 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC, cob -> {
130 cob.bipush(1);
131 cob.ireturn();
132 });
133 });
134 }
135
136 public static byte[] dumpT2() {
137 return ClassFile.of().build(CD_T2, clb -> {
138 clb.withSuperclass(CD_T1);
139 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
140 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
141 cob.aload(0);
142 cob.invokespecial(CD_T1, INIT_NAME, MTD_void);
143 cob.return_();
144 });
145 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC | ACC_STATIC, cob -> {
146 cob.bipush(2);
147 cob.ireturn();
148 });
149 });
150 }
151
152 public static byte[] dumpT3() {
153 return ClassFile.of().build(CD_T3, clb -> {
154 clb.withSuperclass(CD_T2);
155 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
156 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
157 cob.aload(0);
158 cob.invokespecial(CD_T2, INIT_NAME, MTD_void);
159 cob.return_();
160 });
161 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC, cob -> {
162 cob.bipush(3);
163 cob.ireturn();
164 });
165 clb.withMethodBody("getMethodHandle", MethodTypeDesc.of(CD_MethodHandle),
166 ACC_PUBLIC | ACC_STATIC, cob -> {
167 cob.loadConstant(MethodHandleDesc.ofMethod(SPECIAL, CD_T1, METHOD_NAME, MTD_int));
168 cob.areturn();
169 });
170 clb.withMethodBody("getLookup", MTD_Lookup,
171 ACC_PUBLIC | ACC_STATIC, cob -> {
172 cob.invokestatic(CD_MethodHandles, "lookup", MTD_Lookup);
173 cob.areturn();
174 });
175 });
|
103 }
104
105 @Test
106 public void testConstant() throws Throwable {
107 MethodHandle mh = (MethodHandle)t3.getDeclaredMethod("getMethodHandle").invoke(null);
108 int result = (int)mh.invoke(t3.newInstance());
109 assertEquals(result, 1); // T1.m should be invoked.
110 }
111
112 @Test
113 public void testFindSpecial() throws Throwable {
114 MethodHandles.Lookup lookup = (MethodHandles.Lookup)t3.getDeclaredMethod("getLookup").invoke(null);
115 MethodHandle mh = lookup.findSpecial(t1, "m", MethodType.methodType(int.class), t3);
116 int result = (int)mh.invoke(t3.newInstance());
117 assertEquals(result, 1); // T1.m should be invoked.
118 }
119
120 public static byte[] dumpT1() {
121 return ClassFile.of().build(CD_T1, clb -> {
122 clb.withSuperclass(CD_Object);
123 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
124 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
125 cob.aload(0);
126 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
127 cob.return_();
128 });
129 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC, cob -> {
130 cob.bipush(1);
131 cob.ireturn();
132 });
133 });
134 }
135
136 public static byte[] dumpT2() {
137 return ClassFile.of().build(CD_T2, clb -> {
138 clb.withSuperclass(CD_T1);
139 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
140 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
141 cob.aload(0);
142 cob.invokespecial(CD_T1, INIT_NAME, MTD_void);
143 cob.return_();
144 });
145 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC | ACC_STATIC, cob -> {
146 cob.bipush(2);
147 cob.ireturn();
148 });
149 });
150 }
151
152 public static byte[] dumpT3() {
153 return ClassFile.of().build(CD_T3, clb -> {
154 clb.withSuperclass(CD_T2);
155 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
156 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
157 cob.aload(0);
158 cob.invokespecial(CD_T2, INIT_NAME, MTD_void);
159 cob.return_();
160 });
161 clb.withMethodBody(METHOD_NAME, MTD_int, ACC_PUBLIC, cob -> {
162 cob.bipush(3);
163 cob.ireturn();
164 });
165 clb.withMethodBody("getMethodHandle", MethodTypeDesc.of(CD_MethodHandle),
166 ACC_PUBLIC | ACC_STATIC, cob -> {
167 cob.loadConstant(MethodHandleDesc.ofMethod(SPECIAL, CD_T1, METHOD_NAME, MTD_int));
168 cob.areturn();
169 });
170 clb.withMethodBody("getLookup", MTD_Lookup,
171 ACC_PUBLIC | ACC_STATIC, cob -> {
172 cob.invokestatic(CD_MethodHandles, "lookup", MTD_Lookup);
173 cob.areturn();
174 });
175 });
|