Path: blob/master/test/jdk/java/lang/instrument/HiddenClass/HiddenClassApp.java
41153 views
/*1* Copyright (c) 2020, 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*/2223import java.io.ByteArrayOutputStream;24import java.io.File;25import java.io.FileInputStream;2627import java.lang.invoke.MethodHandles;28import java.lang.invoke.MethodHandles.Lookup;2930import java.nio.file.Path;31import java.nio.file.Paths;3233import jdk.test.lib.compiler.CompilerUtils;34import jdk.test.lib.Utils;353637interface Test {38void test();39}4041public class HiddenClassApp {42static void log(String str) { System.out.println(str); }4344static final String HCName = "HiddenClass.java";45static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "hidden");46static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "hidden");4748static void compileSources(String sourceFile) throws Throwable {49boolean ok = CompilerUtils.compile(SRC_DIR.resolve(sourceFile), CLASSES_DIR,50"-cp", Utils.TEST_CLASSES.toString());51if (!ok){52throw new RuntimeException("HiddenClassApp: Compilation of the test failed. ");53}54}5556static byte[] readClassFile(String classFileName) throws Exception {57File classFile = new File(CLASSES_DIR + File.separator + classFileName);58try (FileInputStream in = new FileInputStream(classFile);59ByteArrayOutputStream out = new ByteArrayOutputStream())60{61int b;62while ((b = in.read()) != -1) {63out.write(b);64}65return out.toByteArray();66}67}6869static Class<?> defineHiddenClass(String name) throws Exception {70Lookup lookup = MethodHandles.lookup();71byte[] bytes = readClassFile(name + ".class");72Class<?> hc = lookup.defineHiddenClass(bytes, false).lookupClass();73return hc;74}7576public static void main(String args[]) throws Exception {77log("HiddenClassApp: started");78try {79compileSources(HCName);80log("HiddenClassApp: compiled " + HCName);81} catch (Throwable t) {82t.printStackTrace();83throw new Exception("HiddenClassApp: Failed to compile " + HCName);84}8586Class<?> c = defineHiddenClass("HiddenClass");87log("HiddenClassApp: Defined HiddenClass with name: " + c.getName());88HiddenClassAgent.setHiddenClassLoaded();8990Test t = (Test) c.newInstance();91t.test();92log("HiddenClassApp: Tested HiddenClass");9394if (!HiddenClassAgent.checkWaitForCompleteness()) {95throw new Exception("HiddenClassApp: FAIL: HiddenClassAgent did not complete");96}97if (HiddenClassAgent.failed()) {98throw new Exception("HiddenClassApp: FAIL: HiddenClassAgent failed");99}100log("HiddenClassApp: finished");101}102}103104105