Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy closure not work with static final field from super class

class Parent {
    final static String newLine = "*"
}
class Child extends Parent{
    List body = [3, 4, 5]
    String toString() {
        def str = new StringBuilder()
        body.each { str.append(it + newLine) }
        str
    }
}

def c = new Child()
println c

The above is one trivial sample to illustrate the problem. It couldn't be compiled using Groovy plugin on Eclipse. Remove either final or static in the field of super class solves the problem. However, I have no idea why it's the case.

http://groovy.codehaus.org/Groovy+Beans In this link it mentions the rules for property and fields used in Groovy. I suppose the one applied should be the last one, i.e. meta class. Unfortunately, I still couldn't understand the behavior.

The behavior is reproduced consistently in all versions of Groovy. Maybe someone could report one bug to Groovy team. I have never done this before. It would be more efficient if someone experienced could do that.

like image 577
Albert Netymk Avatar asked Oct 22 '22 18:10

Albert Netymk


1 Answers

This is most probably https://issues.apache.org/jira/browse/GROOVY-5776 which is more difficult to fix than it looks like

like image 176
blackdrag Avatar answered Oct 24 '22 10:10

blackdrag