<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 因为类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  Singleton across JVM or Application instance or Tomcat instance(跨 JVM 或 Application 实例或 Tomcat 实例的单例)

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