Created
March 2, 2015 19:12
-
-
Save tanob/11b040f377e0527321bd to your computer and use it in GitHub Desktop.
A problem with Gradle's Instantiator and closures referencing class' private fields
This file contains hidden or 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
import org.gradle.api.Project | |
class CommandRunner { | |
private final Project project | |
def CommandRunner(Project project) { | |
this.project = project | |
} | |
public String run(Object... args) { | |
def result = new ByteArrayOutputStream().withStream { os -> | |
project.exec { | |
commandLine args | |
standardOutput = os | |
} | |
os.toString() | |
} | |
result | |
} | |
} |
This file contains hidden or 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
import org.gradle.api.internal.project.ProjectInternal | |
import org.gradle.internal.reflect.Instantiator | |
import org.gradle.testfixtures.ProjectBuilder | |
import org.junit.Test | |
public class CommandRunnerTest { | |
@Test | |
public void testName() throws Exception { | |
def project = new ProjectBuilder().build() | |
Instantiator instantiator = ((ProjectInternal) project).getServices().get(Instantiator) | |
def commandRunner = instantiator.newInstance(CommandRunner, project) | |
commandRunner.run("echo", "hello world") | |
} | |
} |
This file contains hidden or 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
groovy.lang.MissingPropertyException: Could not find property 'project' on CommandRunner_Decorated@7e32156a. | |
at org.gradle.api.internal.AbstractDynamicObject.propertyMissingException(AbstractDynamicObject.java:43) | |
at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:35) | |
at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeDynamicObject.java:94) | |
at CommandRunner_Decorated.getProperty(Unknown Source) | |
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:168) | |
at groovy.lang.Closure.getPropertyTryThese(Closure.java:321) | |
at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:315) | |
at groovy.lang.Closure.getProperty(Closure.java:304) | |
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:47) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231) | |
at CommandRunner$_run_closure1.doCall(CommandRunner.groovy:12) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) | |
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) | |
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:278) | |
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016) | |
at groovy.lang.Closure.call(Closure.java:423) | |
at groovy.lang.Closure.call(Closure.java:439) | |
at org.codehaus.groovy.runtime.IOGroovyMethods.withStream(IOGroovyMethods.java:1298) | |
at org.codehaus.groovy.runtime.dgm$731.invoke(Unknown Source) | |
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271) | |
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53) | |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) | |
at CommandRunner.run(CommandRunner.groovy:11) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:166) | |
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:68) | |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) | |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) | |
at CommandRunnerTest.testName(CommandRunnerTest.groovy:14) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment