Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What else can throw a ClassCastException in java?

This is an interview question.

The interview is over, but this question is still on my mind.

I can't ask the interviewer, as I did not get the job.

Scenario:

  • put object of class C1 in to a cache with key "a"

Later code:

C1 c1FromCache = (C1) cache.get("a");

This code throws a ClassCastException.

What can the reasons be?

I said because someone else put another object with the same key and so overwrote it. I was told no, think of other possibilities.

I said maybe the jar defining class C1 was not available on this node (not sure if this would result in a class cast or a ClassNotFoundException, but I was grasping for any lead now. Then I said maybe wrong version of class? They said the same jar of class C1 is there in all nodes).

Edit/ Add Asked if the get was throwing the ClassCast but was told no. after that i told him my action to resolve such an issue would be to drop in a test jsp that would mimic the actions and put better logging (stack trace) after the exception. that was the 2nd part of the question (why and what would you do if this happened in production)

Does anyone else have any ideas about why a cache get would result in a cast issue?

like image 349
tgkprog Avatar asked Apr 18 '13 20:04

tgkprog


People also ask

Which of the following method throws ClassCastException?

A class cast exception is thrown by Java when you try to cast an Object of one data type to another.

How do you handle ClassCastException in Java?

How to handle ClassCastException. To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What causes ClassCastException in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What might a ClassCastException be?

ClassCastException is an unchecked exception that signals the code has attempted to cast a reference to a type of which it's not a subtype.


1 Answers

One reason could be that the part of the code inserting the object uses a different classloader than the code retrieving it.
An instance of a class can not be cast to the same class loaded by a different classloader.

Response to the edit:

What would you do if this happened in production?

This generally happens when the reading and inserting modules each include the same jar containing C1.
Since most containers try the parent classloader first, and then the local classloader (the Parent first strategy), the common solution to the problem is to instead load the class in the closest common parent to the inserting and reading modules.
If you move the module containing the C1 class to the parent module, you force both submodules to get the class from the parent, removing any classloader differences.

like image 181
Keppil Avatar answered Oct 02 '22 02:10

Keppil