强制转换到同一类时出现 ClassCastException [英] ClassCastException when casting to the same class

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

问题描述

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

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

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

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;
}

然后我继续使用 validatorClass.newInstance() 创建一个 Validator 对象并将其存储在 validator 然后我创建一个 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);

Form 项目的生命周期中,我调用 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.

当我使用

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.

我收到这个有什么原因吗?

Any reason why I'm getting this?

推荐答案

我不太了解你对程序流程的描述,但通常当你得到 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天全站免登陆