You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to JLS 5.5 (going at least as far back as Java 7), we should be able to cast an Object to unboxed primitives directly.
Casting contexts allow the use of one of:
.
.
.
a narrowing reference conversion (§5.1.6) optionally followed by either an unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)
However, Janino does not allow this while Javac does. E.g
this does not compile in Janino
java -cp janino.jar:commons-compiler.jar org.codehaus.commons.compiler.samples.ScriptDemo 'Object value = 1L; long l = (long) value;'
Exception in thread "main" org.codehaus.commons.compiler.CompileException: Line 1, Column 34: Cannot cast "java.lang.Object" to "long"
at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:13228)
at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:5311)
at org.codehaus.janino.UnitCompiler.access$8800(UnitCompiler.java:240)
at org.codehaus.janino.UnitCompiler$16.visitCast(UnitCompiler.java:4819)
at org.codehaus.janino.UnitCompiler$16.visitCast(UnitCompiler.java:4800)
at org.codehaus.janino.Java$Cast.accept(Java.java:5283)
while performing reference conversion followed by unboxing in two steps works
java -cp janino.jar:commons-compiler.jar org.codehaus.commons.compiler.samples.ScriptDemo 'Object value = 1L; long l = (long) ((Long) value);'
According to JLS 5.5 (going at least as far back as Java 7), we should be able to cast an Object to unboxed primitives directly.
However, Janino does not allow this while Javac does. E.g
this does not compile in Janino
while performing reference conversion followed by unboxing in two steps works