8140273: restore use of CompositeOperation.contains where it is needed
authorattila
Thu, 22 Oct 2015 10:43:10 +0200
changeset 33345 ef8c859f7992
parent 33344 cc4a221f42b7
child 33346 1c5439fcdc26
8140273: restore use of CompositeOperation.contains where it is needed Reviewed-by: hannesw, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/CompositeOperation.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/DynamicLinker.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JavaSuperAdapterLinker.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/CompositeOperation.java	Wed Oct 21 18:39:15 2015 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/CompositeOperation.java	Thu Oct 22 10:43:10 2015 +0200
@@ -204,6 +204,23 @@
     }
 
     /**
+     * Returns true if this composite operation contains an operation equal to
+     * the specified operation.
+     * @param operation the operation being searched for. Must not be null.
+     * @return true if the if this composite operation contains an operation
+     * equal to the specified operation.
+     */
+    public boolean contains(final Operation operation) {
+        Objects.requireNonNull(operation);
+        for(final Operation component: operations) {
+            if (component.equals(operation)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Returns true if the other object is also a composite operation and their
      * component operations are equal.
      * @param obj the object to compare to
@@ -257,4 +274,24 @@
                 ? ((CompositeOperation)op).operations.clone()
                 : new Operation[] { op };
     }
+
+    /**
+     * Returns true if the specified potentially composite operation is a
+     * {@link CompositeOperation} and contains an operation equal to the
+     * specified operation. If {@code composite} is not a
+     * {@link CompositeOperation}, then the two operations are compared for
+     * equality.
+     * @param composite the potentially composite operation. Must not be null.
+     * @param operation the operation being searched for. Must not be null.
+     * @return true if the if the passed operation is a
+     * {@link CompositeOperation} and contains a component operation equal to
+     * the specified operation, or if it is not a {@link CompositeOperation} and
+     * is equal to {@code operation}.
+     */
+    public static boolean contains(final Operation composite, final Operation operation) {
+        if (composite instanceof CompositeOperation) {
+            return ((CompositeOperation)composite).contains(operation);
+        }
+        return composite.equals(Objects.requireNonNull(operation));
+    }
 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/DynamicLinker.java	Wed Oct 21 18:39:15 2015 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/DynamicLinker.java	Thu Oct 22 10:43:10 2015 +0200
@@ -123,7 +123,13 @@
  *     }
  *
  *     public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type) {
- *         return dynamicLinker.link(new SimpleRelinkableCallSite(new CallSiteDescriptor(lookup, name, type)));
+ *         return dynamicLinker.link(
+ *             new SimpleRelinkableCallSite(
+ *                 new CallSiteDescriptor(lookup, parseOperation(name), type)));
+ *     }
+ *
+ *     private static Operation parseOperation(String name) {
+ *         ...
  *     }
  * }
  * </pre>
@@ -151,8 +157,10 @@
  *
  * <li>You also need to provide {@link CallSiteDescriptor}s to your call sites.
  * They are immutable objects that contain all the information about the call
- * site: the class performing the lookups, the name of the method being invoked,
- * and the method signature.</li>
+ * site: the class performing the lookups, the operation being invoked, and the
+ * method signature. You will have to supply your own scheme to encode and
+ * decode operations in the call site name or static parameters, that is why
+ * in the above example the {@code parseOperation} method is left unimplemented.</li>
  *
  * </ul>
  */
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JavaSuperAdapterLinker.java	Wed Oct 21 18:39:15 2015 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JavaSuperAdapterLinker.java	Thu Oct 22 10:43:10 2015 +0200
@@ -77,7 +77,7 @@
 
         final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
 
-        if(NashornCallSiteDescriptor.getFirstStandardOperation(descriptor) != StandardOperation.GET_METHOD) {
+        if(!NashornCallSiteDescriptor.contains(descriptor, StandardOperation.GET_METHOD)) {
             // We only handle GET_METHOD
             return null;
         }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java	Wed Oct 21 18:39:15 2015 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java	Thu Oct 22 10:43:10 2015 +0200
@@ -335,6 +335,18 @@
     }
 
     /**
+     * Returns true if the passed call site descriptor's operation contains (or
+     * is) the specified standard operation.
+     * @param desc the call site descriptor.
+     * @param operation the operation whose presence is tested.
+     * @return Returns true if the call site descriptor's operation contains (or
+     * is) the specified standard operation.
+     */
+    public static boolean contains(final CallSiteDescriptor desc, final StandardOperation operation) {
+        return CompositeOperation.contains(NamedOperation.getBaseOperation(desc.getOperation()), operation);
+    }
+
+    /**
      * Returns the error message to be used when CALL or NEW is used on a non-function.
      *
      * @param obj object on which CALL or NEW is used
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java	Wed Oct 21 18:39:15 2015 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java	Thu Oct 22 10:43:10 2015 +0200
@@ -129,7 +129,7 @@
             // allow 'static' access on Class objects representing public classes of non-restricted packages
             if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
                 final CallSiteDescriptor desc = request.getCallSiteDescriptor();
-                if ("static".equals(NashornCallSiteDescriptor.getOperand(desc)) && NashornCallSiteDescriptor.getFirstStandardOperation(desc) == StandardOperation.GET_PROPERTY) {
+                if ("static".equals(NashornCallSiteDescriptor.getOperand(desc)) && NashornCallSiteDescriptor.contains(desc, StandardOperation.GET_PROPERTY)) {
                     if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {
                         // If "GET_PROPERTY:static" passes access checks, allow access.
                         return;