Solved

typesafe enum in generic class?

Posted on 2006-07-22
3
1,486 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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 step-by-step guide to install VisualVM launcher in eclipse.

772 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