changeset 51054:a2438bc09010 jep-334

add interface aware kinds to MethodHandleDesc
author vromero
date Fri, 08 Jun 2018 12:28:45 -0700
parents 0001cbfdf09b
children 7f2baec494c9 4ae74db0ee18
files src/java.base/share/classes/java/lang/constant/ConstantMethodHandleDesc.java src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java src/java.base/share/classes/java/lang/invoke/MethodHandle.java test/jdk/java/lang/constant/MethodHandleRefTest.java
diffstat 4 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/constant/ConstantMethodHandleDesc.java	Thu Jun 07 12:21:54 2018 -0700
+++ b/src/java.base/share/classes/java/lang/constant/ConstantMethodHandleDesc.java	Fri Jun 08 12:28:45 2018 -0700
@@ -158,11 +158,13 @@
         MethodType resolvedType = this.type.resolveConstantDesc(lookup);
         switch (kind) {
             case STATIC:
+            case INTERFACE_STATIC:
                 return lookup.findStatic(resolvedOwner, name, resolvedType);
             case INTERFACE_VIRTUAL:
             case VIRTUAL:
                 return lookup.findVirtual(resolvedOwner, name, resolvedType);
             case SPECIAL:
+            case INTERFACE_SPECIAL:
                 return lookup.findSpecial(resolvedOwner, name, resolvedType, lookup.lookupClass());
             case CONSTRUCTOR:
                 return lookup.findConstructor(resolvedOwner, resolvedType);
--- a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java	Thu Jun 07 12:21:54 2018 -0700
+++ b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java	Fri Jun 08 12:28:45 2018 -0700
@@ -52,12 +52,16 @@
     public enum Kind {
         /** A method handle for a method invoked as with {@code invokestatic} */
         STATIC(REF_invokeStatic),
+        /** A method handle for a method invoked as with {@code invokestatic} */
+        INTERFACE_STATIC(REF_invokeStatic, true),
         /** A method handle for a method invoked as with {@code invokevirtual} */
         VIRTUAL(REF_invokeVirtual),
         /** A method handle for a method invoked as with {@code invokeinterface} */
-        INTERFACE_VIRTUAL(REF_invokeInterface),
+        INTERFACE_VIRTUAL(REF_invokeInterface, true),
         /** A method handle for a method invoked as with {@code invokespecial} */
         SPECIAL(REF_invokeSpecial),
+        /** A method handle for an interface method invoked as with {@code invokespecial} */
+        INTERFACE_SPECIAL(REF_invokeSpecial, true),
         /** A method handle for a constructor */
         CONSTRUCTOR(REF_newInvokeSpecial),
         /** A method handle for a read accessor for an instance field  */
@@ -73,10 +77,14 @@
          * as defined by {@link MethodHandleInfo}
          */
         public final int refKind;
+        /** Is this an interface
+         */
+        public final boolean isInterface;
 
         Kind(int refKind) {
-            this.refKind = refKind;
+            this(refKind, false);
         }
+        Kind(int refKind, boolean isInterface) { this.refKind = refKind; this.isInterface = isInterface; }
     }
 
 
--- a/src/java.base/share/classes/java/lang/invoke/MethodHandle.java	Thu Jun 07 12:21:54 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/MethodHandle.java	Fri Jun 08 12:28:45 2018 -0700
@@ -1522,8 +1522,10 @@
         ClassDesc owner;
         String name;
         MethodTypeDesc type;
+        boolean isInterface;
         try {
             info = IMPL_LOOKUP.revealDirect(this);
+            isInterface = info.getDeclaringClass().isInterface();
             owner = info.getDeclaringClass().describeConstable().orElseThrow();
             type = info.getMethodType().describeConstable().orElseThrow();
             name = info.getName();
@@ -1544,9 +1546,13 @@
             case REF_invokeVirtual:
                 return Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.VIRTUAL, owner, name, type));
             case REF_invokeStatic:
-                return Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, owner, name, type));
+                return isInterface ?
+                        Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_STATIC, owner, name, type)) :
+                        Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, owner, name, type));
             case REF_invokeSpecial:
-                return Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, owner, name, type));
+                return isInterface ?
+                        Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_SPECIAL, owner, name, type)) :
+                        Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, owner, name, type));
             case REF_invokeInterface:
                 return Optional.of(MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_VIRTUAL, owner, name, type));
             case REF_newInvokeSpecial:
--- a/test/jdk/java/lang/constant/MethodHandleRefTest.java	Thu Jun 07 12:21:54 2018 -0700
+++ b/test/jdk/java/lang/constant/MethodHandleRefTest.java	Fri Jun 08 12:28:45 2018 -0700
@@ -162,15 +162,15 @@
     public void testMethodHandleRef() throws Throwable {
         MethodHandleDesc ctorRef = MethodHandleDesc.of(MethodHandleDesc.Kind.CONSTRUCTOR, testClass, "<ignored!>", CR_void);
         MethodHandleDesc staticMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, testClass, "sm", "(I)I");
-        MethodHandleDesc staticIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, testInterface, "sm", "(I)I");
+        MethodHandleDesc staticIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_STATIC, testInterface, "sm", "(I)I");
         MethodHandleDesc instanceMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.VIRTUAL, testClass, "m", "(I)I");
         MethodHandleDesc instanceIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_VIRTUAL, testInterface, "m", "(I)I");
         MethodHandleDesc superMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, testSuperclass, "m", "(I)I");
-        MethodHandleDesc superIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, testInterface, "m", "(I)I");
+        MethodHandleDesc superIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_SPECIAL, testInterface, "m", "(I)I");
         MethodHandleDesc privateMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, testClass, "pm", "(I)I");
-        MethodHandleDesc privateIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.SPECIAL, testInterface, "pm", "(I)I");
+        MethodHandleDesc privateIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_SPECIAL, testInterface, "pm", "(I)I");
         MethodHandleDesc privateStaticMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, testClass, "psm", "(I)I");
-        MethodHandleDesc privateStaticIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, testInterface, "psm", "(I)I");
+        MethodHandleDesc privateStaticIMethodRef = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_STATIC, testInterface, "psm", "(I)I");
 
         for (MethodHandleDesc r : List.of(ctorRef, staticMethodRef, staticIMethodRef, instanceMethodRef, instanceIMethodRef))
             testMethodHandleRef(r);