changeset 50926:07f2229cb09c jep-334

updating implementation after amber-dev review comments
author briangoetz
date Fri, 01 Jun 2018 08:51:04 -0700
parents d853e7821dcb
children f0212b293c0e
files src/java.base/share/classes/java/lang/invoke/VarHandle.java src/java.base/share/classes/java/lang/invoke/constant/ClassDesc.java src/java.base/share/classes/java/lang/invoke/constant/ConstantClassDesc.java src/java.base/share/classes/java/lang/invoke/constant/DynamicConstantDesc.java
diffstat 4 files changed, 18 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Fri Jun 01 07:57:11 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Fri Jun 01 08:51:04 2018 -0700
@@ -2150,13 +2150,13 @@
                 this.descFactory = descFactory;
             }
 
-            List<ConstantDesc<?>> toBSMArgs(ClassDesc declaringClass, String name, ClassDesc varType) {
+            ConstantDesc<?>[] toBSMArgs(ClassDesc declaringClass, String name, ClassDesc varType) {
                 switch (this) {
                     case FIELD:
                     case STATIC_FIELD:
-                        return List.of(declaringClass, name, varType);
+                        return new ConstantDesc<?>[] { declaringClass, name, varType };
                     case ARRAY:
-                        return List.of(declaringClass);
+                        return new ConstantDesc<?>[] { declaringClass };
                     default:
                         throw new InternalError("Cannot reach here");
                 }
@@ -2183,7 +2183,7 @@
         private VarHandleDesc(Kind kind, String name, ClassDesc declaringClass, ClassDesc varType) {
             super(kind.bootstrapMethod, name,
                   ConstantDescs.CR_VarHandle,
-                  kind.toBSMArgs(declaringClass, name, varType).toArray(ConstantUtils.EMPTY_CONSTANTDESC));
+                  kind.toBSMArgs(declaringClass, name, varType));
             this.kind = kind;
             this.declaringClass = declaringClass;
             this.varType = varType;
--- a/src/java.base/share/classes/java/lang/invoke/constant/ClassDesc.java	Fri Jun 01 07:57:11 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/constant/ClassDesc.java	Fri Jun 01 08:51:04 2018 -0700
@@ -132,7 +132,7 @@
      */
     default ClassDesc arrayType(int rank) {
         if (rank <= 0)
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException("rank: " + rank);
         return ClassDesc.ofDescriptor("[".repeat(rank) + descriptorString());
     }
 
@@ -234,13 +234,13 @@
      * @return the human-readable name
      */
     default String displayName() {
-        if (descriptorString().length() == 1)
+        if (isPrimitive())
             return Wrapper.forBasicType(descriptorString().charAt(0)).primitiveSimpleName();
-        else if (descriptorString().startsWith("L")) {
+        else if (isClassOrInterface()) {
             return descriptorString().substring(Math.max(1, descriptorString().lastIndexOf('/') + 1),
                                                 descriptorString().length() - 1);
         }
-        else if (descriptorString().startsWith(("["))) {
+        else if (isArray()) {
             int depth = ConstantUtils.arrayDepth(descriptorString());
             ClassDesc c = this;
             for (int i=0; i<depth; i++)
--- a/src/java.base/share/classes/java/lang/invoke/constant/ConstantClassDesc.java	Fri Jun 01 07:57:11 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/constant/ConstantClassDesc.java	Fri Jun 01 08:51:04 2018 -0700
@@ -75,12 +75,12 @@
         for (int i=0; i<depth; i++)
             c = c.componentType();
 
-        if (c.descriptorString().length() == 1)
+        if (c.isPrimitive())
             return lookup.findClass(descriptorString());
         else {
             Class<?> clazz = lookup.findClass(internalToBinary(dropFirstAndLastChar(c.descriptorString())));
             for (int i = 0; i < depth; i++)
-                clazz = Array.newInstance(clazz, 0).getClass();
+                clazz = clazz.arrayType();
             return clazz;
         }
     }
--- a/src/java.base/share/classes/java/lang/invoke/constant/DynamicConstantDesc.java	Fri Jun 01 07:57:11 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/constant/DynamicConstantDesc.java	Fri Jun 01 08:51:04 2018 -0700
@@ -321,26 +321,10 @@
 
     private static Object[] resolveArgs(MethodHandles.Lookup lookup, ConstantDesc<?>[] args)
             throws ReflectiveOperationException {
-        try {
-            return Stream.of(args)
-                    .map(arg -> {
-                        try {
-                            return arg.resolveConstantDesc(lookup);
-                        }
-                        catch (ReflectiveOperationException e) {
-                            throw new RuntimeException(e);
-                        }
-                    })
-                    .toArray();
-        }
-        catch (RuntimeException e) {
-            if (e.getCause() instanceof ReflectiveOperationException) {
-                throw (ReflectiveOperationException) e.getCause();
-            }
-            else {
-                throw e;
-            }
-        }
+        Object[] result = new Object[args.length];
+        for (int i = 0; i < args.length; i++)
+            result[i] = args[i].resolveConstantDesc(lookup);
+        return result;
     }
 
     @SuppressWarnings("unchecked")
@@ -353,12 +337,13 @@
                 throw new BootstrapMethodError(
                         "Invalid bootstrap method declared for resolving a dynamic constant: " + bootstrapMethod);
             }
-            Object[] staticArgs = resolveArgs(lookup, bootstrapArgs);
-            Object[] bsmArgs = new Object[3 + staticArgs.length];
+            Object[] bsmArgs = new Object[3 + bootstrapArgs.length];
             bsmArgs[0] = lookup;
             bsmArgs[1] = constantName;
             bsmArgs[2] = constantType.resolveConstantDesc(lookup);
-            System.arraycopy(staticArgs, 0, bsmArgs, 3, staticArgs.length);
+            for (int i = 0; i < bootstrapArgs.length; i++)
+                bsmArgs[3 + i] = bootstrapArgs[i].resolveConstantDesc(lookup);
+
             return (T) bsm.invokeWithArguments(bsmArgs);
         } catch (Error e) {
             throw e;