The following code shows two testcases that only differ in the declaration of the variable p, which is in one case an interface and in the other case an object. The problem is that the interface code cannot be parsed and throws the following exception:
java.lang.RuntimeException: A compilation unit has no enclosing scope
at org.codehaus.janino.Java$CompilationUnit.getEnclosingScope(Unknown Source)
at org.codehaus.janino.UnitCompiler.checkThrownException(Unknown Source)
at org.codehaus.janino.UnitCompiler.findIMethod(Unknown Source)
at org.codehaus.janino.UnitCompiler.compileGet2(Unknown Source)
at org.codehaus.janino.UnitCompiler.access$46(Unknown Source)
at org.codehaus.janino.UnitCompiler$7.visitMethodInvocation(Unknown Source)
at org.codehaus.janino.Java$MethodInvocation.accept(Unknown Source)
at org.codehaus.janino.UnitCompiler.compileGet(Unknown Source)
at org.codehaus.janino.UnitCompiler.compileGetValue(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile2(Unknown Source)
at org.codehaus.janino.UnitCompiler.access$13(Unknown Source)
at org.codehaus.janino.UnitCompiler$2.visitReturnStatement(Unknown Source)
at org.codehaus.janino.Java$ReturnStatement.accept(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile2(Unknown Source)
at org.codehaus.janino.UnitCompiler.access$3(Unknown Source)
at org.codehaus.janino.UnitCompiler$2.visitBlock(Unknown Source)
at org.codehaus.janino.Java$Block.accept(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile2(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile2(Unknown Source)
at org.codehaus.janino.UnitCompiler$1.visitPackageMemberClassDeclaration(Unknown Source)
at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Unknown Source)
at org.codehaus.janino.UnitCompiler.compile(Unknown Source)
at org.codehaus.janino.UnitCompiler.compileUnit(Unknown Source)
at jadex.parser.janinoimpl.EvaluatorBaseCopy.compileAndLoad(EvaluatorBaseCopy.java:359)
at jadex.parser.janinoimpl.EvaluatorBaseCopy.compileAndLoad(EvaluatorBaseCopy.java:390)
at jadex.parser.janinoimpl.ClassBodyEvaluatorCopy.<init>(ClassBodyEvaluatorCopy.java:125)
at jadex.parser.janinoimpl.ClassBodyEvaluatorCopy.<init>(ClassBodyEvaluatorCopy.java:69)
at jadex.parser.janinoimpl.ClassBodyEvaluatorCopy.<init>(ClassBodyEvaluatorCopy.java:54)
at jadex.parser.janinoimpl.ClassBodyEvaluatorCopy.<init>(ClassBodyEvaluatorCopy.java:21)
at Tester.main(Tester.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
import jadex.parser.janinoimpl.ClassBodyEvaluatorCopy;
public class Tester
{
public static void main(String[] args)
{
try
{
String script = "public static boolean getO() {\n" +
"IPred p = new Pred();\n"+
"return p.filter();}\n";
ClassBodyEvaluatorCopy c = new ClassBodyEvaluatorCopy(script);
System.out.println("Result1: "+c.evaluate());
}
catch(Throwable t)
{
t.printStackTrace();
}
try
{
String script = "public static boolean getO() {\n" +
"Pred p = new Pred();\n"+
"return p.filter();}\n";
ClassBodyEvaluatorCopy c = new ClassBodyEvaluatorCopy(script);
System.out.println("Result2: "+c.evaluate());
}
catch(Throwable t)
{ t.printStackTrace(); } }
}
}
interface IPred
{
public boolean filter() throws Exception;
}
class Pred implements IPred
{
public boolean filter()
{
return false;
}
}