Skip to content

Commit

Permalink
Make the AST transformation more robust... (#1704)
Browse files Browse the repository at this point in the history
against missing classes in the compile classpath
  • Loading branch information
Vampire authored Aug 21, 2023
1 parent 62b11bb commit 2fff484
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,17 @@ private static boolean checkIsConditionBlock(MethodCallExpression methodCallExpr
ClassNode targetType = methodCallExpr.getObjectExpression().getType();
String methodName = methodCallExpr.getMethodAsString();

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

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.spockframework.smoke

import org.codehaus.groovy.ast.ClassNode
import org.jetbrains.annotations.Debug
import org.spockframework.EmbeddedSpecification
import org.spockframework.compiler.InvalidSpecCompileException

Expand Down Expand Up @@ -80,4 +82,36 @@ println "hi"
then:
thrown(InvalidSpecCompileException)
}

static class Foo {
Debug getDebug() { return null }
void bar() {}
}

def "compiling with missing dependencies does not fail"() {
given:
compiler.addClassImport('org.spockframework.smoke.CompileTimeErrorReporting.Foo')

when:
new ClassNode(Class.forName('org.spockframework.smoke.CompileTimeErrorReporting$Foo'))
.getMethods('bar')

then: 'make sure it would fail for the right reason, this might change if `Debug` makes it into the runtime classpath'
NoClassDefFoundError ncdfe = thrown()
println(ncdfe.message)
ncdfe.message.contains('org.spockframework.smoke.CompileTimeErrorReporting$Foo')
ncdfe.message.contains('org/jetbrains/annotations/Debug')

when:
compiler.compileFeatureBody """
when:
new Foo().bar()
then:
noExceptionThrown()
"""

then:
noExceptionThrown()
}
}

0 comments on commit 2fff484

Please sign in to comment.