Skip to content

Commit

Permalink
Issue janino-compiler#116: replace operand to final target type if bo…
Browse files Browse the repository at this point in the history
…xing conversion & widening reference conversion happens together
  • Loading branch information
HeartSaVioR committed Mar 13, 2020
1 parent 3fec0cb commit 991e454
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions janino/src/main/java/org/codehaus/janino/UnitCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9850,8 +9850,8 @@ interface Compilable { void compile() throws CompileException; }
getDeclaredIConstructors2() {
if (atd instanceof AbstractClassDeclaration) {
AbstractClassDeclaration acd = (AbstractClassDeclaration) atd;

ConstructorDeclarator[] cs = acd.getConstructors();
ConstructorDeclarator[] cs = acd.getConstructors();
IClass.IConstructor[] result = new IClass.IConstructor[cs.length];
for (int i = 0; i < cs.length; ++i) result[i] = UnitCompiler.this.toIConstructor(cs[i]);
return result;
Expand Down Expand Up @@ -10875,6 +10875,8 @@ interface Compilable { void compile() throws CompileException; }
}
if (this.isWideningReferenceConvertible(boxedType, targetType)) {
this.boxingConversion(locatable, sourceType, boxedType);
this.getCodeContext().popOperand(boxedType.getDescriptor());
this.getCodeContext().pushOperand(targetType.getDescriptor());
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.IClassBodyEvaluator;
import org.codehaus.commons.compiler.IScriptEvaluator;
import org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader;
import org.codehaus.commons.compiler.util.resource.MapResourceCreator;
import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
Expand All @@ -53,6 +54,7 @@
import org.codehaus.janino.Java.Rvalue;
import org.codehaus.janino.Parser;
import org.codehaus.janino.Scanner;
import org.codehaus.janino.ScriptEvaluator;
import org.codehaus.janino.SimpleCompiler;
import org.codehaus.janino.UnitCompiler;
import org.codehaus.janino.util.ClassFile;
Expand Down Expand Up @@ -343,6 +345,46 @@ class GithubIssuesTest {
return sb.toString();
}

@Test public void
testIssue116() throws Exception {
IScriptEvaluator eval = new ScriptEvaluator();
eval.setReturnType(Object[].class);
eval.cook(
""
+ "class A {\n"
+ " private Integer val;\n"
+ " public A(Integer v) {\n"
+ " val = v;\n"
+ " }\n"
+ " public boolean isNull() {\n"
+ " return val == null;\n"
+ " }\n"
+ " public int getInt() {\n"
+ " return val;\n"
+ " }\n"
+ "}\n"
+ "A a = new A(3);\n"
+ "Object[] c = new Object[]{\n"
// auto boxing & casting in LHS
+ "!a.isNull() ? (Object) a.getInt() : null,\n"
// auto boxing & no explicit casting in LHS
+ "!a.isNull() ? a.getInt() : null,\n"
// auto boxing & casting in RHS
+ "a.isNull() ? null : (Object) a.getInt(),\n"
// auto boxing & no explicit casting in RHS
+ "a.isNull() ? null : a.getInt(),\n"
// simple casting
+ "(Object) \"hello\"};\n"
+ "return c;"
);
Object[] ret = (Object[]) eval.evaluate(new Object[]{});
Assert.assertEquals(3, ret[0]);
Assert.assertEquals(3, ret[1]);
Assert.assertEquals(3, ret[2]);
Assert.assertEquals(3, ret[3]);
Assert.assertEquals("hello", ret[4]);
}

public ClassLoader
compile(ClassLoader parentClassLoader, CompileUnit... compileUnits) {

Expand Down

0 comments on commit 991e454

Please sign in to comment.