Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassFormatError when processors passed to Reflect.compile() produce new source files #81

Open
lukaseder opened this issue May 10, 2019 · 6 comments

Comments

@lukaseder
Copy link
Member

When a processor passed to Reflect.compile() attempts to produce new source files, we get the following error:

Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1885430635 in class file test/Data
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:877)
	at org.joor.Compile$ByteArrayClassLoader.findClass(Compile.java:169)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at org.joor.Compile.lambda$compile$3(Compile.java:142)
	at org.joor.Compile$ClassFileManager.loadAndReturnMainClass(Compile.java:232)
	at org.joor.Compile.compile(Compile.java:141)
	at org.joor.Reflect.compile(Reflect.java:102)
	at org.jooq.annotation.processor.Test.main(Test.java:9)
@ghost
Copy link

ghost commented Mar 13, 2020

Hi, @lukaseder!
+1 to this issue.
I also faced this issue, while trying to test my annotation processor with jOOR.
But I have another error... Kind of this:

Caused by: java.lang.RuntimeException: java.lang.UnsupportedOperationException
        at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:163)
        at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
        at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
        at org.joor.Compile.compile(Compile.java:97)
        ... 67 more
Caused by: java.lang.UnsupportedOperationException
        at [email protected]/javax.tools.SimpleJavaFileObject.getCharContent(SimpleJavaFileObject.java:131)
        at jdk.compiler/com.sun.tools.javac.api.ClientCodeWrapper$WrappedFileObject.getCharContent(ClientCodeWrapper.java:661)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.readSource(JavaCompiler.java:613)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:676)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:1031)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.<init>(JavacProcessingEnvironment.java:1117)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.next(JavacProcessingEnvironment.java:1153)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1392)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1263)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:935)
        at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
        at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:147)
        ... 70 more

Tested on Java 13, as you can see.

java -version

openjdk version "13.0.2" 2020-01-14

javax.tools.SimpleJavaFileObject#getCharContent just throws an exception and org.joor.Compile.JavaFileObject doesn't override this method and that's it.

@lukaseder
Copy link
Member Author

Thanks, @jbreathe. Perhaps it's the same error, perhaps it isn't. To be sure, would you mind creating a new issue with a full example on how to reproduce this?

@ghost
Copy link

ghost commented Mar 16, 2020

Sure, @lukaseder!

@gabrielps
Copy link

I'm having the same error as @lukaseder. Will probably fork and try to solve it.

@michael-schnell
Copy link

michael-schnell commented May 12, 2020

Same problem here ("java.lang.ClassFormatError: Incompatible magic value") while trying to test an annotation processor.

It seems that the source code generated by the annotation processor is interpreted as byte code. In ByteArrayClassLoader the return defineClass(name, bytes, 0, bytes.length) method fails, because the bytes array has the source and not class byte code. So the failing "magical number" is actually representing the first bytes of the source code created by the annotation processor.

@muehmar
Copy link

muehmar commented Sep 15, 2021

I'm developing an annotation processor which should create a new source file for each annotated class and facing the same issue when trying to use jOOR in tests:

Incompatible magic value 1885430635 in class file CustomerExtension
java.lang.ClassFormatError: Incompatible magic value 1885430635 in class file CustomerExtension
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:878)
	at org.joor.Compile$ByteArrayClassLoader.findClass(Compile.java:183)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at org.joor.Compile.lambda$compile$3(Compile.java:150)
	at org.joor.Compile$ClassFileManager.loadAndReturnMainClass(Compile.java:251)
	at org.joor.Compile.compile(Compile.java:149)
	at org.joor.Reflect.compile(Reflect.java:104)

I guess the new source files should get compiled before the class can be loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants