[
https://issues.apache.org/jira/browse/GROOVY-4856?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17141680#comment-17141680
]
Eric Milles commented on GROOVY-4856:
-------------------------------------
AsmClassGenerator checks for "this", "super" or defined variables before
anything else.
{code:java}
public void visitVariableExpression(final VariableExpression expression) {
final String variableName = expression.getName();
if (expression.isThisExpression()) {
...
}
if (expression.isSuperExpression()) {
...
}
BytecodeVariable variable =
controller.getCompileStack().getVariable(variableName, false);
if (variable != null) {
controller.getOperandStack().loadOrStoreVariable(variable,
expression.isUseReferenceDirectly());
} else ...
{code}
I'm not sure if it is intended behavior to have local or script variables take
precedence over delegate or owner properties. However, you can add a
"delegate." qualifier to disambiguate.
> Closure resolveStrategy does not work the same way for properties and methods
> -----------------------------------------------------------------------------
>
> Key: GROOVY-4856
> URL: https://issues.apache.org/jira/browse/GROOVY-4856
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 1.7.6
> Environment: Mac OS 10.6.7, Groovy installed via Ports
> Reporter: Guven Demir
> Priority: Major
>
> When the resolveStrategy is set to DELEGATE_FIRST and the delegate is set
> property, a method with the same name as a global function is preferred
> instead of the global function. The same is not true for properties.
> The following code fragment demonstrates this issue:
> -----
> {noformat}
> class ClosureTest
> {
> def someProperty = "<unset>"
> def anotherProperty = "<unset>"
> public void test(Closure closure)
> {
> closure.delegate = this
> closure.resolveStrategy = Closure.DELEGATE_FIRST
> closure()
> }
>
> public void someMethod()
> {
> println "ClosureTest.someMethod()"
> }
> public void anotherMethod()
> {
> println "ClosureTest.anotherMethod()"
> }
>
> public String toString()
> {
> return "ClosureTest: sp: " + someProperty + ", ap: " + anotherProperty
> }
> }
> def someProperty = "global someProperty"
> public void someMethod()
> {
> println "global someMethod()"
> }
> def ct = new ClosureTest()
> ct.test() {
> someMethod()
> anotherMethod()
> someProperty = "foo"
> anotherProperty = "bar"
> }
> println "ct.someProperty : ${ct.someProperty}"
> println "ct.anotherProperty : ${ct.anotherProperty}"
> println "global: someProperty: " + someProperty
> {noformat}
> -----
> And the output of this script run is:
> {noformat}
> ClosureTest.someMethod()
> ClosureTest.anotherMethod()
> ct.someProperty : <unset>
> ct.anotherProperty : bar
> global: someProperty: foo
> {noformat}
> -----
> As seen above, the methods are called correctly, but the properties are not
> set correctly.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)