I tried to compile the attached Foo.java (which
compiles fine with Sun's javac) with janinoc. Got back
the message "null". When called from my own code with
some similar generated code, here's the relevant
portion of the corresponding stack trace:
java.lang.ClassCastException
at net.janino.Java.invokeConstructor(Unknown Source)
at net.janino.Java.access$6400(Unknown Source)
at net.janino.Java$NewClassInstance.compileGet(Unknown
Source)
at net.janino.Java$Rvalue.compileGetValue(Unknown Source)
at net.janino.Java$FieldDeclarator.compile(Unknown Source)
at
net.janino.Java$ConstructorDeclarator.initializeInstanceVariablesAndInvokeInstanceInitializers(Unknown
Source)
at
net.janino.Java$ConstructorDeclarator.compilePreamble(Unknown
Source)
at net.janino.Java$FunctionDeclarator.compile(Unknown
Source)
at net.janino.Java$ClassDeclaration.compile(Unknown
Source)
at
net.janino.Java$AnonymousClassDeclaration.compile(Unknown
Source)
at
net.janino.Java$NewAnonymousClassInstance.compileGet(Unknown
Source)
at net.janino.Java$Rvalue.compileGetValue(Unknown Source)
code to reproduce bug in isolation
Logged In: YES
user_id=173374
note: janino version 2.0.3; Sun java version 1.4.2_03-b02
Logged In: YES
user_id=173374
Here's a patch which fixes this bug:
In src/net/janino/Java.java, change line 6415 from
for (; s instanceof Statement; s =
s.getEnclosingScope());
to
for (; !(s instanceof TypeBodyDeclaration);
s = s.getEnclosingScope());
This fixed my problem, but I haven't studied the code long
enough to know whether it's a proper fix.
Logged In: YES
user_id=865893
The fix you proposed is perfect! Thank you.
Will be released in version 2.0.4.