8154447: Exempt classes under java.util.concurrent from MH.Lookup restrictions
authorpsandoz
Tue, 26 Apr 2016 18:42:51 -0700
changeset 37669 6ba0f2bb6e30
parent 37668 34a002e5168a
child 37670 0b61883743f7
8154447: Exempt classes under java.util.concurrent from MH.Lookup restrictions Reviewed-by: mchung, martin
jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
jdk/test/java/lang/invoke/JavaUtilConcurrentLookupTest.java
jdk/test/java/lang/invoke/java.base/java/util/concurrent/LookupTester.java
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Apr 26 18:30:00 2016 -0700
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Apr 26 18:42:51 2016 -0700
@@ -745,10 +745,13 @@
             if (name.startsWith("java.lang.invoke."))
                 throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
 
-            // For caller-sensitive MethodHandles.lookup()
-            // disallow lookup more restricted packages
+            // For caller-sensitive MethodHandles.lookup() disallow lookup from
+            // restricted packages.  This a fragile and blunt approach.
+            // TODO replace with a more formal and less fragile mechanism
+            // that does not bluntly restrict classes under packages within
+            // java.base from looking up MethodHandles or VarHandles.
             if (allowedModes == ALL_MODES && lookupClass.getClassLoader() == null) {
-                if (name.startsWith("java.") ||
+                if ((name.startsWith("java.") && !name.startsWith("java.util.concurrent.")) ||
                         (name.startsWith("sun.") && !name.startsWith("sun.invoke."))) {
                     throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
                 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/invoke/JavaUtilConcurrentLookupTest.java	Tue Apr 26 18:42:51 2016 -0700
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/* @test
+ * @summary Tests that Lookup can be produced from classes under java.util.concurrent
+ * @bug 8154447
+ * @compile/module=java.base java/util/concurrent/LookupTester.java
+ * @run testng/othervm JavaUtilConcurrentLookupTest
+ */
+
+import org.testng.annotations.Test;
+
+import java.util.concurrent.LookupTester;
+
+public class JavaUtilConcurrentLookupTest {
+
+    @Test
+    public void testLookup() {
+        LookupTester.getLookup();
+    }
+
+    @Test
+    public void testLookupIn() {
+        LookupTester.getLookupIn();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/invoke/java.base/java/util/concurrent/LookupTester.java	Tue Apr 26 18:42:51 2016 -0700
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+package java.util.concurrent;
+
+import java.lang.invoke.MethodHandles;
+
+public class LookupTester {
+    public static MethodHandles.Lookup getLookup() {
+        return MethodHandles.lookup();
+    }
+
+
+    public static MethodHandles.Lookup getLookupIn() {
+        return MethodHandles.lookup().in(ConcurrentHashMap.class);
+    }
+}