Get Values from Enum Known Only At Runtime - java

I need to get all the values from an enum, whose type will only be known at runtime. I've come up with the following, but would like to know if anyone knows a better way:
enum TestEnum {
FOO,
BAR
}
Enum[] getValuesForEnum(Class type) {
try {
Method m = type.getMethod("values");
return (Enum[])m.invoke(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Class testEnum = Class.forName("TestEnum");
getValuesForEnum(testEnum);
Thanks!

Use the available API instead:
T[] getValuesForEnum(Class<T> type) {
return type.getEnumConstants();
}
From the Javadoc:
Returns the elements of this enum class or null if this Class object does not represent an enum type.
Note that I have turned your method into generic to make it typesafe. This way you need no downcasts to get the actual enum values from the returned array. (Of course, this makes the method so trivial that you can omit it and call type.getEnumConstants() directly :-)

Here's a variant of Kevin Stembridge's answer that preserves the type (avoiding downcasts), whilst still guarding against being called with a non-enum type:
static <E extends Enum<E>> E[] getValuesForEnum(Class<E> clazz) {
return clazz.getEnumConstants();
}

I use type.getEnumConstants().

I think this works:
Enum<?>[] getValuesForEnum(Class<Enum<?>> enumType) {
return enumType.getEnumConstants();
}

Related

Creating a List using the concrete type's Class

I can understand the example of instantiating a List object using a generic type. My current homework assignment involves creating an Appender for Log4j that stores the logs in a list. One of the requirements is that the user could specify a concrete implementation of List for the constructor and I have done this by just accepting a List that they have created.
Is there a way to make it so that they can provide the Class<T> implementing List that they want to be using and my constructor will instantiate a new instance of it?
If I understand your question, then yes; the simplest method I can think of, would be a constructor that takes a Collection. For example, ArrayList(Collection).
List<SomeType> copyList = new ArrayList<>(original);
Or perhaps, you wanted something that returns a List<T> given an item T. Something like Collections.singletonList(T)
SomeType t = // ...
List<SomeType> al = Collections.singletonList(t);
Class<T> will let you call newInstance(), which will return you a T. If you then combine this with wildcard generics you could in theory specify a Class<? extends List<LoggingEvent>> and create any type that implements the List<LoggingEvent> interface.
However here is the first problem: you cannot use a parameterized type with the class literal (i.e. LinkedList<LoggingEvent>.class will not compile). Therefore you have to relax your method/constructor parameter to only bound the wildcard on the raw type of List, like this: Class<? extends List>.
So now when you create the List you will have to cast it to the correct generic type. That will mean you need to do unchecked conversion using #SuppressWarnings("unchecked"). In this case this is safe to do as you will never try and use that raw type as any other generic type other than List<LoggingEvent>. †
The final implementation would look something like:
class LogStore {
private List<LogLine> loggingEvents = null;
public LogStore(Class<? extends List> clazz) {
try {
#SuppressWarnings("unchecked")
List<LogLine> logStoreList = clazz.newInstance();
this.loggingEvents = logStoreList;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
† In this case this may be safe to do so if you provide the user with a set of prompts to let them choose a standard List<LoggingEvent> implementation, but if they are going to use this as an API and you have no control over which Class<? extends List> they pass in then you have two choices: either let the api fail at runtime in unpredictable places, or try and check the type argument for list. However, even if you do check the type argument you cannot check for all eventualities and the API may still break (e.g. if the user passes the class for a read-only List<LoggingEvent>).
For example, even something as simple as this will cause a (to the API user strange, possibly untraceable) runtime exception:
new LogStore(IntList.class);
// Used with IntList defined as ...
public class IntList extends ArrayList<Integer> {
#Override public Integer remove(int index) { return super.remove(index); }
}
If you choose to do the latter of the two options you will want to do something like this to check it really is a List<LoggingEvent>:
public LogStore(Class<? extends List> clazz) {
assertListTypeArgsValid(clazz);
// ... the rest of the above method implementation ...
}
private void assertListOk(Class<? extends List> clazz) {
boolean verified = false;
for (Type intr : clazz.getGenericInterfaces()) {
if (!(intr instanceof ParameterizedType)) continue;
ParameterizedType pIntr = (ParameterizedType)intr;
if (pIntr.getRawType().getTypeName() != "java.util.List") continue;
Type[] typeArgs = pIntr.getActualTypeArguments();
if (typeArgs.length != 1) break;
Class<?> tac = (Class<?>)typeArgs[0];
verified = tac.isAssignableFrom(LoggingEvent.class);
if (!verified) throw new IllegalArgumentException("clazz must be a List<LoggingEvent>, and is a: "
+ pIntr.getTypeName());
break;
}
if (!verified) throw new IllegalArgumentException("clazz must be a List<LoggingEvent>");
}

How to get Class<?> object of a generic type

I have a static method which will return a custom type based on the type of the class,
public class GenericMethod {
public static <T> T returnGeneric(Class<T> clazz) {
return null;
}
}
Now, I want to pass a class with a generic type in to it,
CustomType<String> type = GenericMethod.returnGeneric(CustomType.class);
Only problem is that the above statement gives and unchecked conversion warning.
I tried the workaround new CustomType<String>().getName() which is also not solving the problem.
Is there a right way to it, or the only solution is to use #SuppressWarnings ?
What you would/should like to try is this:
CustomType<String> type = GenericMethod.returnGeneric(CustomType<String>.class);
Unfortunately, because of type erasure there is no difference between CustomType<A>.class and CustomType<B>.class, hence this syntax is not supported by Java.
So my $.02: what you are asking for is not possible, so hang on to the #suppresswarnings...
The best approach is to use a wrapper method and place all your warnings in a single place.
And the term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety.
In theory you can't do it because of type erasure.
In practice though ;) you can do it because the information is actually in the .class files.
The easiest way I know of is using Spring's GenericTypeResolver.
Have a look at it.
As you said #Simeon "In theory you can't do it because of type erasure".
You can use it only if you have subclasses of CustomType:
class GenericMethod {
public static <T> T returnGeneric(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static void main(String [] args){
CustomeType<String> ct = returnGeneric(StringCustomeType.class);
System.out.println(ct);
}
}
class StringCustomeType extends CustomeType<String> {
}
class CustomeType<T> {
}

Instantiating generics type in java

I would like to create an object of Generics Type in java. Please suggest how can I achieve the same.
Note: This may seem a trivial Generics Problem. But I bet.. it isn't. :)
suppose I have the class declaration as:
public class Abc<T> {
public T getInstanceOfT() {
// I want to create an instance of T and return the same.
}
}
public class Abc<T> {
public T getInstanceOfT(Class<T> aClass) {
return aClass.newInstance();
}
}
You'll have to add exception handling.
You have to pass the actual type at runtime, since it is not part of the byte code after compilation, so there is no way to know it without explicitly providing it.
In the code you posted, it's impossible to create an instance of T since you don't know what type that is:
public class Abc<T>
{
public T getInstanceOfT()
{
// There is no way to create an instance of T here
// since we don't know its type
}
}
Of course it is possible if you have a reference to Class<T> and T has a default constructor, just call newInstance() on the Class object.
If you subclass Abc<T> you can even work around the type erasure problem and won't have to pass any Class<T> references around:
import java.lang.reflect.ParameterizedType;
public class Abc<T>
{
T getInstanceOfT()
{
ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
Class<T> type = (Class<T>) superClass.getActualTypeArguments()[0];
try
{
return type.newInstance();
}
catch (Exception e)
{
// Oops, no default constructor
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
String instance = new SubClass().getInstanceOfT();
System.out.println(instance.getClass());
}
}
class SubClass
extends Abc<String>
{
}
What you wrote doesn't make any sense, generics in Java are meant to add the functionality of parametric polymorphism to objects.
What does it mean? It means that you want to keep some type variables of your classes undecided, to be able to use your classes with many different types.
But your type variable T is an attribute that is resolved at run-time, the Java compiler will compile your class proving type safety without trying to know what kind of object is T so it's impossible for it to let your use a type variable in a static method. The type is associated to a run-time instance of the object while public void static main(..) is associated to the class definition and at that scope T doesn't mean anything.
If you want to use a type variable inside a static method you have to declare the method as generic (this because, as explained type variables of a template class are related to its run-time instance), not the class:
class SandBox
{
public static <T> void myMethod()
{
T foobar;
}
}
this works, but of course not with main method since there's no way to call it in a generic way.
EDIT: The problem is that because of type erasure just one generic class is compiled and passed to JVM. Type checker just checks if code is safe, then since it proved it every kind of generic information is discarded.
To instantiate T you need to know the type of T, but it can be many types at the same time, so one solution with requires just the minimum amount of reflection is to use Class<T> to instantiate new objects:
public class SandBox<T>
{
Class<T> reference;
SandBox(Class<T> classRef)
{
reference = classRef;
}
public T getNewInstance()
{
try
{
return reference.newInstance();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
SandBox<String> t = new SandBox<String>(String.class);
System.out.println(t.getNewInstance().getClass().getName());
}
}
Of course this implies that the type you want to instantiate:
is not a primitive type
it has a default constructor
To operate with different kind of constructors you have to dig deeper into reflection.
You need to get the type information statically. Try this:
public class Abc<T> {
private Class<T> clazz;
public Abc(Class<T> clazz) {
this.clazz = clazz;
}
public T getInstanceOfT()
throws throws InstantiationException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException,
NoSuchMethodException,
SecurityException {
return clazz.getDeclaredConstructor().newInstance();
}
}
Use it as such:
Abc<String> abc = new Abc<String>(String.class);
abc.getInstanceOfT();
Depending on your needs, you may want to use Class<? extends T> instead.
The only way to get it to work is to use Reified Generics. And this is not supported in Java (yet? it was planned for Java 7, but has been postponed). In C# for example it is supported assuming that T has a default constructor. You can even get the runtime type by typeof(T) and get the constructors by Type.GetConstructor(). I don't do C# so the syntax may be invalid, but it roughly look like this:
public class Foo<T> where T:new() {
public void foo() {
T t = new T();
}
}
The best "workaround" for this in Java is to pass a Class<T> as method argument instead as several answers already pointed out.
First of all, you can't access the type parameter T in the static main method, only on non-static class members (in this case).
Second, you can't instantiate T because Java implements generics with Type Erasure. Almost all the generic information is erased at compile time.
Basically, you can't do this:
T member = new T();
Here's a nice tutorial on generics.
You don't seem to understand how Generics work.
You may want to look at http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
Basically what you could do is something like
public class Abc<T>
{
T someGenericThing;
public Abc(){}
public T getSomeGenericThing()
{
return someGenericThing;
}
public static void main(String[] args)
{
// create an instance of "Abc of String"
Abc<String> stringAbc = new Abc<String>();
String test = stringAbc.getSomeGenericThing();
}
}
I was implementing the same using the following approach.
public class Abc<T>
{
T myvar;
public T getInstance(Class<T> clazz) throws InstantiationException, IllegalAccessException
{
return clazz.newInstance();
}
}
I was trying to find a better way to achieve the same.
Isn't it possible?
Type Erasure Workaround
Inspired by #martin's answer, I wrote a helper class that allows me to workaround the type erasure problem. Using this class (and a little ugly trick) I'm able to create a new instance out of a template type:
public abstract class C_TestClass<T > {
T createTemplateInstance() {
return C_GenericsHelper.createTemplateInstance( this, 0 );
}
public static void main( String[] args ) {
ArrayList<String > list =
new C_TestClass<ArrayList<String > >(){}.createTemplateInstance();
}
}
The ugly trick here is to make the class abstract so the user of the class is forced to subtype it. Here I'm subclassing it by appending {} after the call to the constructor. This defines a new anonymous class and creates an instance of it.
Once the generic class is subtyped with concrete template types, I'm able to retrieve the template types.
public class C_GenericsHelper {
/**
* #param object instance of a class that is a subclass of a generic class
* #param index index of the generic type that should be instantiated
* #return new instance of T (created by calling the default constructor)
* #throws RuntimeException if T has no accessible default constructor
*/
#SuppressWarnings( "unchecked" )
public static <T> T createTemplateInstance( Object object, int index ) {
ParameterizedType superClass =
(ParameterizedType )object.getClass().getGenericSuperclass();
Type type = superClass.getActualTypeArguments()[ index ];
Class<T > instanceType;
if( type instanceof ParameterizedType ) {
instanceType = (Class<T > )( (ParameterizedType )type ).getRawType();
}
else {
instanceType = (Class<T > )type;
}
try {
return instanceType.newInstance();
}
catch( Exception e ) {
throw new RuntimeException( e );
}
}
}
There are hacky ways around this when you really have to do it.
Here's an example of a transform method that I find very useful; and provides one way to determine the concrete class of a generic.
This method accepts a collection of objects as input, and returns an array where each element is the result of calling a field getter on each object in the input collection. For example, say you have a List<People> and you want a String[] containing everyone's last name.
The type of the field value returned by the getter is specified by the generic E, and I need to instantiate an array of type E[] to store the return value.
The method itself is a bit ugly, but the code you write that uses it can be so much cleaner.
Note that this technique only works when somewhere in the input arguments there is an object whose type matches the return type, and you can deterministically figure it out. If the concrete classes of your input parameters (or their sub-objects) can tell you nothing about the generics, then this technique won't work.
public <E> E[] array (Collection c) {
if (c == null) return null;
if (c.isEmpty()) return (E[]) EMPTY_OBJECT_ARRAY;
final List<E> collect = (List<E>) CollectionUtils.collect(c, this);
final Class<E> elementType = (Class<E>) ReflectionUtil.getterType(c.iterator().next(), field);
return collect.toArray((E[]) Array.newInstance(elementType, collect.size()));
}
Full code is here: https://github.com/cobbzilla/cobbzilla-utils/blob/master/src/main/java/org/cobbzilla/util/collection/FieldTransformer.java#L28
It looks like you are trying to create the class that serves as the entry point to your application as a generic, and that won't work... The JVM won't know what type it is supposed to be using when it's instantiated as you start the application.
However, if this were the more general case, then something like would be what you're looking for:
public MyGeneric<MyChoiceOfType> getMeAGenericObject(){
return new MyGeneric<MyChoiceOfType>();
}
or perhaps:
MyGeneric<String> objMyObject = new MyGeneric<String>();
Abc<String> abcInstance = new Abc<String> ();
..for example

Problem at JUnit test with generics

In my utility method:
public static <T> T getField(Object obj, Class c, String fieldName) {
try {
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(obj);
} catch (Exception e) {
e.printStackTrace();
fail();
return null;
}
}
The line
return (T) field.get(obj);
gives the warning "Type safety: Unchecked cast from Object to T";
but I cannot perform instanceof check against type parameter T,
so what am I suppose to do here?
The annotation #SuppressWarnings will stop the compiler reporting this warning. I don't think there's any way you can get away from the compiler warning when using reflection like this. Something like the following:
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
#SuppressWarnings(value="unchecked")
T t = (T) field.get(obj);
return t;
You can easily solve this problem by adding an additional parameter to your method which will specify the type of the filed, the method will then look as follows:
public static <T> T getField(Class<T> fieldType, Object obj, Class<?> c,
String fieldName)
{
try {
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(obj);
return fieldType.cast(value);
} catch (Exception e) {
e.printStackTrace();
fail();
return null;
}
}
And here's how you can use it: getField(String.class, new G(), G.class, "s") where G is defined as:
public class G {
String s = "abc";
}
A 2nd improvement is to eliminate the c parameter of getFiled(). c can be obtained inside the method by invoking obj.getClass(). The only caveat is that this will give you the dynamic type of the object so you mat want to loop over all of C's superclasses until you find the field you're looking for, or until you arrive at Object (You will also need to use c.getFields() and look for the field in the resulting array).
I think that these changes will make your method easier to use and less prone to errors so it's worth the effort.
Generics are there to provide type safety in places where you didn't previously have any in Java. So it used to be that if you had a list full of Strings you had to do:
String myString = (String)myList.get(0);
but now you can retrieve it without casting it:
String myString = myList.get(0); //Compiler won't complain
When you generify using the variable T, you are saying T is a placeholder for a specific type, which will be defined on the instance of the class at instantiation time. For instance:
public class ArrayList<T> {
public ArrayList<T> {
....
}
}
allows you to instantiate the list with:
ArrayList<String> myList = new ArrayList<String>();
Now every function on ArrayList will return a String, and the compiler knows this so it doesn't require a cast. Each of those functions was defined much like yours above:
public T get(int index);
public void set(int index, T object);
at compile time they become:
public String get(int index);
public void set(int index, String object);
In your case, however, you seem to be trying to use T as a wildcard, which is different from a placeholder for a specific type. You might call this method three times for three different fields, each of which has a different return type, right? This means that, when you instantiate this class, you cannot pick a single type for T.
In general, look at your method signatures and ask yourself "will a single type be substituted for T for each instance of this class"?
public static <T> T getField(Object obj, Class c, String fieldName)
If the answer is "no", that means this is not a good fit for Generics. Since each call will return a different type, you have to cast the results from the call. If you cast it inside this function, you're losing any benefits Generics would provide, and might as well save yourself the headaches.
If I've misunderstood your design, and T does refer to a single type, then simply annotating the call with #SuppressWarnings(value="unchecked") will do the trick. But if I've understood correctly, fixing this error will just lead you to a long road of confusion unless you grok what I've written above.
Good luck!
As suggested above, you can specify the expected type of the field and call the cast method.
Also. you don't need to pass argument object's class. You can find out what it is by calling obj.getClass()
This simplifies your code to
public static <T> T getField(Object obj, Class<T> fieldClass, String fieldName) {
try {
Class<?> declaringClass = obj.getClass();
Field field = declaringClass.getDeclaredField(fieldName);
field.setAccessible(true);
return fieldClass.cast(field.get(obj));
}
catch (Exception e) {
throw new AssertionFailedError();
}
}

Can Java methods return type Enum?

I could be wrong but I'm guessing from Why can't enums be declared locally in a method?
that, since an enum in Java cannot be declared locally, that therefore it is problematic for a method to return type Enum? I can declare that a method should return an Enum (see below) but how would one then go about implementing such a method to return anything other than null, or a reference to an Enum declared outside the method? My first inclination would be to investigate using Generics for this but I'd like to avoid any deadends if the SO community can help me avoid them.
private Enum resources() {
return null;
}
I think you're correct, it's only going to be able to either return null or an Enum declared somewhere else. But you don't necessarily have to specify that "something else" at compile time.
class EnumEnumerator<T extends Enum<T>> implements Iterable<T> {
private final Class<T> enumClass;
public EnumEnumerator(Class<T> enumClass) {
this.enumClass = enumClass;
}
public Iterator<T> iterator() {
T[] values = enumClass.getEnumConstants();
return Arrays.asList(values).iterator();
}
}
Later, you invoke it by specializing the generic constructor and passing in the enum class you're interested in:
class EnumEnumeratorDemo {
enum Foo {
BAR, BAZ, QUX;
#Override public String toString() {
return name().toLowerCase();
}
}
public static void main(String[] args) {
for (Foo f : new EnumEnumerator<Foo>(Foo.class)) {
System.out.println(f);
}
}
}
(Obviously this is a contrived example and in real life you should just call Foo.values(), but you get the idea.)
The entire point of the way Java does Enums is that they are typesafe--so you wouldn't return an Enum (that would be double-plus ungood) instead you return the actual type you define (like "Suit") which acts just like a class. Suit has 4 "Enumerated" instances.
If you were expecting a "Suit", what good would it be to return a "Rank" of 7? It would break everything!
Also if you passed an "Enum" or some generic value, you couldn't call methods on it. The coolest thing about TypeSafe Enums is that you can just get a "Suit" and call "Suit.getColor()" and fully expect to get the color of that suit. You could also have a ranksHigherThan(Suit s) which might fulfill:
assertTrue(SPADES.ranksHigherThan(HEARTS));
Or, more importantly:
suit1.ranksHigherThan(suit2);
(assuming they were both passed in and you don't know what they are)
Type safety is really amazing (even though it feels a little uncomfortable at first), embrace it.
All enums implement the interface Enum, so you can certainly write a method that returns an enum this way. But this method will return a single enum value. There is no way to return a generic value which encompasses the whole enum (apart from returning the class and doing reflection). You can however return all the enum values which is more or less what you want I think.
enum Resources { ONE, TWO, THREE }
private Enum<?>[] resources() {
return Resources.values();
}
One benefit of this approach is you can return more or less values for example:
enum Resources { ONE, TWO, THREE }
enum MoreResources { UN, DEUX, TROIS }
private Enum<?>[] resources() {
List<Enum<?>> resources = new ArrayList<Enum<?>>();
resources.addAll(Arrays.asList(Resources.values());
resources.addAll(Arrays.asList(MoreResources.values());
return resources.toList(new Enum<?>[] {});
}
An even better approach that is more typesafe is to have the enums of interest
implement a common interface e.g.
public interface Resources {}
enum SomeResources implements Resources { ONE, TWO, THREE }
enum MoreResources implements Resources { UN, DEUX, TROIS }
private Resources[] resources() {
List<Resources> resources = new ArrayList<Resources>();
resources.addAll(Arrays.asList(Resources.values());
resources.addAll(Arrays.asList(MoreResources.values());
return resources.toList(new Resources[] {});
}
You can add additional methods to the interface to provide more functionality.
What are you trying to accomplish? This is a way to return an Enum:
public class Test
{
public static void main(String args[])
{
System.out.println(doit());
}
public enum Foo {
BAR,
BAZ;
}
public static Enum doit() {
return Enum.valueOf(Foo.class,"BAR");
}
}
But, I'm guessing this is not what you are going for?
Yes, it definitely is possible.
private Enum getRetentionPolicy() {
return java.lang.annotation.RetentionPolicy.SOURCE;
}
If your question is about declaring Enums, you may declare them:
in their own java file, similar to a top-level class;
within a java file belonging to another class, similar to a static inner class;
Not totally sure what your goal is, but if you wanted to return a generified method (i.e. one that would be overridden) you might have something like the following:
public class MyEnumClass<T extends Enum<T>> {
public T resources() {
//do stuff here
}
}
Not entirely sure what you would gain there, although it can be beneficial if you are talking about different sets of Enums and their elements.
If you are talking about the Enum class (i.e. the percursor to Iterator) as far as I know it has not been generified, so I am not sure generics would help much here.
You can refer to a value of an enum by its name, e.g. Suit.SPADES.
You can iterate over all values by using the values() method and pick one of the values.

Categories