Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding an Enumeration Class using Reflection in Java

I think I need some help with finding an enumeration class within another class using reflection in Java. I have been battling with this for far too long now. I have read this as well as a number of other posts and they all make me believe it should work as below.

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

However, what actually happens is that the modelClass is found correctly, but the attributeEnumerationClass is not, that is, I get the second ClassNotFoundException as follows:

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)

Could anyone please point the--probably obvious--mistake out to me. Thank you.

like image 379
McKrassy Avatar asked Jan 03 '11 08:01

McKrassy


1 Answers

See for yourself:

package foo.bar;

public class Outer{

    public enum Inner{}

    public static void main(final String[] args){
        System.out.println(Inner.class.getName());
    }

}

Output:

foo.bar.Outer$Inner

Inner class names are delimited with $, not with a period, so you want ModelActivity$AttributeEnumeration.

BTW:

The $ syntax is valid for class loading only. Use periods to access instances of the class in source as follows:

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

or like this:

private foo.bar.Outer.Inner myEnumValue;

Or, to put it this way:

assertEquals( // two ways to reference the same class
    foo.bar.Outer.Inner.class,
    Class.forName("foo.bar.Outer$Inner")
);
like image 119
Sean Patrick Floyd Avatar answered Nov 17 '22 04:11

Sean Patrick Floyd