Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

enum and generics  RSS feed

 
Max Chandran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method



I am trying to build a map containing name to enum mapping to pass to the above method.
I tried this.

How do i build a map to pass to this method.
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have a look at this forum and the Beginning Java forum, where you will find two similar discussions: 1 2. Maybe they will help explain your problem. But let's see if I can get it right:-

Remember that the idea behind generics is to prevent exceptions being thrown when objects are cast to the wrong type. That improves type‑safety, by moving the errors to compile time. If there is a risk that a particular type will cause an exception at runtime, the compiler will not allow it. Remember the days before Java5 and generics: an incorrect type will compile and might throw an exception at runtime. Now things are different: an incorrect type which might throw an exception at runtime won't compile. So you move out of a situation where you might suffer a runtime error (exception) sometimes, to a situation where you will suffer a compile‑time error. Always. The compiler is programmed to be risk‑averse.

You know that <E extends Enum<E>> is the type of an enumerated type? So you are passing a reference to a Class object whose instances are enum constants. So your method heading says that E represents an enum type (line 1).
I presume you have the Enum.valueOf() call (line 4) written differently in the real code. Please always copy'n'paste code onto the website if possible, so we can see the exact problem, and it is easier to help you. You are, presumably, getting the enum constant corresponding to that text. Note that method isn't type‑safe; it will throw exceptions if the wrong details are passed, but you shou‍ld be able to use it to get an instance of the MyEnum class. So I presume that isn't a problem.
Now in line 3 of the second code block, you are creating a Map whose “V” is of type Class<?>. So you can pass Class<MyEnum> or String.class or who knows what. So far, so good. But what is going to happen when you pass that Map to the buildXXX method? The compiler will perceive that you might be passing any type of Class object as a “V” in that Map. Remember the compiler is programmed on the basis of Murphy's Law:-
Anything which can go wrong, will go wrong.
So the compiler cannot be confident that you will always pass the correct type of Class object, so the program won't compile. I think you are going to have to change the type from Class<?> to Class<E extends Enum<E>> in one place and Class<E> in the other, but am not sure. Somebody else will probably know better.

You didn't quite have the code tags right, and one line was too long to read, but I have corrected that.
 
Max Chandran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.. I tried that and still see compile errors in eclipse. The createEnum() is existing code and it works.
The testMap method job is to take a data map, and enumMap and convert given enum value which is String, into an actual enum and put that in dataMap.

 
Max Chandran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it working, had to typecast to Class.

 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max Chandran wrote:Got it working, had to typecast to Class. . . .
I think that means you have introduced another error into your code and have got away with it. The whole idea of generics is to reduce the amount of casting. If that method is public it will be possible for any code to pass arguments to it and there is nothing to stop the wrong type being passed. I presume you get lots of compiler warnings. Remember that code compiling doesn't imply that code is correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!