Resolution/translation of VarHandle.compareAndSet seems to conflict with JVMS/javadoc.

Jorn Vernee jbvernee at
Sat Jan 5 19:56:24 UTC 2019


I'm bringing this up based on a question asked on Stack Overflow [1].

For the following code:

public class Main {
     public int publicTestVariable = 10;

     public static void main(String[] args) throws Throwable {
	Main e = new Main();

     public void update() throws Throwable {
	VarHandle publicIntHandle = MethodHandles.lookup()
	    .findVarHandle(Main.class, "publicTestVariable", int.class);
	publicIntHandle.compareAndSet(this, 10, 100);

javac derives the descriptor for the call to `compareAndSet` [2] as 
`(Lmain/Main;II)Z`, but eclipsec derives the descriptor as 
`(Lmain/Main;II)V`. Note that `compareAndSet` is signature polymorphic.

Each compiler generates a different return type. The one generated by 
eclipsec seems to be correct since the call site has no indication that 
the call should return a boolean. According to the javadoc for Signature 
polymorphism [3] "The unusual part is that the symbolic type descriptor 
is derived from the actual argument and return types, not from the 
method declaration.", and the code generated by javac definitely seems 
to depend on the method declaration, and not just the call site.

When running the eclipsec generated code on jdk/jdk tip this throws a 

     Exception in thread "main" java.lang.NoSuchMethodError: 
         at main.Main.update(
         at main.Main.main(

While the code generated by javac runs without an error.

Regardless of the question whether discarding the returned value is a 
good idea or not, the exception being thrown seems to conflict with 
JVMS- [4] which states:

"If C declares exactly one method with the name specified by the method 
reference, and the declaration is a signature polymorphic method 
(§2.9.3), then method lookup succeeds. All the class names mentioned in 
the descriptor are resolved (§ The resolved method is the 
signature polymorphic method declaration. It is not necessary for C to 
declare a method with the descriptor specified by the method reference."

The javadoc [3] also says that: "When the JVM processes bytecode 
containing signature polymorphic calls, it will successfully link any 
such call, regardless of its symbolic type descriptor".

So the VM should not be throwing an NSME here AFAICT. But, it looks like 
this case is being checked explicitly and an error is being thrown [5]:

     if ( {
         if ( != mtype.returnType()) {
             // The caller contains a different return type than that
             // defined by the method
             throw newNoSuchMethodErrorOnVarHandle(name, mtype);
         // Adjust the return type of the signature method type
         guardReturnType =;

I guess this is being done because the return type of compareAndSet is 
monomorphic, but imho the call should still succeed with `void` as a 
return type (discarding the value). For other types, it would be more 
consistent with the spec/doc if the return type was dynamically cast to 
that of the descriptor, with possible CCE, but linking still succeeded.

Any thoughts on this?


[1] :
[2] :
[3] :
[4] :
[5] :

More information about the jdk-dev mailing list