< prev index next >
test/java/lang/invoke/MethodHandles/privateLookupIn/test/p/PrivateLookupInTests.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -124,10 +124,30 @@
// get obj field
MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
Object obj = mh.invokeExact();
}
+ // test target class in unnamed module
+ public void testTargetClassInUnnamedModule() throws Throwable {
+ Class<?> clazz = Class.forName("Unnamed");
+ assertFalse(clazz.getModule().isNamed());
+
+ // thisModule does not read the unnamed module
+ Module thisModule = getClass().getModule();
+ assertFalse(thisModule.canRead(clazz.getModule()));
+ try {
+ MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
+ assertTrue(false);
+ } catch (IllegalAccessException expected) { }
+
+ // thisModule reads the unnamed module
+ thisModule.addReads(clazz.getModule());
+ Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
+ assertTrue(lookup.lookupClass() == clazz);
+ assertTrue(lookup.hasPrivateAccess());
+ }
+
// test does not read m2, m2 opens p2 to test
@Test(expectedExceptions = {IllegalAccessException.class})
public void testCallerDoesNotRead() throws Throwable {
// m2/p2.Type
Class<?> clazz = Class.forName("p2.Type");
< prev index next >