[GRADLE-2384] MarkupBuilder resolves labels as properties Created: 13/Jul/12  Updated: 04/Jan/13  Resolved: 14/Jul/12

Status: Resolved
Project: Gradle
Affects Version/s: 1.0
Fix Version/s: None

Type: Bug
Reporter: Björn Kautler Assignee: Unassigned
Resolution: Not A Bug Votes: 0


 Description   

When executing the task

task test << {
    file('test').withWriter {
        new groovy.xml.MarkupBuilder(it).root {
            properties()
        }
    }
}

with Gradle 1.0, I get the message

The AbstractTask.getDynamicObjectHelper() method has been deprecated and will be removed in the next version of Gradle. Please use the getAsDynamicObject() method instead.

This happens because when the "properties" is handled, the properties of the "test" task are retrieved. During this, getValue() for all the properties is called which gives the message for "dynamicObjectHelper" property.
Nevertheless the resulting markup correctly has the "properties" tag like expected.
But the point is, why are the properties retrieved at all? It should not be a retrieval of the properties but a label. I also tried to use curly braces instead of parentheses and to enclose "properties" in single quotes, double quotes and slashes which should also make it be used as label and not property reference, but it did not change behaviour. I was not able to make it just use "properties" as label and not resolve it as reference.
Even declaring a variable "def propString = 'properties'" and then using '"$propString"' in the MarkupBuilder gives the same message.



 Comments   
Comment by Adam Murdoch [ 14/Jul/12 ]

This is a side effect of how Groovy works:

1. When Groovy evaluates 'properties()', it first looks for a method 'properties()' on the closure containing the call. It does not find one.
2. It then looks for a property called 'properties' on the closure. By default, the closure will look for the property on its owner first. In this case, this will be the project.
3. If the property value is Runnable, or a Closure, Groovy will then call the run()/call() method on the property value. In this case, the value is a Map, so Groovy continues on to the next step.
4. It then invokes missingMethod() on the Closure, which in turn calls through to MarkupBuilder to add the 'properties' element to the output.

So, the problem is happening in step 2. There are a few options you have:

1. Use 'delegate.properties()' in the closure
2. Set resolveStrategy = Closure.DELEGATE_FIRST in the closure
3. Move the code to a static method

Comment by Björn Kautler [ 16/Jul/12 ]

Ah, I see. I used the first solution and now the warning is gone, thanks.

Generated at Wed Jun 30 12:20:27 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.