Solved

typesafe enum in generic class?

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

691 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