2011-01-03 23 views
5

Creo que necesito ayuda para encontrar una clase de enumeración dentro de otra clase usando reflection en Java. He estado luchando con esto por mucho tiempo ahora. He leído esto, así como una serie de otras publicaciones y todas me hacen creer que debería funcionar de la siguiente manera.Encontrar una clase de enumeración usando Reflection en Java

public class ModelActivity { 
    public enum AttributeEnumeration { MODELID, MODELURGENCY, MODELDUEDATEANDTIME } 

    public static void main(String[] args) { 
    // Find the class with the given name 
    String className = "ModelActivity"; 
    Class modelClass = null; 
    try { 
     // Retrieve the Class with the given className... 
     modelClass = Class.forName(className); 
    } catch (ClassNotFoundException e) { 
     throw new RuntimeException("Class by name '" + className + "' not found.", e); 
    } 

    // Find the AttributeEnumeration within the class 
    String attributeEnumerationClassName = className + ".AttributeEnumeration"; 
    Class attributeEnumerationClass = null; 
    try { 
     attributeEnumerationClass = Class.forName(attributeEnumerationClassName); 
    } catch (ClassNotFoundException e) { 
     throw new RuntimeException("Class by name '" + attributeEnumerationClassName + "' not found.", e); 
    } 
    } 
} 

Sin embargo, lo que realmente sucede es que el modelClass se encuentra correctamente, pero el attributeEnumerationClass no es, es decir, consigo el segundo ClassNotFoundException de la siguiente manera:

Exception in thread "main" java.lang.RuntimeException: Class by name 'ModelActivity.AttributeEnumeration' not found. 
at ModelActivity.main(ModelActivity.java:27) 
    Caused by: java.lang.ClassNotFoundException: ModelActivity.AttributeEnumeration 
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:169) 
at ModelActivity.main(ModelActivity.java:25) 

Podría alguien por favor señalar THE- -probablemente obvio- me equivoco. Gracias.

+1

intente cargar la clase ' ModelActivity $ AttributeEnumeration'. –

Respuesta

10

Vea usted mismo:

Salida:

foo.bar.Outer $ Interior

nombres de clase interna se delimitan con $, no con un período así, quieres ModelActivity$AttributeEnumeration.

Por cierto:

La sintaxis $ es válida sólo para la carga de clases. Utilizar períodos acceder a instancias de la clase en la fuente de la siguiente manera:

import foo.bar.Outer.Inner; 
// ... 
private Inner myEnumValue; 

o como esto:

private foo.bar.Outer.Inner myEnumValue; 

O, para decirlo de esta manera:

assertEquals(// two ways to reference the same class 
    foo.bar.Outer.Inner.class, 
    Class.forName("foo.bar.Outer$Inner") 
); 
+0

¡Eso es una locura, no lo sabía! –

+0

Simplemente defina algunas clases internas (por ejemplo, detectores de eventos) y busque en sus carpetas de resultados. Verás $$$ por todo el lugar :-) –

Cuestiones relacionadas