Skip to content

Commit 2fff484

Browse files
authored
Make the AST transformation more robust... (#1704)
against missing classes in the compile classpath
1 parent 62b11bb commit 2fff484

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,17 @@ private static boolean checkIsConditionBlock(MethodCallExpression methodCallExpr
266266
ClassNode targetType = methodCallExpr.getObjectExpression().getType();
267267
String methodName = methodCallExpr.getMethodAsString();
268268

269-
List<MethodNode> methods = targetType.getMethods(methodName);
270-
for (MethodNode method : methods) {
271-
for (AnnotationNode annotation : method.getAnnotations()) {
272-
if (annotation.getClassNode().getName().equals(ConditionBlock.class.getName())) return true;
269+
try {
270+
// if targetType has any method with a parameter or return type that is not in the
271+
// compile classpath this call will fail with a NoClassDefFoundError
272+
List<MethodNode> methods = targetType.getMethods(methodName);
273+
for (MethodNode method : methods) {
274+
for (AnnotationNode annotation : method.getAnnotations()) {
275+
if (annotation.getClassNode().getName().equals(ConditionBlock.class.getName())) return true;
276+
}
273277
}
278+
} catch (NoClassDefFoundError e) {
279+
// just assume there is no condition block and return false
274280
}
275281

276282
return false;

spock-specs/src/test/groovy/org/spockframework/smoke/CompileTimeErrorReporting.groovy

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package org.spockframework.smoke
1616

17+
import org.codehaus.groovy.ast.ClassNode
18+
import org.jetbrains.annotations.Debug
1719
import org.spockframework.EmbeddedSpecification
1820
import org.spockframework.compiler.InvalidSpecCompileException
1921

@@ -80,4 +82,36 @@ println "hi"
8082
then:
8183
thrown(InvalidSpecCompileException)
8284
}
85+
86+
static class Foo {
87+
Debug getDebug() { return null }
88+
void bar() {}
89+
}
90+
91+
def "compiling with missing dependencies does not fail"() {
92+
given:
93+
compiler.addClassImport('org.spockframework.smoke.CompileTimeErrorReporting.Foo')
94+
95+
when:
96+
new ClassNode(Class.forName('org.spockframework.smoke.CompileTimeErrorReporting$Foo'))
97+
.getMethods('bar')
98+
99+
then: 'make sure it would fail for the right reason, this might change if `Debug` makes it into the runtime classpath'
100+
NoClassDefFoundError ncdfe = thrown()
101+
println(ncdfe.message)
102+
ncdfe.message.contains('org.spockframework.smoke.CompileTimeErrorReporting$Foo')
103+
ncdfe.message.contains('org/jetbrains/annotations/Debug')
104+
105+
when:
106+
compiler.compileFeatureBody """
107+
when:
108+
new Foo().bar()
109+
110+
then:
111+
noExceptionThrown()
112+
"""
113+
114+
then:
115+
noExceptionThrown()
116+
}
83117
}

0 commit comments

Comments
 (0)