0

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.

akagixxer
  • 1,767
  • 3
  • 21
  • 35
  • please include a [mre] in question (posted code works for me, after adding missing parts) – user16320675 Jun 16 '23 at 16:51
  • It also works for me depending on where I build the project. I believe its some difference in compilers OR environment. I can make it work; what I'm really looking for is an explanation of why a Java compiler might choose to output bytecode that does this. – akagixxer Jun 16 '23 at 16:59
  • Are you using different versions of `ClassB` during compile time and runtime? Compare your runtime classpath against your compile classpath configuration. (You can get the runtime classpath with `System.getProperty("java.class.path")`.) – Thomas Behr Jun 16 '23 at 17:07
  • Good question. No we are using Gradle to pull from a maven repo in Artifactory. The jars/versions etc should be consistent. Also, looking at the git commit history, the method call has never passed in boxed boolean. – akagixxer Jun 16 '23 at 17:09
  • Are you using any bytecode rewriting things like Lombok or annotation processors? – Louis Wasserman Jun 17 '23 at 01:37
  • No bytecode rewriting processors are being used. – akagixxer Jun 20 '23 at 22:18

0 Answers0