I am using some objects from my runtime solution in my editor and I am seeing a weird classcastexception: java.lang.ClassCastException: class exchange.runtime.OrderBook cannot be cast to class exchang...
This is an auto-generated question from the MPS Slack community: I am using some objects from my runtime solution in my editor and I am seeing a weird classcastexception: java.lang.ClassCastException: class exchange.runtime.OrderBook cannot be cast to class exchange.runtime.OrderBook (exchange.runtime.OrderBook is in unnamed module of loader jetbrains.mps.classloading.ModuleClassLoader @e21bbe4; exchange.runtime.OrderBook is in unnamed module of loader jetbrains.mps.classloading.ModuleClassLoader @47f4b8b8). They are the same time but somehow I am getting them in different unnamed modules during class loading.
What causes such a situation and any ideas on how to solve it? My code is pretty simple and not sure what I could have possibly done to cause different unnamed modules of loaders.
9 comments
Sort by
Date
Votes
When you rebuild a module in MPS then a new ModuleClassLoader is created that loads the new version of your classes. What the error means is that there is still an old instance of the OrderBook class that was created before the module was rebuilt. This often happens when you register a listener somewhere in MPS and forget to unregister it. The strack trace of the exception will tell you where this old instance is stored im MPS.
Thanks Slack user:U6L4B9THU>. I am looking at the stack trace and not seeing a location. Is it supposed to be a location on disk? or somewhere else? Here is the stack trace I am looking at:
java.lang.ClassCastException: class exchange.runtime.OrderBook cannot be cast to class exchange.runtime.OrderBook (exchange.runtime.OrderBook is in unnamed module of loader jetbrains.mps.classloading.ModuleClassLoader @e21bbe4; exchange.runtime.OrderBook is in unnamed module of loader jetbrains.mps.classloading.ModuleClassLoader @71395d33)
at exchange.editor.OrderBookTest_EditorBuilder_a$expectedOrderBookSingleRoleHandler_v8xn2p_c1c0._StyleParameter_QueryFunction_v8xn2p_a0c1c0(OrderBookTest_EditorBuilder_a.java:351)
at exchange.editor.OrderBookTest_EditorBuilder_a$expectedOrderBookSingleRoleHandler_v8xn2p_c1c0.installCellInfo(OrderBookTest_EditorBuilder_a.java:320)
at exchange.editor.OrderBookTest_EditorBuilder_a$expectedOrderBookSingleRoleHandler_v8xn2p_c1c0.createChildCell(OrderBookTest_EditorBuilder_a.java:306)
at jetbrains.mps.lang.editor.cellProviders.SingleRoleCellProvider.createSingleCell(SingleRoleCellProvider.java:109)
at jetbrains.mps.lang.editor.cellProviders.SingleRoleCellProvider.createCell(SingleRoleCellProvider.java:74)
at exchange.editor.OrderBookTest_EditorBuilder_a.createRefNode_2(OrderBookTest_EditorBuilder_a.java:285)
at exchange.editor.OrderBookTest_EditorBuilder_a.createCollection_4(OrderBookTest_EditorBuilder_a.java:170)
at exchange.editor.OrderBookTest_EditorBuilder_a.createCollection_2(OrderBookTest_EditorBuilder_a.java:130)
at exchange.editor.OrderBookTest_EditorBuilder_a.createCollection_0(OrderBookTest_EditorBuilder_a.java:76)
at exchange.editor.OrderBookTest_EditorBuilder_a.createCell(OrderBookTest_EditorBuilder_a.java:66)
at exchange.editor.OrderBookTest_Editor.createEditorCell(OrderBookTest_Editor.java:12)
at jetbrains.mps.nodeEditor.cells.EditorCellFactoryImpl.createCell(EditorCellFactoryImpl.java:137)
In your `OrderBookTest_Editor` there is a cell for `expected` that contains a query for some style. Check where the code in that style gets the `OrderBook` instance from.
Hi Slack user:U6L4B9THU> Yes it is in a style. I grab it from a userObject that has been attached to the node by my interpreter so I can change the colors of the expected in my editor based on the interpreted results. Not sure why my interpreter would being using a different loader?
The user object that is attached here came from before the module was reloaded, but now it's being cast to the new incarnation of OrderBook class so it fails.
If the module containing the OrderBook class is rebuilt, it is reloaded and the old classes are incompatible with the newly loaded ones
You need to reevaluate the node so that a new result is attached. Also, that code should be modified to be more defensive, checking with instanceof before casting.
Ah ok. Thanks Slack user:U6K3FGW3U> and Slack user:U6L4B9THU>
This is auto-generated question from the MPS Community Slack Workspace. If you want to comment on the question, do it from the Slack workspace
Post is closed for comments.