Skip to content

Inconsistent operand stack - ternary operator which LHS and/or RHS has auto boxing & cast #116

@HeartSaVioR

Description

@HeartSaVioR

This is one of test failures I've seen in Spark with Janino 3.1.1.

Please add below tests into ExpressionEvaluatorTest and run to see the failures:

    @Test public void
    testTernaryWithAutoBoxingAndCastingInLHSAndRHS() throws Exception {
        new ScriptEvaluator().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[] b = new Object[]{\n"
                // auto boxing & casting in LHS
                + "a.isNull() ? (Object) a.getInt(): null,\n"
                // auto boxing & casting in RHS
                + "!a.isNull() ? null: (Object) a.getInt(),\n"
                // simple casting
                + "(Object) \"hello\"};\n"
        );
    }

I checked with two different UTs ad it failed on both LHS and RHS - I've flatten these tests into one.

It seems that the expected operand is Object but (Object) a.getInt() adds Integer.valueOf() (which is correct and Javac also does that) which pushes Integer as the operand.

Below code works with javac -source 1.6 -target 1.6:

class A {
    private Integer val;
    public A(Integer v) {
         val = v;
    }
    public boolean isNull() {
        return val == null;
    }
    public int getInt() {
        return val;
    }
}

class M {
	public static void main(String[] args) {
		A a = new A(3);
                A b = new A(null);
		Object[] c = new Object[]{ a.isNull() ? null : (Object) a.getInt(), !b.isNull() ? (Object) b.getInt() : null, "hello" };
                System.out.println(c[0]);
                System.out.println(c[1]);
                System.out.println(c[2]);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions