2012-08-08 12 views
6

Giocando con classloader ho ricevuto la seguente eccezione:ClassCastException a causa di classloader?

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

Questo significa che un'istanza da un classloader non è calcinabile a una classe di un altro programma di caricamento classe?

Controllare il mio codice in cui sono in grado di instanciare 3 singleton grazie a classloaders, anche con la "" sicurezza.

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

} 

risposta

3

Non puoi lanciare tra i caricatori di classe. L'identità di classe è composta da un nome completo e dal caricatore di classe. Verifica class identity crysishere.

+1

Può java eseguire identità Cr ** y ** sis però? –

2

Sì, hai ragione.

Ciò accade spesso nei progetti OSGi, a causa della cattiva gestione delle dipendenze.

Problemi correlati