From 4e5430011fc9283074339b39754b0f07ff5d8a04 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Wed, 17 Mar 2021 23:38:08 +0700 Subject: Add Java 15 / AspectJ 1.9.7 test suite Also fix some minor details in Java 14 suite Signed-off-by: Alexander Kriegisch --- tests/features197/java15/HiddenClassDemo.java | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/features197/java15/HiddenClassDemo.java (limited to 'tests/features197/java15/HiddenClassDemo.java') diff --git a/tests/features197/java15/HiddenClassDemo.java b/tests/features197/java15/HiddenClassDemo.java new file mode 100644 index 000000000..0b7f0ab17 --- /dev/null +++ b/tests/features197/java15/HiddenClassDemo.java @@ -0,0 +1,39 @@ +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Constructor; + +import java.io.FileInputStream; + +import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE; + +public class HiddenClassDemo { + public static void main(String[] args) throws Throwable { + // Step 1: Create lookup object + MethodHandles.Lookup lookup = MethodHandles.lookup(); + + // Step 2: Fetch or create the class bytes we want to define + byte[] bytes = Thread.currentThread().getContextClassLoader() + .getResourceAsStream("HiddenClass.class") + .readAllBytes(); + + // Step 3: Define hidden class + Class clazz = lookup.defineHiddenClass(bytes, true, NESTMATE).lookupClass(); + // Hidden classes have class names like my.package.MyClass/0x2a23f5, but no canonical name (null) + System.out.println("Hidden class name = " + clazz.getName()); + System.out.println("Hidden class canonical name = " + clazz.getCanonicalName ()); + // Hidden classes cannot be resolved by any class loader (ClassNotFoundException) + try { + Class.forName(clazz.getName()); + } + catch (ClassNotFoundException e) { + System.out.println("Class.forName resolution error = " + e); + } + + //Step 4: Create instance of hidden class object and call interface method + Test test = (Test) clazz.getConstructor(null).newInstance(null); + test.concat("Hello", "from", "dynamically", "defined", "hidden", "class"); + } +} + +interface Test { + void concat(String... words); +} -- cgit v1.2.3