Skip to content

Commit

Permalink
[JENKINS-26481] Limit the error to cases where the binary method actu…
Browse files Browse the repository at this point in the history
…ally requested a Closure, and is thus likely to try to call it.
  • Loading branch information
jglick committed Jun 6, 2016
1 parent efeee2b commit 6ad4d04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.workflow.cps;

import com.cloudbees.groovy.cps.impl.CpsClosure;
import groovy.lang.Closure;
import groovy.lang.GroovyClassLoader;
import org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist;

Expand Down Expand Up @@ -80,8 +81,9 @@ private boolean checkJenkins26481(Object[] args, Executable method) throws Unsup
if (permits(method.getDeclaringClass())) { // fine for source-defined methods to take closures
return true;
}
for (Object arg : args) {
if (arg instanceof CpsClosure) {
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof CpsClosure && parameterTypes.length > i && parameterTypes[i] == Closure.class) {
throw new UnsupportedOperationException("Calling " + method + " on a CPS-transformed closure is not yet supported (JENKINS-26481); encapsulate in a @NonCPS method, or use Java-style loops");
}
}
Expand Down
Expand Up @@ -237,6 +237,7 @@ public void stop(Throwable cause) throws Exception {
@Test public void eachClosureNonCps() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
ScriptApproval.get().approveSignature("staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods plus java.util.Collection java.lang.Object");
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(
"@NonCPS def fine() {\n" +
Expand All @@ -246,7 +247,9 @@ public void stop(Throwable cause) throws Exception {
"}\n" +
"def takesMyOwnClosure(body) {\n" +
" node {\n" +
" echo body()\n" +
" def list = []\n" +
" list += body\n" +
" echo list[0]()\n" +
" }\n" +
"}\n" +
"takesMyOwnClosure {\n" +
Expand Down

0 comments on commit 6ad4d04

Please sign in to comment.