<tfoot id='79X9W'></tfoot>

    <bdo id='79X9W'></bdo><ul id='79X9W'></ul>
  • <i id='79X9W'><tr id='79X9W'><dt id='79X9W'><q id='79X9W'><span id='79X9W'><b id='79X9W'><form id='79X9W'><ins id='79X9W'></ins><ul id='79X9W'></ul><sub id='79X9W'></sub></form><legend id='79X9W'></legend><bdo id='79X9W'><pre id='79X9W'><center id='79X9W'></center></pre></bdo></b><th id='79X9W'></th></span></q></dt></tr></i><div id='79X9W'><tfoot id='79X9W'></tfoot><dl id='79X9W'><fieldset id='79X9W'></fieldset></dl></div>
    <legend id='79X9W'><style id='79X9W'><dir id='79X9W'><q id='79X9W'></q></dir></style></legend>
    1. <small id='79X9W'></small><noframes id='79X9W'>

      1. ClassCastException 因为类加载器?

        ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
        1. <tfoot id='IHpXN'></tfoot>
            <legend id='IHpXN'><style id='IHpXN'><dir id='IHpXN'><q id='IHpXN'></q></dir></style></legend>
          1. <small id='IHpXN'></small><noframes id='IHpXN'>

              • <bdo id='IHpXN'></bdo><ul id='IHpXN'></ul>
                <i id='IHpXN'><tr id='IHpXN'><dt id='IHpXN'><q id='IHpXN'><span id='IHpXN'><b id='IHpXN'><form id='IHpXN'><ins id='IHpXN'></ins><ul id='IHpXN'></ul><sub id='IHpXN'></sub></form><legend id='IHpXN'></legend><bdo id='IHpXN'><pre id='IHpXN'><center id='IHpXN'></center></pre></bdo></b><th id='IHpXN'></th></span></q></dt></tr></i><div id='IHpXN'><tfoot id='IHpXN'></tfoot><dl id='IHpXN'><fieldset id='IHpXN'></fieldset></dl></div>
                  <tbody id='IHpXN'></tbody>

                  本文介绍了ClassCastException 因为类加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在使用类加载器时,我遇到了以下异常:

                  While playing with classloaders i got the following exception:

                  Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton
                  

                  这是否意味着来自类加载器的实例不能转换为另一个类加载器的类?

                  Does this mean that an instance from a classloader is not castable to an class of another classloader?

                  检查我的代码,感谢类加载器,我可以在其中实例化 3 个单例,即使使用"安全性也是如此.

                  Check my code where i'm able to instanciate 3 singletons thanks to classloaders, even with the "" security.

                  public static void main(String[] args) throws Exception {
                          URL basePath = new URL("file:/myMavenPath/target/classes/");
                  
                      Object instance = getClassInstance(Singleton.class);
                      System.out.println(instance);
                      //
                      Object instance2 = getClassInstance(
                              new URLClassLoader( new URL[]{basePath} , null )
                                      .loadClass("my.Singleton")
                      );
                      System.out.println(instance2);
                      //
                      Object instance3 = getClassInstance(
                              new URLClassLoader( new URL[]{basePath} , null )
                                      .loadClass("my.Singleton")
                      );
                      System.out.println(instance3);
                  
                      // Only the 1st cast is ok
                      Singleton testCast1 = (Singleton) instance;
                      System.out.println("1st cast ok");
                      Singleton testCast2 = (Singleton) instance2;
                      System.out.println("2nd cast ok");
                      Singleton testCast3 = (Singleton) instance3;
                      System.out.println("3rd cast ok");
                  }
                  
                  private static Object getClassInstance(Class clazz) throws Exception {
                      Method method = clazz.getMethod("getInstance");
                      method.setAccessible(true);
                      return method.invoke(null);
                  }
                  
                  
                  class Singleton {
                  
                      private static final Singleton INSTANCE = new Singleton();
                  
                      public static Singleton getInstance() {
                          return INSTANCE;
                      }
                  
                      private Singleton() {
                          Exception e = new Exception();
                          StackTraceElement[] stackTrace = e.getStackTrace();
                          if (!"<clinit>".equals(stackTrace[1].getMethodName())) {
                              throw new IllegalStateException("You shall not instanciate the Singleton twice !",e);
                          }
                      }
                  
                      public void sayHello() {
                          System.out.println("Hello World ! " + this);
                      }
                  
                  }
                  

                  推荐答案

                  你不能在类加载器之间进行转换.类标识由完全限定名和类加载器组成.检查类身份危机这里.

                  You cannot cast between class loaders. Class identity is composed of fully qualified name and the class loader. Check class identity crysis here.

                  这篇关于ClassCastException 因为类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                      <small id='WrN4u'></small><noframes id='WrN4u'>

                        <tbody id='WrN4u'></tbody>
                        <bdo id='WrN4u'></bdo><ul id='WrN4u'></ul>

                          <legend id='WrN4u'><style id='WrN4u'><dir id='WrN4u'><q id='WrN4u'></q></dir></style></legend>
                          <tfoot id='WrN4u'></tfoot>
                          <i id='WrN4u'><tr id='WrN4u'><dt id='WrN4u'><q id='WrN4u'><span id='WrN4u'><b id='WrN4u'><form id='WrN4u'><ins id='WrN4u'></ins><ul id='WrN4u'></ul><sub id='WrN4u'></sub></form><legend id='WrN4u'></legend><bdo id='WrN4u'><pre id='WrN4u'><center id='WrN4u'></center></pre></bdo></b><th id='WrN4u'></th></span></q></dt></tr></i><div id='WrN4u'><tfoot id='WrN4u'></tfoot><dl id='WrN4u'><fieldset id='WrN4u'></fieldset></dl></div>