Constant descriptor resolved too soon during constant resolution?

Jorn Vernee jbvernee at xs4all.nl
Sat Aug 4 09:36:01 UTC 2018


> Brian told me in an earlier email that `ConstantDesc`s should also be
> able to describe themselves, so I think the only missing puzzle piece
> here is to add a method to `ConstantDesc` for that:

Or I guess a much simpler solution is to cast the descriptors to 
`Constable`, since any `ConstantDesc` _should_ implement it:

```
diff -r 8ba8e64cc9c2 
src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java
--- 
a/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java 
   Thu Jul 26 22:07:46 2018 +0200
+++ 
b/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java 
   Sat Aug 04 11:29:34 2018 +0200
@@ -364,7 +364,21 @@
          args[2] = bootstrapMethod.methodType().descriptorString();
          args[3] = constantName;
          args[4] = constantType.descriptorString();
-        System.arraycopy(bootstrapArgs, 0, args, 5, 
bootstrapArgs.length);
+
+        int i = 5;
+        for(ConstantDesc<?> bsArg : bootstrapArgs) {
+            if(bsArg instanceof Constable) { // should always be true
+                Constable<?> constable = (Constable<?>) bsArg;
+                Optional<? extends ConstantDesc<?>> descOp = 
constable.describeConstable();
+                if(descOp.isPresent()) { // should always have a value
+                    args[i] = descOp.get();
+                    i++;
+                    continue;
+                }
+            }
+            return Optional.empty(); // fallback just in case
+        }
+
          return 
Optional.of(DynamicConstantDesc.of(BSM_DYNAMICCONSTANTDESC, 
ConstantDescs.DEFAULT_NAME,
                                                    
CR_DynamicConstantDesc, args));
      }
```

That patch fixes the bug. It generates this BSMT entry for the 
`DynamicConstantDesc`:

```
   0: #26 REF_invokeStatic 
java/lang/constant/DynamicConstantDesc.constantBootstrap:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/constant/ConstantDesc;)Ljava/lang/constant/DynamicConstantDesc;
     Method arguments:
       #27 Ljava/lang/invoke/ConstantBootstraps;
       #28 invoke
       #29 
(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;
       #30 _
       #31 Ljava/lang/Object;
       #32 #2:_:Ljava/lang/constant/DirectMethodHandleDesc;
       #33 Hello,
       #34 world!
```

Which looks right to me. It has a dynamic constant in there to 
reconstitute the method handle descriptor.

Sorry about my earlier email, I was a little too eager to find a 
solution and was just kind of thinking out loud in the email and 
probably being a little incoherent.

Jorn


More information about the amber-dev mailing list