Why does Groovy compiler reject the dependency JSR308 Type Annotations Compiler?

122 Views Asked by At

I have been struggling to understand why groovyc does not want to compile my code for awhile and I finally narrowed it down to it disliking the dependency JSR308 Type Annotations Compiler, which uses as one of its package names java.lang.annotations.

When groovyc tries to compile my code with this jar in its classpath, the following is printed:

groovyc -cp jsr308-compiler-1.7.5.jar EmptyClass.groovy 
>>> a serious error occurred: null
>>> stacktrace:
java.lang.ExceptionInInitializerError
    at org.codehaus.groovy.classgen.Verifier.<clinit>(Verifier.java:137)
    at org.codehaus.groovy.control.CompilationUnit.<init>(CompilationUnit.java:171)
    at org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit.<init>(JavaAwareCompilationUnit.java:66)
    at org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit.<init>(JavaAwareCompilationUnit.java:57)
    at org.codehaus.groovy.tools.FileSystemCompiler.<init>(FileSystemCompiler.java:64)
    at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:224)
    at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompile(FileSystemCompiler.java:163)
    at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompileWithErrorHandling(FileSystemCompiler.java:203)
    at org.codehaus.groovy.tools.FileSystemCompiler.main(FileSystemCompiler.java:187)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:110)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
Caused by: java.lang.SecurityException: Prohibited package name: java.lang.annotation
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at org.codehaus.groovy.tools.RootLoader.oldFindClass(RootLoader.java:174)
    at org.codehaus.groovy.tools.RootLoader.loadClass(RootLoader.java:146)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.groovy.ast.ClassHelper.<clinit>(ClassHelper.java:129)
    ... 15 more

Running javac -cp jsr308-compiler-1.7.5.jar EmptyClass.java successfully compiles the dummy class.

java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~16.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
groovy -version
Groovy Version: 3.0.0-beta-2 JVM: 1.8.0_222 Vendor: Private Build OS: Linux

From what I have read, the Type Annotations Project was integrated into JDK 8 back in 2014, so I have no idea why groovyc would not recognize this dependency's right to use java.lang.annotation as a package name.

[Update] As requested, here is the code for both EmptyClass.groovy and EmptyClass.java:

public class EmptyClass {

}
1

There are 1 best solutions below

0
John Jekyllson On

I have retried with multiple versions of groovy. It did not work with a 2.5 version, but with 2.0.0 my command actually technically compiles. Unfortunately, there are some other compatibility problems beyond this basic example and I would not recommend that anyone try to make groovy work with JSR-308 if it is not necessary.