1 /* 2 * Copyright (c) 2020, 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 import java.io.ByteArrayOutputStream; 25 import java.io.File; 26 import java.io.FileInputStream; 27 28 import java.lang.invoke.MethodHandles; 29 import java.lang.invoke.MethodHandles.Lookup; 30 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 34 import jdk.test.lib.compiler.CompilerUtils; 35 import jdk.test.lib.Utils; 36 37 38 interface Test { 39 void test(); 40 } 41 42 public class HiddenClassApp { 43 static void log(String str) { System.out.println(str); } 44 45 static final String HCName = "HiddenClass.java"; 46 static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "hidden"); 47 static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "hidden"); 48 49 static void compileSources(String sourceFile) throws Throwable { 50 boolean ok = CompilerUtils.compile(SRC_DIR.resolve(sourceFile), CLASSES_DIR, 51 "-cp", Utils.TEST_CLASSES.toString()); 52 if (!ok){ 53 throw new RuntimeException("HiddenClassApp: Compilation of the test failed. "); 54 } 55 } 56 57 static byte[] readClassFile(String classFileName) throws Exception { 58 File classFile = new File(CLASSES_DIR + File.separator + classFileName); 59 try (FileInputStream in = new FileInputStream(classFile); 60 ByteArrayOutputStream out = new ByteArrayOutputStream()) 61 { 62 int b; 63 while ((b = in.read()) != -1) { 64 out.write(b); 65 } 66 return out.toByteArray(); 67 } 68 } 69 70 static Class<?> defineHiddenClass(String name) throws Exception { 71 Lookup lookup = MethodHandles.lookup(); 72 byte[] bytes = readClassFile(name + ".class"); 73 Class<?> hc = lookup.defineHiddenClass(bytes, false).lookupClass(); 74 return hc; 75 } 76 77 public static void main(String args[]) throws Exception { 78 log("HiddenClassApp: started"); 79 try { 80 compileSources(HCName); 81 log("HiddenClassApp: compiled " + HCName); 82 } catch (Throwable t) { 83 t.printStackTrace(); 84 throw new Exception("HiddenClassApp: Failed to compile " + HCName); 85 } 86 87 Class<?> c = defineHiddenClass("HiddenClass"); 88 log("HiddenClassApp: Defined HiddenClass with name: " + c.getName()); 89 HiddenClassAgent.setHiddenClassLoaded(); 90 91 Test t = (Test) c.newInstance(); 92 t.test(); 93 log("HiddenClassApp: Tested HiddenClass"); 94 95 if (!HiddenClassAgent.checkWaitForCompleteness()) { 96 throw new Exception("HiddenClassApp: FAIL: HiddenClassAgent did not complete"); 97 } 98 if (HiddenClassAgent.failed()) { 99 throw new Exception("HiddenClassApp: FAIL: HiddenClassAgent failed"); 100 } 101 log("HiddenClassApp: finished"); 102 } 103 }