Solved

typesafe enum in generic class?

Posted on 2006-07-22
3
1,499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 86
jsp error 6 67
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 32
junit initializtion error 2 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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