janino-compiler / janino

Janino is a super-small, super-fast Java™ compiler.
http://janino-compiler.github.io/janino
Other
1.21k stars 205 forks source link

ClassFormatError when assigning integer to constant double #177

Closed cvogt729 closed 1 year ago

cvogt729 commented 1 year ago

I'm using Janino 3.1.7 with SimpleCompiler to compile and execute the following code:

public class test2 {
  public final static double x = 0;
  public static void main(String[] args){
    System.out.println("Hello World!");
  }
}

When invoking SimpleCompiler in Windows PowerShell with OpenJDK 11.0.14.1:

java '-cp' 'janino-3.1.7.jar;commons-compiler-3.1.7.jar' 'org.codehaus.janino.SimpleCompiler' 'test2.java' 'test2'

I get the following error:

Exception in thread "main" java.lang.ClassFormatError: Inconsistent constant value type in class file test2
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader.findClass(ByteArrayClassLoader.java:77)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at org.codehaus.janino.SimpleCompiler.main(SimpleCompiler.java:115)

The error goes away when you replace 0 with 0.0. While investigating, I found a forum that mentions a similar bug 7 years ago, but I guess it was never brought to anyone's attention: https://fractalsoftworks.com/forum/index.php?topic=9472.0

aunkrig commented 1 year ago

Seems like I fixed this one together with #178. Please test!

cvogt729 commented 1 year ago

This issue is not yet resolved. I should mention that the ClassFormatError occurs at runtime, not compile-time. So the compilation test does not demonstrate the problem.

aunkrig commented 1 year ago

Hey Cameron,

you are right; the #178 fix did not fix this one.

I looked into it and implemented a fix and a regression test case. Please test.

cvogt729 commented 1 year ago

This issue is resolved. Thank you!