After a general system (Java, Groovy, Gradle) upgrade I get this problem with otherwise unchanged code.
Full message:
java.lang.IllegalAccessError: Update to non-static final field org.mohme.gradle.BuildLogicFunctionalTest.testProjectDir attempted from a different method ($spock_initializeFields) than the initializer method
at org.mohme.gradle.BuildLogicFunctionalTest.$spock_initializeFields(BuildLogicFunctionalTest.groovy:13)
This looks similar to (same as?) issue #911, #963
./gradlew clean buildBuildLogicFunctionalTestjava -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)
[edit] The issue does not occur with Oracle Java8 (1.8.0_202-b08)
groovy -version
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/usr/local/Cellar/groovy/2.5.8/libexec/lib/groovy-2.5.8.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Groovy Version: 2.5.8 JVM: 11.0.4 Vendor: AdoptOpenJDK OS: Mac OS X
Attention: Although the system has Groovy 2.5.8 installed, the build (intentionally) uses the Groovy version (2.5.4) which comes with the used Gradle version (see below) => The problem occurs with Groovy 2.5.4!
./gradlew -version
Build time: 2019-08-14 21:05:25 UTC
Revision: f0b9d60906c7b8c42cd6c61a39ae7b74767bb012
Kotlin: 1.3.41
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM: 11.0.4 (AdoptOpenJDK 11.0.4+11)
OS: Mac OS X 10.14.2 x86_64
Mac OS X 10.14.2 x86_64
testCompile("org.spockframework:spock-core:1.3-groovy-2.5") {
exclude(module = "groovy-all")
}
./gradlew dependencies --configuration testCompile
--- org.spockframework:spock-core:1.3-groovy-2.5
+--- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-json:2.5.4
| --- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-nio:2.5.4
| --- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-macro:2.5.4
| --- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-templates:2.5.4
| +--- org.codehaus.groovy:groovy:2.5.4
| --- org.codehaus.groovy:groovy-xml:2.5.4
| --- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-test:2.5.4
| +--- org.codehaus.groovy:groovy:2.5.4
| --- junit:junit:4.12
| --- org.hamcrest:hamcrest-core:1.3
+--- org.codehaus.groovy:groovy-sql:2.5.4
| --- org.codehaus.groovy:groovy:2.5.4
+--- org.codehaus.groovy:groovy-xml:2.5.4 ()
--- junit:junit:4.12 ()
Have we same issue:
openjdk 11.0.4 2019-07-16
groovy 2.5.8
spock 1.3-groovy-2.5
Not sure what you are asking, but modification of final fields ist
generally not possible and hence the error is legit.
Am Mo., 14. Okt. 2019 um 11:55 Uhr schrieb joistick11 <
[email protected]>:
Have we same issue:
openjdk 11.0.4 2019-07-16
groovy 2.5.8
spock 1.3-groovy-2.5—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/spockframework/spock/issues/1011?email_source=notifications&email_token=AA4H5KSXA2I7VTQT6I4TPG3QOQ6YFA5CNFSM4IMQEZD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBD7HRQ#issuecomment-541586374,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA4H5KRPR24WC5LQ7QOFGATQOQ6YFANCNFSM4IMQEZDQ
.
Sorry, I have found the exact reason the tests failed for me - I was trying to access final variable (RO) in test. Here how you can reproduce the problem:
class ExampleFailingSpec extends Specification {
private final String path = "src/test/resources"
def "readProps"() {
expect:
path + "/some" == "src/test/resources/some"
}
}
Is this a correct behaviour?
Removing the final keyword solved my issue.
@tmohme I think you have the same problem. In BuildLogicFunctionalTest you have final var testProjectDir.
Actually this is not a modification to path since path + "/some" create a new String variable. So the error is not legit IMHO. Probably there is some AST magic involved here, which probably should not happen.
@mkutz
In my code I also do not modify the final field.
Besides, the message in the exception clearly states that the modification was . . .
... attempted from a different method ($spock_initializeFields) than the initializer method
at org.mohme.gradle.BuildLogicFunctionalTest.$spock_initializeFields(BuildLogicFunctionalTest.groovy:13)
I haven't written the method in question ("$spock_initializeFields"), so I can only guess it was created by some kind of Spock/groovy "magic" which I don't know how to influence.
As you said this probably should not happen - and it is the reason why I created this issue.
I ran into this too when attempting to upgrade to Java 11.
openjdk 11.0.5 2019-10-15
Groovy 2.5.8
@joistick11
Removing the
finalkeyword solved my issue.
If you remove the ExampleFailingSpec test class it will "solve the issue" too :trollface:
@ all
Is there any proper fix to this issue? I've just upgraded a project to JDK 11 and quite a bunch of tests are exploding because of that. Although I think only when run under IDE (at least so far).
To reproduce:
(assuming Maven project)
xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<!-- alternatively the below configuration properties can be used - doesn't matter really -->
<!-- <source>11</source> -->
<!-- <target>11</target> -->
</configuration>
</plugin>
Add following test class:
import spock.lang.Specification
class FooTest extends Specification {
final foo = 5
def "plop is 5"() {
expect:
foo == 5
}
}
Key points to note:
So we get in kind of a weird state where configuring Maven compiler plugin to Java 11 at the project's _pom.xml_ makes issue manifest under IDE but doesn't bother Maven build itself at all :roll_eyes:
I've seen posts that the issue manifests to some folks when tests are run with either Gradle or Maven. Can't tell about Gradle this time but seems with the versions I used (all the most recent) it's a problem with IDEA. Maybe some IDE compiler's settings can be tweaked (which?) or it might be an IntelliJ's :bug:
Versions used:
11.0.5 (2019-10-15)2.5.81.3-groovy-2.51.8.13.6.02019.2.4IDEA:
Corresponding issue on Jetbrains issue tracker.
Consider voting it up (no need to sign up, can use GitHub account).
I got this issue as well. It works with Java8. I had to remove final keywords to get it working with Java 12.

Replicated with 2.0-M3-groovy-3.0 on AdoptOpenJDK 11.0.7.
@Vampire and I talked about this, and the best way we came up with honoring the intend of final while also keep Spock working as it does, is to do something like we do when we rewrite shared fields in https://github.com/spockframework/spock/blob/7d6f24ebbfb9311d36361809d21f30a2757f3ff6/spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java#L84-L90
Basically hiding the field and generating a getter for it, so to the users it would still be read-only while spock could still defer the initialization.
@mlkristiansen you wanted to take a look at this.
Most helpful comment
@joistick11
If you remove the
ExampleFailingSpectest class it will "solve the issue" too :trollface:@ all
Is there any proper fix to this issue? I've just upgraded a project to JDK 11 and quite a bunch of tests are exploding because of that. Although I think only when run under IDE (at least so far).
To reproduce:
(assuming Maven project)
xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> <!-- alternatively the below configuration properties can be used - doesn't matter really --> <!-- <source>11</source> --> <!-- <target>11</target> --> </configuration> </plugin>Add following test class:
Key points to note:
So we get in kind of a weird state where configuring Maven compiler plugin to Java 11 at the project's _pom.xml_ makes issue manifest under IDE but doesn't bother Maven build itself at all :roll_eyes:
I've seen posts that the issue manifests to some folks when tests are run with either Gradle or Maven. Can't tell about Gradle this time but seems with the versions I used (all the most recent) it's a problem with IDEA. Maybe some IDE compiler's settings can be tweaked (which?) or it might be an IntelliJ's :bug:
Versions used:
11.0.5(2019-10-15)2.5.81.3-groovy-2.51.8.13.6.02019.2.4IDEA:
Corresponding issue on Jetbrains issue tracker.
Consider voting it up (no need to sign up, can use GitHub account).