I've run into a weird issue with the Groovy / Grails compiler in combination with Java8 the last week.
Found with Grails 2.5.0, not tested in previous versions.
When I have Groovy code which has the following pattern:
class BaseClass {
protected int foo() { return 1 }
}
class SubClass extends BaseClass {
@Override
protected int foo() { return 2 }
}
And I start my application with grails run-app and Java8, then any attempt to dynamically re-compile class SubClass will result in an error:
|Server running. Browse to
http://localhost:8080/GrailsCompilationTest [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[groovyc] /Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: 4: foo() in grailscompiletest.SubClass cannot override foo in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
[groovyc] @ line 4, column 5.
......
[groovyc] @Override
|Compiling 1 source files
[groovyc] ^
.Error
[groovyc]
|
[groovyc] /Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: -1: $getStaticMetaClass() in grailscompiletest.SubClass cannot override $getStaticMetaClass in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
[groovyc] @ line -1, column -1.
[groovyc] 2 errors
2015-04-22 17:14:07,175 [Thread-16] ERROR compiler.GrailsProjectWatcher - Compilation Error: startup failed:
/Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: 4: foo() in grailscompiletest.SubClass cannot override foo in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
@ line 4, column 5.
@Override
^
/Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: -1: $getStaticMetaClass() in grailscompiletest.SubClass cannot override $getStaticMetaClass in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
@ line -1, column -1.
2 errors
| Compiling 1 source files
| Compiling 1 source files.
| Error 2015-04-22 17:14:08,116 [Thread-12] ERROR compiler.GrailsProjectWatcher - Compilation Error: startup failed:
/Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: 4: foo() in grailscompiletest.SubClass cannot override foo in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
@ line 4, column 5.
@Override
^
/Users/gebruiker/IdeaProjects/GrailsCompilationTest/src/groovy/grailscompiletest/SubClass.groovy: -1: $getStaticMetaClass() in grailscompiletest.SubClass cannot override $getStaticMetaClass in grailscompiletest.BaseClass; attempting to assign weaker access privileges; was public
@ line -1, column -1.
2 errors
This compilation error only happens when compilation is triggered from 'grails run-app'; compiling code ahead of time with 'grails compile' there is no problem at all. However there will be a problem if I make a chance to class SubClass and it needs to be recompiled.
When doing the exact same when running on JDK7, there is no problem at all.
Looking at the code, the code is correct - the error must be in the compiler, but is triggered only under these very specific circumstances.
The problem is trivially reproduced by running:
grails create-app
Adding to BuildConfig.groovy 2 dependencies:
compile ':quartz:1.0.2'
compile ':quartz-monitor:1.0'
And then:
grails run-app
Startup will fail with error like the one above.
Alternatively you can create Groovy files for the 2 classes copy-pasted above in an empty application, run with grails run-app, and modify the SubClass and save changes to trigger re-compilation.
This problem makes dynamic development using JDK8 much harder.
Is there an issue for this already, or are the developers already aware of this problem? Should I raise in issue?
Since the issue only shows in the Grails runtime environment, I'm not sure if it's a Groovy issue or a Grails issue.
Kind regards,
--Tim