2.5.3 breaks Spock delayed $spock_initializeFields for final fields

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

2.5.3 breaks Spock delayed $spock_initializeFields for final fields

Leonard Brünings


it seems that 2.5.3 introduced some additional checks that causes these exceptions:

java.lang.IllegalAccessError: Update to static final field spock.util.concurrent.PollingConditionsSpec.$const$0 attempted from a different method (__$swapInit) than the initializer method <clinit>
            at spock.util.concurrent.PollingConditionsSpec.__$swapInit(PollingConditionsSpec.groovy)
            at spock.util.concurrent.PollingConditionsSpec.<clinit>(PollingConditionsSpec.groovy)

(https://github.com/spockframework/spock/pull/900#issuecomment-420158524 is the source)

Spock does some AST rewrites
class AClass extends Specification {
 String a = "test"
 final String b = "test"

gets transformed into

class AClass extends Specification {
    @org.spockframework.runtime.model.FieldMetadata(name = 'a', ordinal = 0, line = 5, initializer = true)
    private java.lang.String a 
    @org.spockframework.runtime.model.FieldMetadata(name = 'b', ordinal = 1, line = 6, initializer = true)
    final private java.lang.String b 

    private java.lang.Object $spock_initializeFields() {
        a = 'test'
        b = 'test'
This allows spock to delay the initialization of fields after the instance creation. What is the best solution for this? Should Spock remove the final modifier, or is there a way to mark the $spock_initializeFields method as valid initializer?