< prev index next >

test/jdk/java/lang/invoke/lookup/SpecialStatic.java

Print this page

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /* @test
 25  * @bug 8032400
 26  * @summary JSR292: invokeSpecial: InternalError attempting to lookup a method

 27  * @compile -XDignore.symbol.file SpecialStatic.java
 28  * @run testng test.java.lang.invoke.lookup.SpecialStatic
 29  */
 30 package test.java.lang.invoke.lookup;
 31 
 32 import java.lang.classfile.ClassFile;
 33 import java.lang.constant.ClassDesc;
 34 import java.lang.constant.MethodHandleDesc;
 35 import java.lang.constant.MethodTypeDesc;
 36 import java.lang.invoke.MethodHandle;
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.invoke.MethodType;
 39 import java.lang.reflect.AccessFlag;
 40 
 41 import org.testng.annotations.*;
 42 
 43 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 44 import static java.lang.classfile.ClassFile.ACC_STATIC;
 45 import static java.lang.constant.ConstantDescs.*;
 46 import static java.lang.constant.DirectMethodHandleDesc.Kind.SPECIAL;

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

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /* @test
 25  * @bug 8032400
 26  * @summary JSR292: invokeSpecial: InternalError attempting to lookup a method
 27  * @enablePreview
 28  * @compile -XDignore.symbol.file SpecialStatic.java
 29  * @run testng test.java.lang.invoke.lookup.SpecialStatic
 30  */
 31 package test.java.lang.invoke.lookup;
 32 
 33 import java.lang.classfile.ClassFile;
 34 import java.lang.constant.ClassDesc;
 35 import java.lang.constant.MethodHandleDesc;
 36 import java.lang.constant.MethodTypeDesc;
 37 import java.lang.invoke.MethodHandle;
 38 import java.lang.invoke.MethodHandles;
 39 import java.lang.invoke.MethodType;
 40 import java.lang.reflect.AccessFlag;
 41 
 42 import org.testng.annotations.*;
 43 
 44 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 45 import static java.lang.classfile.ClassFile.ACC_STATIC;
 46 import static java.lang.constant.ConstantDescs.*;
 47 import static java.lang.constant.DirectMethodHandleDesc.Kind.SPECIAL;

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         });
< prev index next >