-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ClassCastException: class cannot be cast to class scala.runtime.Nothing$ #22204
Comments
Scala CLI repro: //> using dep com.fasterxml.jackson.module::jackson-module-scala:2.15.4
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.{
JsonNodeFactory,
ObjectNode,
TextNode
}
val json: ObjectNode = JsonNodeFactory.instance.objectNode()
def foo(a: String): Unit = {
a match {
case "a" =>
json.set("type", TextNode.valueOf("value"))
case _ =>
json.set("type", TextNode.valueOf("value"))
}
}
@main def main = foo("") Fails at runtime with: Exception in thread "main" java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.ObjectNode cannot be cast to class scala.runtime.Nothing$ (com.fasterxml.jackson.databind.node.ObjectNode and scala.runtime.Nothing$ are in unnamed module of loader 'app')
at repro$package$.foo(repro.scala:16)
at repro$package$.main(repro.scala:20)
at main.main(repro.scala:20) Optimally, we'd like to minimize without the |
cc @sjrd |
seems like the return type is convert to nothing instead insert a () |
a quic fix : import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.{
JsonNodeFactory,
ObjectNode,
TextNode
}
val json: ObjectNode = JsonNodeFactory.instance.objectNode()
def foo(a: String): Unit = {
a match {
case "a" =>
json.set("type", TextNode.valueOf("value"))
case _ =>
json.set("type", TextNode.valueOf("value"))
()
}
}
foo("") This is a bug I think even I insert the |
The signature of There's no language bug, here. Explicitly write the type parameter to something that |
That's seems true abstract class BaseJsonNode
extends JsonNode
abstract class ContainerNode<T extends ContainerNode<T>>
extends BaseJsonNode
class ObjectNode
extends ContainerNode<ObjectNode> and In public <T extends JsonNode> T set(String propertyName, JsonNode value)
{
if (value == null) {
value = nullNode();
}
_children.put(propertyName, value);
return (T) this;
} I think the code should be written to : public ObjectNode set(String propertyName, JsonNode value)
{
if (value == null) {
value = nullNode();
}
_children.put(propertyName, value);
return (T) this;
} @pjfanning cc |
The current implementation in Jackson will cause error anyway, eg: ArrayNode arrayNode = objectNode.set(..) which will throw ClassCustException too, but the compiler will not complain. |
If anyone is suggesting that it is Jackson that gets changed, I honestly don't think the Jackson team (which includes me) will be changing the Java code in Jackson to suit the Scala 3 compiler. Scala 3 users form a tiny minority of Jackson users and it's hard to justify discommoding other Jackson users by changing Jackson APIs. Jackson 3 might be a place where we can make a change but there is no date for a GA release of Jackson 3. |
Interestingly, Jackson 3 code has been changed so that ObjectNode.set and setAll returns ObjectNode explicitly. There are snapshots published. |
@jtjeferreira a messy workaround might be feasible. Maybe a Java class in your code base called something like ObjectNodeWrapper that wraps an ObjectNode and that adds set and setAll methods that delegate to the ObjectNode methods. The ObjectNodeWrapper methods would return ObjectNode explicitly. |
The correct workaround is much simpler. I mentioned it above: call |
yes this workaround works and thats what I am using. I just wanted this to work as in scala2... If that not feasible, feel free to close the issue. |
Compiler version
Scala 3.6.2, 3.5.2, 3.3.4
Minimized code
method
set
is a java method:https://scastie.scala-lang.org/i507EyzXTdO1BM362YaOhA
Output
Expectation
That it does not crash like in scala2 https://scastie.scala-lang.org/gvqImCf7QeKBXahfClCZHA
The text was updated successfully, but these errors were encountered: