< prev index next >

test/jdk/java/lang/invoke/defineHiddenClass/StaticInvocableTest.java

Print this page

  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 /*
 25  * @test
 26  * @bug 8266925
 27  * @summary hidden class members can't be statically invocable

 28  * @modules java.base/jdk.internal.misc
 29  * @build java.base/*
 30  * @run testng StaticInvocableTest
 31  */
 32 
 33 import java.lang.classfile.ClassFile;
 34 import java.lang.constant.ClassDesc;
 35 import java.lang.constant.MethodTypeDesc;
 36 import java.lang.invoke.MethodHandle;
 37 import java.lang.invoke.MethodHandles.Lookup;
 38 import java.lang.invoke.MethodType;
 39 import java.lang.invoke.LookupHelper;
 40 import java.lang.reflect.AccessFlag;
 41 import org.testng.annotations.Test;
 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.CD_Object;
 46 import static java.lang.constant.ConstantDescs.CD_int;
 47 import static java.lang.constant.ConstantDescs.INIT_NAME;

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

  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 /*
 25  * @test
 26  * @bug 8266925
 27  * @summary hidden class members can't be statically invocable
 28  * @enablePreview
 29  * @modules java.base/jdk.internal.misc
 30  * @build java.base/*
 31  * @run testng StaticInvocableTest
 32  */
 33 
 34 import java.lang.classfile.ClassFile;
 35 import java.lang.constant.ClassDesc;
 36 import java.lang.constant.MethodTypeDesc;
 37 import java.lang.invoke.MethodHandle;
 38 import java.lang.invoke.MethodHandles.Lookup;
 39 import java.lang.invoke.MethodType;
 40 import java.lang.invoke.LookupHelper;
 41 import java.lang.reflect.AccessFlag;
 42 import org.testng.annotations.Test;
 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.CD_Object;
 47 import static java.lang.constant.ConstantDescs.CD_int;
 48 import static java.lang.constant.ConstantDescs.INIT_NAME;

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