changeset 59281:77788f5a4633 nestmates

[nestmates] minor cleanup and improve test documentation
author mchung
date Wed, 18 Mar 2020 15:45:52 -0700
parents f4f1d5294876
children 76bb96f31be6
files src/java.base/share/classes/java/lang/invoke/MethodHandles.java test/hotspot/jtreg/runtime/Nestmates/membership/TestDynamicNestmateMembership.java test/jdk/java/lang/invoke/defineHiddenClass/HiddenNestmateTest.java test/jdk/java/lang/invoke/defineHiddenClass/UnreflectTest.java test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenClassThrow.java test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenInterface.java test/jdk/java/lang/invoke/defineHiddenClass/src/Outer.java test/jdk/java/lang/reflect/AccessibleObject/HiddenClassTest.java
diffstat 8 files changed, 70 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Wed Mar 18 15:45:52 2020 -0700
@@ -1683,9 +1683,12 @@
         }
 
         /**
-         * Defines a class to the same class loader and in the same runtime package and
+         * Creates a class or interface from {@code bytes}
+         * with the same class loader and in the same runtime package and
          * {@linkplain java.security.ProtectionDomain protection domain} as this lookup's
-         * {@linkplain #lookupClass() lookup class}.
+         * {@linkplain #lookupClass() lookup class} as if calling
+         * {@link ClassLoader#defineClass(String,byte[],int,int,ProtectionDomain)
+         * ClassLoader::defineClass}.
          *
          * <p> The {@linkplain #lookupModes() lookup modes} for this lookup must include
          * {@link #PACKAGE PACKAGE} access as default (package) members will be
@@ -1870,7 +1873,7 @@
          *
          * <p> The unloading characteristics are set for each hidden class when it is
          * defined, and cannot be changed later.  An advantage of allowing hidden classes
-         * to be unloaded independently of the loader deemed as their defining loader
+         * to be unloaded independently of the class loader marked as their defining loader
          * is that a very large number of hidden classes may be created by an application.
          * In contrast, if {@code STRONG} is used, then the JVM may run out of memory,
          * just as if normal classes were created by class loaders.
--- a/test/hotspot/jtreg/runtime/Nestmates/membership/TestDynamicNestmateMembership.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/hotspot/jtreg/runtime/Nestmates/membership/TestDynamicNestmateMembership.java	Wed Mar 18 15:45:52 2020 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @summary Test the rules for dynamic nest membership using the Lookup.defineClass API
+ * @summary Test the rules for dynamic nest membership using the Lookup.defineHiddenClass API
  * @compile OtherPackage.java
  * @run main/othervm TestDynamicNestmateMembership
  */
--- a/test/jdk/java/lang/invoke/defineHiddenClass/HiddenNestmateTest.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/invoke/defineHiddenClass/HiddenNestmateTest.java	Wed Mar 18 15:45:52 2020 -0700
@@ -63,13 +63,15 @@
         assertTrue(Arrays.equals(hiddenClass.getNestMembers(), nestHost.getNestMembers()));
     }
 
+    /*
+     * Test a hidden class to have no access to private members of another class
+     */
     @Test
     public void hiddenClass() throws Throwable {
         // define a hidden class
         Lookup lookup = MethodHandles.lookup().defineHiddenClass(bytes, false);
         Class<?> c = lookup.lookupClass();
-        assertTrue((lookup.lookupModes() & PRIVATE) != 0);
-        assertTrue((lookup.lookupModes() & MODULE) != 0);
+        assertTrue(lookup.hasFullPrivilegeAccess());
         assertTrue(c.getNestHost() == c);  // host of its own nest
         assertTrue(c.isHiddenClass());
 
@@ -88,6 +90,9 @@
         } catch (IllegalAccessError e) {}
     }
 
+    /*
+     * Test a hidden class to have access to private members of its nestmates
+     */
     @Test
     public void hiddenNestmate() throws Throwable {
         // define a hidden nestmate class
@@ -106,6 +111,9 @@
         assertTrue(x1 == privMethod());
     }
 
+    /*
+     * Test a hidden class created with NESTMATE and STRONG option is a nestmate
+     */
     @Test
     public void hiddenStrongClass() throws Throwable {
         // define a hidden class strongly referenced by the class loader
@@ -113,6 +121,9 @@
         assertNestmate(lookup);
     }
 
+    /*
+     * Fail to create a hidden class if dropping PRIVATE lookup mode
+     */
     @Test(expectedExceptions = IllegalAccessException.class)
     public void noPrivateLookupAccess() throws Throwable {
         Lookup lookup = MethodHandles.lookup().dropLookupMode(Lookup.PRIVATE);
@@ -130,6 +141,9 @@
         assertNestmate(lc2);
     }
 
+    /*
+     * Fail to create a hidden class in a different package from the lookup class' package
+     */
     @Test(expectedExceptions = IllegalArgumentException.class)
     public void notSamePackage() throws Throwable {
         MethodHandles.lookup().defineHiddenClass(classBytes("p/HiddenInjected"), false, NESTMATE);
--- a/test/jdk/java/lang/invoke/defineHiddenClass/UnreflectTest.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/invoke/defineHiddenClass/UnreflectTest.java	Wed Mar 18 15:45:52 2020 -0700
@@ -25,7 +25,7 @@
  * @test
  * @compile src/Fields.java
  * @run testng/othervm UnreflectTest
- * @summary Test java.lang.reflect.AccessibleObject with modules
+ * @summary Test Lookup::unreflectSetter and Lookup::unreflectVarHandle
  */
 
 import java.io.IOException;
@@ -58,8 +58,12 @@
         }
     }
 
+    /*
+     * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
+     * can write the value of a non-static final field in a normal class
+     */
     @Test
-    public void testNonHiddenFields() throws Throwable {
+    public void testFieldsInNormalClass() throws Throwable {
         // despite the name "HiddenClass", this class is loaded by the
         // class loader as non-hidden class
         Class<?> c = Fields.class;
@@ -71,8 +75,12 @@
         readWriteAccessibleObject(c, "NON_FINAL", o, false);
     }
 
+    /*
+     * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
+     * has NO write the value of a non-static final field in a hidden class
+     */
     @Test
-    public void testHiddenFields() throws Throwable {
+    public void testFieldsInHiddenClass() throws Throwable {
         assertTrue(hiddenClass.isHiddenClass());
         Object o = hiddenClass.newInstance();
         readOnlyAccessibleObject(hiddenClass, "STATIC_FINAL", null, true);
--- a/test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenClassThrow.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenClassThrow.java	Wed Mar 18 15:45:52 2020 -0700
@@ -23,7 +23,7 @@
 
 /*
  *  The classfile for this class will be loaded directly and used to define
- *  a non-findable class.
+ *  a hidden class.
  */
 public class HiddenClassThrow implements HiddenTest {
 
--- a/test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenInterface.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/invoke/defineHiddenClass/src/HiddenInterface.java	Wed Mar 18 15:45:52 2020 -0700
@@ -22,8 +22,8 @@
  */
 
 /*
- *  The classfile for this class will be used to define a non-findable
- *  interface.  The load of this class will fail because non-findable classes
+ *  The classfile for this class will be used to define a hidden interface.
+ *  This class will fail to be created as a hidden class because hidden classes
  *  cannot user their name in field signatures.
  */
 public interface HiddenInterface {
--- a/test/jdk/java/lang/invoke/defineHiddenClass/src/Outer.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/invoke/defineHiddenClass/src/Outer.java	Wed Mar 18 15:45:52 2020 -0700
@@ -1,3 +1,26 @@
+/*
+ * Copyright (c) 2019, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
 public class Outer {
     public static class Inner {
     }
--- a/test/jdk/java/lang/reflect/AccessibleObject/HiddenClassTest.java	Tue Mar 17 11:37:58 2020 -0700
+++ b/test/jdk/java/lang/reflect/AccessibleObject/HiddenClassTest.java	Wed Mar 18 15:45:52 2020 -0700
@@ -55,8 +55,12 @@
         }
     }
 
+    /*
+     * Test Field::set that can write the value of a non-static final field
+     * in a normal class
+     */
     @Test
-    public void testNonHiddenFields() throws Exception {
+    public void testFieldsInNormalClass() throws Throwable {
         // despite the name "HiddenClass", this class is loaded by the
         // class loader as non-hidden class
         Class<?> c = Fields.class;
@@ -68,8 +72,12 @@
         readWriteAccessibleObject(c, "NON_FINAL", o, false);
     }
 
+    /*
+     * Test Field::set that fails to write the value of a non-static final field
+     * in a hidden class
+     */
     @Test
-    public void testHiddenFields() throws Exception {
+    public void testFieldsInHiddenClass() throws Throwable {
         assertTrue(hiddenClass.isHiddenClass());
         Object o = hiddenClass.newInstance();
         readOnlyAccessibleObject(hiddenClass, "STATIC_FINAL", null, true);