Janino

Combination of interface declaration with method that declares a thrown exception cannot be parsed

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    Janino 2.4.3
  • Number of attachments :
    0

Description

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; }

}

Activity

Hide
Lars added a comment -

Sorry I think this is not a bug, as the exception is not handled in the script method. I was confused because of the strange error message. Maybe is should be "unhandled exception" or sth. similar.

Show
Lars added a comment - Sorry I think this is not a bug, as the exception is not handled in the script method. I was confused because of the strange error message. Maybe is should be "unhandled exception" or sth. similar.
Hide
Arno Unkrig added a comment -

Hello Mr. Lars,

the problem seems to be that IPred.filter() is declared with "throws Exception" clause, while get0() neither declares "throws Exception" nor encloses it the method invocation in a "try { ... } catch (Exception ex) ..." statement. Actually a compilation error

Exception "Exception" is neither declared in a "throws" clause nor is it caught in a TRY...CATCH statement

should be raised rather than throwing that embarassing RuntimeException.

I will look into this... I'm on vacation right now and cannot do anything from italy

Stay tuned!

CU

Arno

Show
Arno Unkrig added a comment - Hello Mr. Lars, the problem seems to be that IPred.filter() is declared with "throws Exception" clause, while get0() neither declares "throws Exception" nor encloses it the method invocation in a "try { ... } catch (Exception ex) ..." statement. Actually a compilation error
Exception "Exception" is neither declared in a "throws" clause nor is it caught in a TRY...CATCH statement
should be raised rather than throwing that embarassing RuntimeException. I will look into this... I'm on vacation right now and cannot do anything from italy Stay tuned! CU Arno
Hide
Lars added a comment -

Hi Arno,

yes I absolutely agree with your interpretation

Have a nice holiday
Lars

Show
Lars added a comment - Hi Arno, yes I absolutely agree with your interpretation Have a nice holiday Lars
Hide
Arno Unkrig added a comment -

Back home... looked into it.

Fixed in 2.4.4, now it issues a nice error like

Thrown exception of type "java.lang.Exception" is neither caught by a "try...catch" block nor declared in the "throws" clause of the declaring function

Show
Arno Unkrig added a comment - Back home... looked into it. Fixed in 2.4.4, now it issues a nice error like
Thrown exception of type "java.lang.Exception" is neither caught by a "try...catch" block nor declared in the "throws" clause of the declaring function

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: