Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.3.4
-
None
Description
When implementing interface method that returns Runnable, expecting compiler to be able to cast Closure to Runnable, but getting a compilation class cast exception. Works fine when passing the same Closure to a method accepting Runnable.
import groovy.transform.* @CompileStatic interface Job { Runnable getRunnable() } @CompileStatic class Printer implements Job{ protected void execute() { println "Printing" } public void acceptsRunnable(Runnable r){ r.run() } //@CompileStatic(TypeCheckingMode.SKIP) public Runnable getRunnable(){ acceptsRunnable(this.&execute) // OK return this.&execute // compile error } } Printer p = new Printer()
Reproducible in any groovy console.
Compilation Error:
ava.lang.ClassCastException: org.codehaus.groovy.ast.expr.VariableExpression cannot be cast to org.codehaus.groovy.ast.expr.ClosureExpression
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.getResultType(StaticTypeCheckingVisitor.java:3108)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:448)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitDeclarationExpression(CodeVisitorSupport.java:245)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitDeclarationExpression(ClassCodeVisitorSupport.java:107)
at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:86)