Solved

typesafe enum in generic class?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 79
countHi challenge 25 85
computer science syllabus 3 52
for i loop in grovy 1 32
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

22 Experts available now in Live!

Get 1:1 Help Now