Janino

ClassBodyEvaluator base class can be abstract and compile fine although it shouldn't compile

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
  • Number of attachments :
    0

Description

Running the command:
ClassBodyEvaluator.createFastClassBodyEvaluator(new Scanner(null, new StringReader(scriptText)), scriptName, scriptBaseClass, new Class[0], null)
does not throw an exception when scriptBaseClass is an abstract class. From what I can tell it just creates a derived abstract class. Trying to call the abstract method causes a java.lang.AbstractMethodError.

If this behavior is by design it would be nice to have an option to compile a non-abstract class (as this is what I need, I want it to fail in this case).

Example:

Base.java
public abstract class Base{
    public abstract run();
}
CompileTheClass.java
public class CompileTheClass{
    String scriptText = "public void m(){ System.out.println(\"Got here\"); }";

    public void compileAndRun(){
        Base obj = (Base)ClassBodyEvaluator.createFastClassBodyEvaluator(
                new Scanner(null, new StringReader(scriptText)), "MyScript", "Base", new Class[0], null);

        obj.run();  // Crash here
    }
}

Activity

Hide
Tim Steenwyk added a comment -

I take back what I said, it doesn't create a derived abstract class. Using reflection, the compiled object (obj in the example) does not appear to be abstract even though it doesn't contain the run() method. Also creating an instance of the class wouldn't be possible if it was abstract.

Show
Tim Steenwyk added a comment - I take back what I said, it doesn't create a derived abstract class. Using reflection, the compiled object (obj in the example) does not appear to be abstract even though it doesn't contain the run() method. Also creating an instance of the class wouldn't be possible if it was abstract.
Hide
Arno Unkrig added a comment -

The problem was that JANINO did not verify that each non-abstract class implements all abstract methods. Added that functionality; now you get

org.codehaus.janino.CompileException: Line 1, Column 1: Non-abstract class "SC" must implement method "void Base.run()"

If the fix is very urgent for you, I can create a new JANINO release. Please let me know.

CU

Arno

Show
Arno Unkrig added a comment - The problem was that JANINO did not verify that each non-abstract class implements all abstract methods. Added that functionality; now you get org.codehaus.janino.CompileException: Line 1, Column 1: Non-abstract class "SC" must implement method "void Base.run()" If the fix is very urgent for you, I can create a new JANINO release. Please let me know. CU Arno
Hide
Tim Steenwyk added a comment -

No, the fix isn't that urgent. I can wait until you release the next version.

Thanks

Show
Tim Steenwyk added a comment - No, the fix isn't that urgent. I can wait until you release the next version. Thanks
Hide
Arno Unkrig added a comment -

2.5.8 is right out the door... have fun!

Show
Arno Unkrig added a comment - 2.5.8 is right out the door... have fun!

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: