It's not a bug but it's not user friendly

Remi Forax forax at univ-mlv.fr
Sat Dec 12 16:07:32 UTC 2020


A student of mine send me a code that can be reduced to this code
 
---
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;

public class ThereIsABugButWhere {
  private static final VarHandle TEXT;
  static {
    try {
      TEXT = MethodHandles.lookup().findVarHandle(ThereIsABugButWhere.class, "text", String.class);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      throw new AssertionError(e);
    }
  }


  private final String text;

  ThereIsABugButWhere() {
    text = "FOO";
  }

  public void update(String s) {
    TEXT.compareAndSet(this, "FOO", s);
  }

  public static void main(String[] args) {
    new ThereIsABugButWhere().update("BAR");
  }
}

---
If you execute it, you get
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.lang.invoke.VarForm.getMemberName(VarForm.java:99)
	at java.base/java.lang.invoke.VarHandleGuards.guard_LLL_Z(VarHandleGuards.java:77)
	at ThereIsABugButWhere.update(ThereIsABugButWhere.java:22)
	at ThereIsABugButWhere.main(ThereIsABugButWhere.java:26)

It takes me 20 mins to find the issue ...
I think we can improve the error message or even better report the issue at the right location :)

regards,
Rémi


More information about the core-libs-dev mailing list