1 /* 2 * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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 /* 25 * @test 26 * @bug 8266925 27 * @summary hidden class members can't be statically invocable 28 * @modules java.base/jdk.internal.misc 29 * @enablePreview 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; 49 import static java.lang.constant.ConstantDescs.MTD_void; 50 51 public class StaticInvocableTest { 52 public static void main(String[] args) throws Throwable { 53 StaticInvocableTest test = new StaticInvocableTest(); 54 test.testJavaLang(); 55 test.testJavaUtil(); 56 test.testJdkInternalMisc(); 57 test.testJavaLangInvoke(); 58 test.testProhibitedJavaPkg(); 59 System.out.println("TEST PASSED"); 60 } 61 62 // Test hidden classes from different packages 63 // (see j.l.i.InvokerBytecodeGenerator::isStaticallyInvocable). 64 @Test public void testJavaLang() throws Throwable { test("java/lang"); } 65 @Test public void testJavaUtil() throws Throwable { test("java/util"); } 66 @Test public void testJdkInternalMisc() throws Throwable { test("jdk/internal/misc"); } 67 @Test public void testJavaLangInvoke() throws Throwable { test("java/lang/invoke"); } 68 @Test public void testProhibitedJavaPkg() throws Throwable { 69 try { 70 test("java/prohibited"); 71 } catch (IllegalArgumentException e) { 72 return; 73 } 74 throw new RuntimeException("Expected SecurityException"); 75 } 76 77 private static void test(String pkg) throws Throwable { 78 byte[] bytes = dumpClass(pkg); 79 Lookup lookup; 80 if (pkg.equals("java/prohibited")) { 81 StaticInvocableTest sampleclass = new StaticInvocableTest(); 82 lookup = LookupHelper.newLookup(sampleclass.getClass()); 83 } else if (pkg.equals("java/lang")) { 84 lookup = LookupHelper.newLookup(Object.class); 85 } else if (pkg.equals("java/util")) { 86 lookup = LookupHelper.newLookup(java.util.ArrayList.class); 87 } else if (pkg.equals("jdk/internal/misc")) { 88 lookup = LookupHelper.newLookup(jdk.internal.misc.Signal.class); 89 } else if (pkg.equals("java/lang/invoke")) { 90 lookup = LookupHelper.newLookup(java.lang.invoke.CallSite.class); 91 } else { 92 throw new RuntimeException("Unexpected pkg: " + pkg); 93 } 94 95 // Define hidden class 96 Lookup l = lookup.defineHiddenClass(bytes, true); 97 98 MethodType t = MethodType.methodType(Object.class, int.class); 99 MethodHandle target = l.findStatic(l.lookupClass(), "get", t); 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 }