1 /* 2 * Copyright (c) 2019, 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 * @modules java.base/jdk.internal.misc 27 * @library /test/lib 28 * @build jdk.test.lib.JDKToolLauncher 29 * jdk.test.lib.process.ProcessTools 30 * jdk.test.lib.Utils 31 * @run main/othervm -Xverify:remote DefineHiddenClass 32 */ 33 34 import java.io.ByteArrayOutputStream; 35 import java.io.File; 36 import java.io.FileInputStream; 37 import java.lang.invoke.MethodHandles; 38 import java.lang.invoke.MethodHandles.Lookup; 39 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import jdk.internal.misc.Unsafe; 43 44 import jdk.test.lib.JDKToolLauncher; 45 import jdk.test.lib.process.ProcessTools; 46 import jdk.test.lib.Utils; 47 48 /* package-private */ interface Test { 49 void test(); 50 } 51 52 53 public class DefineHiddenClass { 54 55 static final Class<?> klass = DefineHiddenClass.class; 56 static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "hidden"); 57 static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "hidden"); 58 59 static void compileSources(String sourceFile) throws Throwable { 60 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("javac"); 61 launcher.addToolArg("-cp") 62 .addToolArg(Utils.TEST_CLASSES.toString()) 63 .addToolArg("-d") 64 .addToolArg(CLASSES_DIR.toString()) 65 .addToolArg(Paths.get(SRC_DIR.toString(), sourceFile).toString()); 66 67 int exitCode = ProcessTools.executeCommand(launcher.getCommand()) 68 .getExitValue(); 69 if (exitCode != 0) { 70 throw new RuntimeException("Compilation of the test failed. " 71 + "Unexpected exit code: " + exitCode); 72 } 73 } 74 75 static byte[] readClassFile(String classFileName) throws Exception { 76 File classFile = new File(CLASSES_DIR + File.separator + classFileName); 77 try (FileInputStream in = new FileInputStream(classFile); 78 ByteArrayOutputStream out = new ByteArrayOutputStream()) 79 { 80 int b; 81 while ((b = in.read()) != -1) { 82 out.write(b); 83 } 84 return out.toByteArray(); 85 } 86 } 87 88 public static void main(String[] args) throws Throwable { 89 compileSources("NameInString.java"); 90 Lookup lookup = MethodHandles.lookup(); 91 byte[] bytes = readClassFile("NameInString.class"); 92 Class<?> c = lookup.defineHiddenClass(bytes, true, NESTMATE).lookupClass(); 93 Test t = (Test) c.newInstance(); 94 t.test(); 95 } 96 97 }