forked from gradle/gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
391 additions
and
89 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...ts/core/src/integTest/groovy/org/gradle/api/dsl/DynamicMethodLookupIntegrationTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gradle.api.dsl | ||
|
||
import org.gradle.integtests.fixtures.AbstractIntegrationSpec | ||
import spock.lang.Issue | ||
|
||
class DynamicMethodLookupIntegrationTest extends AbstractIntegrationSpec { | ||
@Issue("GRADLE-3460") | ||
def "extension configuration method is preferred over property with closure value"() { | ||
given: | ||
buildFile << """ | ||
class ContactExtension { | ||
String prop | ||
} | ||
class ContactPlugin implements Plugin<Project> { | ||
public void apply(Project project) { | ||
ContactExtension extension = project.extensions.create('contacts', ContactExtension) | ||
project.ext.contacts = { String... args -> | ||
return args | ||
} | ||
} | ||
} | ||
apply plugin: ContactPlugin | ||
contacts { | ||
assert delegate instanceof ContactExtension | ||
prop = "value" | ||
} | ||
assert extensions.contacts.prop == "value" | ||
assert contacts() == [] | ||
assert contacts("a") == [ "a" ] | ||
assert contacts("a", "b", "c") == [ "a", "b", "c" ] | ||
""" | ||
expect: | ||
succeeds() | ||
} | ||
|
||
def "extension configuration method is preferred over property with untyped closure value"() { | ||
given: | ||
buildFile << """ | ||
class ContactExtension { | ||
String prop | ||
} | ||
extensions.create('contacts', ContactExtension) | ||
ext.contacts = { args -> | ||
return args | ||
} | ||
contacts { | ||
assert delegate instanceof ContactExtension | ||
prop = "value" | ||
} | ||
assert extensions.contacts.prop == "value" | ||
assert contacts() == null | ||
assert contacts("a") == "a" | ||
""" | ||
|
||
expect: | ||
succeeds() | ||
} | ||
|
||
// Documents actual behaviour for backwards compatibility, not necessarily desired behaviour | ||
def "inherited convention method is preferred over property with closure value"() { | ||
given: | ||
settingsFile << "include 'child'" | ||
buildFile << """ | ||
class ContactConvention { | ||
def contacts(String arg) { arg } | ||
} | ||
convention.plugins.contacts = new ContactConvention() | ||
subprojects { | ||
ext.contacts = { throw new RuntimeException() } | ||
assert contacts("a") == "a" | ||
} | ||
""" | ||
|
||
expect: | ||
succeeds() | ||
} | ||
|
||
def "property with closure value is preferred over inherited property with closure value"() { | ||
given: | ||
settingsFile << "include 'child'" | ||
buildFile << """ | ||
ext.contacts = { throw new RuntimeException() } | ||
subprojects { | ||
ext.contacts = { it } | ||
assert contacts("a") == "a" | ||
} | ||
""" | ||
|
||
expect: | ||
succeeds() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ain/java/org/gradle/internal/metaobject/MixInClosurePropertiesAsMethodsDynamicObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gradle.internal.metaobject; | ||
|
||
import groovy.lang.Closure; | ||
|
||
/** | ||
* Exposes methods for those properties whose value is a closure. | ||
* | ||
* TODO: use composition instead of inheritance | ||
*/ | ||
public abstract class MixInClosurePropertiesAsMethodsDynamicObject extends CompositeDynamicObject { | ||
@Override | ||
public void invokeMethod(String name, InvokeMethodResult result, Object... arguments) { | ||
super.invokeMethod(name, result, arguments); | ||
if (result.isFound()) { | ||
return; | ||
} | ||
|
||
GetPropertyResult propertyLookup = new GetPropertyResult(); | ||
getProperty(name, propertyLookup); | ||
if (propertyLookup.isFound()) { | ||
Object property = propertyLookup.getValue(); | ||
if (property instanceof Closure) { | ||
Closure closure = (Closure) property; | ||
closure.setResolveStrategy(Closure.DELEGATE_FIRST); | ||
new BeanDynamicObject(closure).invokeMethod("doCall", result, arguments); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.