< prev index next >

test/jdk/java/lang/invoke/defineHiddenClass/BasicTest.java

Print this page

        

*** 32,43 **** */ import java.io.File; import java.io.IOException; import java.lang.invoke.MethodHandles.Lookup; ! import java.lang.invoke.MethodHandles.Lookup.ClassOption; import static java.lang.invoke.MethodHandles.lookup; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.Files; --- 32,44 ---- */ import java.io.File; import java.io.IOException; import java.lang.invoke.MethodHandles.Lookup; ! import static java.lang.invoke.MethodHandles.lookup; + import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.Files;
*** 63,76 **** --- 64,81 ---- private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "src"); private static final Path CLASSES_DIR = Paths.get("classes"); private static final Path CLASSES_10_DIR = Paths.get("classes_10"); + private static byte[] hiddenClassBytes; + @BeforeTest static void setup() throws IOException { compileSources(SRC_DIR, CLASSES_DIR); + hiddenClassBytes = Files.readAllBytes(CLASSES_DIR.resolve("HiddenClass.class")); + // compile with --release 10 with no NestHost and NestMembers attribute compileSources(SRC_DIR.resolve("Outer.java"), CLASSES_10_DIR, "--release", "10"); compileSources(SRC_DIR.resolve("EnclosingClass.java"), CLASSES_10_DIR, "--release", "10"); }
*** 160,175 **** } // Verify the nest host and nest members of a hidden class and hidden nestmate class @Test public void testHiddenNestHost() throws Throwable { ! byte[] hc1 = Files.readAllBytes(CLASSES_DIR.resolve("HiddenClass.class")); Lookup lookup1 = lookup().defineHiddenClass(hc1, false); Class<?> host = lookup1.lookupClass(); byte[] hc2 = Files.readAllBytes(CLASSES_DIR.resolve("Lambda.class")); ! Lookup lookup2 = lookup1.defineHiddenClass(hc2, false, ClassOption.NESTMATE); Class<?> member = lookup2.lookupClass(); // test nest membership and reflection API assertTrue(host.isNestmateOf(member)); assertTrue(host.getNestHost() == host); --- 165,180 ---- } // Verify the nest host and nest members of a hidden class and hidden nestmate class @Test public void testHiddenNestHost() throws Throwable { ! byte[] hc1 = hiddenClassBytes; Lookup lookup1 = lookup().defineHiddenClass(hc1, false); Class<?> host = lookup1.lookupClass(); byte[] hc2 = Files.readAllBytes(CLASSES_DIR.resolve("Lambda.class")); ! Lookup lookup2 = lookup1.defineHiddenClass(hc2, false, NESTMATE); Class<?> member = lookup2.lookupClass(); // test nest membership and reflection API assertTrue(host.isNestmateOf(member)); assertTrue(host.getNestHost() == host);
*** 223,233 **** public void defineHiddenClass(String name, boolean nestmate) throws Exception { byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve(name + ".class")); Class<?> hc; Class<?> host; if (nestmate) { ! hc = lookup().defineHiddenClass(bytes, false, ClassOption.NESTMATE).lookupClass(); host = lookup().lookupClass().getNestHost(); } else { hc = lookup().defineHiddenClass(bytes, false).lookupClass(); host = hc; } --- 228,238 ---- public void defineHiddenClass(String name, boolean nestmate) throws Exception { byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve(name + ".class")); Class<?> hc; Class<?> host; if (nestmate) { ! hc = lookup().defineHiddenClass(bytes, false, NESTMATE).lookupClass(); host = lookup().lookupClass().getNestHost(); } else { hc = lookup().defineHiddenClass(bytes, false).lookupClass(); host = hc; }
*** 264,275 **** } @Test(expectedExceptions = {IllegalAccessException.class}) public void lessPrivilegedLookup() throws Throwable { Lookup lookup = lookup().dropLookupMode(Lookup.PRIVATE); ! byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve("HiddenClass.class")); ! lookup.defineHiddenClass(bytes, false); } @DataProvider(name = "nestedTypesOrAnonymousClass") private Object[][] nestedTypesOrAnonymousClass() { return new Object[][] { --- 269,279 ---- } @Test(expectedExceptions = {IllegalAccessException.class}) public void lessPrivilegedLookup() throws Throwable { Lookup lookup = lookup().dropLookupMode(Lookup.PRIVATE); ! lookup.defineHiddenClass(hiddenClassBytes, false); } @DataProvider(name = "nestedTypesOrAnonymousClass") private Object[][] nestedTypesOrAnonymousClass() { return new Object[][] {
< prev index next >