mercredi 7 juin 2017

Class.isEnum() Returns False for Class.getDeclaredClasses

I am trying to determine if one of the inner classes of a class I am examining is an enum. Here is the sample class:

public class TopLevel extends Irrelevent {

  enum OurEnum {
    ONE, TWO, THREE, FOUR, GET, YOUR, WOMAN, ON, THE, FLOOR
  }

  public class InnerClass {
    public boolean blah;
  }

  public static class InnerClassStatic {
    public boolean meh;
  }
}

In the following scenario, none of the operations tell me that OurEnum is an enum. Why?

    // Now examine inner classes
    Class<?>[] innerClasses = clazz.getDeclaredClasses();
    for (Class<?> inner : innerClasses) {
      if (inner.isEnum() || 
          inner.getSuperclass().isEnum() || 
          inner.getDeclaringClass().isEnum() || 
          Enum.class.isAssignableFrom(inner) || 
          inner.isAssignableFrom(Enum.class)) {
        // Do something snazzy
      }

I do not want to use the shotgun approach. I have looked at several questions (e.g. Checking if a class is java.lang.Enum, Is it better to use class.isEnum() or instanceof Enum?, How to check if java.lang.reflect.Type is an Enum, and isEnum() ambiguous behaviour for anonymous inner class)

What am I missing?





Aucun commentaire:

Enregistrer un commentaire