Skip to content
This repository was archived by the owner on Jul 4, 2026. It is now read-only.
This repository was archived by the owner on Jul 4, 2026. It is now read-only.

Janino does not allow casting Object to unboxed primitives #223

Description

@shardulm94

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);'

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