转换为同一个类时出现ClassCastException [英] ClassCastException when casting to the same class

查看:335
本文介绍了转换为同一个类时出现ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的Java项目,一个有2个类: dynamicbeans.DynamicBean2 dynamic.Validator

I have 2 different Java projects, one has 2 classes: dynamicbeans.DynamicBean2 and dynamic.Validator.

在另一个项目中,我动态加载这两个类并将它们存储在对象

On the other project, I load both of these classes dynamically and store them on an Object

class Form {
    Class beanClass;
    Class validatorClass;
    Validator validator;
}

然后我继续创建 Validator 对象使用 validatorClass.newInstance()并将其存储在验证器然后我创建一个bean对象作为好好使用 beanClass.newInstance()并将其添加到会话中。

I then go ahead and create a Validator object using validatorClass.newInstance() and store it on validator then I create a bean object as well using beanClass.newInstance() and add it to the session.

portletRequest.setAttribute("DynamicBean2", bean);

表单项目的生命周期中,我调用 validator.validate(),它从会话中加载以前创建的bean对象(我​​正在运行Websphere Portal Server)。当我尝试将此对象强制转换为 DynamicBean2 时,它会因ClassCastException而失败。

During the lifecycle of the Form project, I call validator.validate() which loads the previously created bean object from the session (I'm running Websphere Portal Server). When I try to cast this object back into a DynamicBean2 it fails with a ClassCastException.

当我拉动对象时退出会话使用

When I pull the object back out of the session using

faces.getApplication().createValueBinding("#{DynamicBean2}").getValue(faces);

并使用 .getClass()我得到 dynamicbeans.DynamicBean2 。这是我想把它投射到的类,但是当我尝试我得到ClassCastException时。

and check the class of it using .getClass() I get dynamicbeans.DynamicBean2. This is the class I want to cast it to however when I try I get the ClassCastException.

为什么我得到这个?

推荐答案

我不太关注你对程序流的描述,但通常当你得到ClassCastExceptions时你无法解释你已经用一个类加载器加载了类然后尝试将它转换为另一个类加载器加载的同一个类。这不起作用 - 它们由JVM中的两个不同的Class对象表示,并且强制转换将失败。

I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.

有一个关于WebSphere中的类加载的文章。我不能说它如何适用于您的应用程序,但有许多可能的解决方案。我至少可以想到:

There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:


  1. 手动更改上下文类加载器。要求您实际上可以获得对适当的类加载器的引用,这在您的情况下可能是不可能的。

  1. Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.

Thread.currentThread().setContextClassloader(...);


  • 确保类是由层次结构中较高的类加载器加载的。

  • Make sure the class is loaded by a class loader higher in the hierarchy.

    序列化和反序列化对象。 (哎呀!)

    Serialize and deserialize the object. (Yuck!)

    尽管如此,对你的特殊情况可能有更合适的方式。

    There is probably a more appropriate way for your particular situation though.

    这篇关于转换为同一个类时出现ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆