diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-03-17 23:38:08 +0700 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-03-17 23:38:08 +0700 |
commit | 4e5430011fc9283074339b39754b0f07ff5d8a04 (patch) | |
tree | c56b73057f7255faee0b5a04532155c281db65e0 /tests/features197/java15/HiddenClassDemo.java | |
parent | 58830078585fd045c92737ce56cb693b903f5545 (diff) | |
download | aspectj-4e5430011fc9283074339b39754b0f07ff5d8a04.tar.gz aspectj-4e5430011fc9283074339b39754b0f07ff5d8a04.zip |
Add Java 15 / AspectJ 1.9.7 test suite
Also fix some minor details in Java 14 suite
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Diffstat (limited to 'tests/features197/java15/HiddenClassDemo.java')
-rw-r--r-- | tests/features197/java15/HiddenClassDemo.java | 39 |
1 files changed, 39 insertions, 0 deletions
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); +} |