Inside Class Loaders
Subject:   Casting classes loaded from different effective classloaders
Date:   2003-11-17 10:56:57
From:   sasho
In normal circumstances the ClassCastException thrown when casting classes with the same type but with different effective classloaders is the desired behavior but in some cases we may need the cast to succeed. Just like in the examples in the article, we may need to invoke a method on a class loaded with a classloader different from the effective classloader of the instance we pass as an argument (in this case the class names will match but the classloaders will not). One possible workaround, if the class is serializable, is to serialize the instance and then deserialize it within a class loaded by the second classloader (thus creating an instance that has the correct type and the correct effective classloader) and pass the new instance as the argument. Although this works just fine, it's not really a very elegant answer to that problem and maybe you could suggest a better solution?

1 to 2 of 2
1 to 2 of 2