Solved

typesafe enum in generic class?

Posted on 2006-07-22
3
1,476 Views
Last Modified: 2008-01-09
Hi everybody,

I have a question about Generics and Enums: The answer is either simple (about 1 line) or there is no answer...

I have a parameterized class which uses an Enum. I want to use "typesafe" Enum.valueOf, but it doesn't work...

looks sth. like this:

public class MyClass<FIELDS extends Enum> {
      protected Class<FIELDS> fieldClass;

      public MyClass() {
            Type[] ata = ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments();
            this.fieldClass = (Class<FIELDS>) ata[0];
            ...
      }

      public void someMethod(String fieldName) {
            FIELDS f = Enum.valueOf(fieldClass, fieldName);
      }

This gives me a type safety warning in Eclipse 3.2.
Why?
Enum.valueOf is defined as
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)

and I'm passing a parameter that is declared as "Class<FIELDS>". What do I have to do to get a return type of FIELDS?

To make this clear: My code works fine, but I just want to use as much typesafe programming as possible and avoid using '@SuppressWarnings("unchecked")' where possible!
 
Thanks for any hints!
0
Comment
Question by:Ronald112197
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 50 total points
ID: 17159768
> public class MyClass<FIELDS extends Enum>
should be

public class MyClass<FIELDS extends Enum<FIELDS>>
0
 
LVL 2

Author Comment

by:Ronald112197
ID: 17159794
ah, ok, thanks for the super fast answer :-)
(see, I know this would only be one line :-)

I have to admit that there might be a tiny piece of the Generics puzzle that I still don't fully understand which I'd like to call "self referencing parameters", like Class<T> and Enum<T>.
Oh well, I'll figure that out sooner or later :-)
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17159804
its needed because the class Enum is also type-safe.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now