Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/DefineHiddenClass.java
40942 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @modules java.base/jdk.internal.misc26* @library /test/lib27* @build jdk.test.lib.JDKToolLauncher28* jdk.test.lib.process.ProcessTools29* jdk.test.lib.Utils30* @run main/othervm -Xverify:remote DefineHiddenClass31*/3233import java.io.ByteArrayOutputStream;34import java.io.File;35import java.io.FileInputStream;36import java.lang.invoke.MethodHandles;37import java.lang.invoke.MethodHandles.Lookup;38import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;39import java.nio.file.Path;40import java.nio.file.Paths;41import jdk.internal.misc.Unsafe;4243import jdk.test.lib.JDKToolLauncher;44import jdk.test.lib.process.ProcessTools;45import jdk.test.lib.Utils;4647/* package-private */ interface Test {48void test();49}505152public class DefineHiddenClass {5354static final Class<?> klass = DefineHiddenClass.class;55static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "hidden");56static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "hidden");5758static void compileSources(String sourceFile) throws Throwable {59JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("javac");60launcher.addToolArg("-cp")61.addToolArg(Utils.TEST_CLASSES.toString())62.addToolArg("-d")63.addToolArg(CLASSES_DIR.toString())64.addToolArg(Paths.get(SRC_DIR.toString(), sourceFile).toString());6566int exitCode = ProcessTools.executeCommand(launcher.getCommand())67.getExitValue();68if (exitCode != 0) {69throw new RuntimeException("Compilation of the test failed. "70+ "Unexpected exit code: " + exitCode);71}72}7374static byte[] readClassFile(String classFileName) throws Exception {75File classFile = new File(CLASSES_DIR + File.separator + classFileName);76try (FileInputStream in = new FileInputStream(classFile);77ByteArrayOutputStream out = new ByteArrayOutputStream())78{79int b;80while ((b = in.read()) != -1) {81out.write(b);82}83return out.toByteArray();84}85}8687public static void main(String[] args) throws Throwable {88compileSources("NameInString.java");89Lookup lookup = MethodHandles.lookup();90byte[] bytes = readClassFile("NameInString.class");91Class<?> c = lookup.defineHiddenClass(bytes, true, NESTMATE).lookupClass();92Test t = (Test) c.newInstance();93t.test();94}9596}979899