Java Compiler Incorrect Boxed Primitive Coercion

50 views Asked by At

USING: openjdk-11-jdk-headless=11.0.16+8-0ubuntu1~20.04

I have ClassB with a static creation method in a library.

public ClassB
{
  public static ClassB create(UUID id, boolean flag);
}

In another library I have ClassA calling the creation method like this:

// Note that "false" is passed in as a literal.
return ClassB.create(someId, false);

I get NoSuchMethodError and it reports the signature called as (java.util.UUID, java.lang.Boolean).

When I decompile the ClassA.class file it gives me this:

// With CFR
ClassB getInputs(UUID id) {
  return ClassB.create((UUID)id, (Boolean)false);
}

// With Procyon
ClassB getInputs(final UUID id) {
  return ClassB.create(id, Boolean.valueOf(false));
}

Both decompilers show that, when compiled, my method call ends up coercing the primitive boolean literal that I pass in to a boxed Boolean which makes all my calls blow up at runtime.

Does anyone know why this is happening?

NOTE:
I have confirmed that I can fix this by declaring a variable:
private static final boolean FLAG = false;
but I want to understand why this is happening.

0

There are 0 answers