views:

83

answers:

2

So, I have an abstract class like:

public abstract class AbstractParent <E extends Enum<E>> {...}

Somewhere in a non-abstract method inside AbstractParent, I would like to iterate over the values of E. Is this possible?

For a better example:

public abstract class AbstractParent <E extends Enum<E>> {
    ...
    protected void doSomething() {
        //iterate over the values of E and perform an action using them
    }
}

public class Child extends AbstractParent<Child.Index> {
    public static enum Index {
        ...
    }
    public Child() {
        super();
        this.doSomething(); //this should iterate over Index's values
    }
}

EDIT:

So, thanks to mdma, this works awesomely:

public abstract class AbstractParent <E extends Enum<E>> {
    ...
    protected void doSomething() {
        //iterate over the values of E and perform an action using them
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type t = pt.getActualTypeArguments()[0];
        E[] enumValues = ((Class<E>)t).getEnumConstants();
        // enumValues is now an iterable array of the values inside Index
    }
}

public class Child extends AbstractParent<Child.Index> {
    public static enum Index {
        ...
    }
    public Child() {
        super();
        this.doSomething(); //this should iterate over Index's values
    }
}

Thanks LOADS to mdma, you deserve more points than I can give.

+2  A: 

Since generics are erased at runtime, the only way this is possible is by having a constructor that requires a Class<E> parameter on which you can then call getEnumConstants().

Michael Borgwardt
The people who utilize this abstract class are not really professional coders (likewise I am no professional, but I know for a fact that I outclass them :( ) and I was hoping to avoid making them deal with Class objects as much as possible. The enum type that will eventually be E will exist as a static enum in the class that overrides AbstractParent. Does that allow any cunning workarounds?
Jordan
This comment is also for mdma, because his suggestion involves the Class as well. Is it possible to define an abstract enum such that the overriding class can populate it but the abstract class can still reference it?
Jordan
superclass and interface generic types are not erased.
mdma
+4  A: 

EDIT2: Generics on superclasses and interfaces are not erased. You can get the generic type at runtime and use that to fetch the enum values. See Class.getGenericSuperclass. This will save you having to pass the value or a class in the constructor.

Original: You cannot do this with generics. However, if you pass in the corresponding class also, e.g. a constructor like

AbstractParent(Class<E> enumClass)
{
   this.enumClass = enumClass;
}

Then you can use that to fetch the corresponding enum values via

public E[] getValues()
{
    return this.enumClass.getEnumConstants();
}

EDIT: Although the clients are not professional programmers, the compiler will ensure the correct class is passed. You can also make the usage clear by providing examples, and unit tests.

You could also have the constructor take and actual value of the Enum, and derive the class from that. This might be simpler to use, since the parameter is then an E rather than the more "scary" Class<E>.

E.g.

AbstractParent(E enumValue)
{
   this.enumClass = enumValue.getClass();
}
mdma
See the comment on Borgwardt's answer, please (both of them)
Jordan
I'm gonna flag yours as accepted, because that edit should do nicely
Jordan
I've added an edit to my answer. You can pass in the actual value of the enum. Java does not support abstract Enums (at least not directly.) Can you provide an example of what you want to achieve?
mdma
Updated question as best as I can, hopefully it makes sense
Jordan
So: protected void doSomething() { Type t = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; // t.getClass().getEnumConstants() would be an enumeration of the values in E? ... }
Jordan
@Jordan - that's the idea. But don't call getClass on Type - it will be a Class instance.
mdma