Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25903

Re: limitations on using enum as generic parameter

From Ken Wesson <kwesson@gmail.com>
Subject Re: limitations on using enum as generic parameter
Newsgroups comp.lang.java.programmer
References <76e9be77-f846-4559-82ec-1d774a8a6a0b@q36g2000yqn.googlegroups.com>
Message-ID <4d518f96$1@news.x-privat.org> (permalink)
Date 2011-02-08 19:46 +0100
Organization X-Privat.Org NNTP Server - http://www.x-privat.org

Show all headers | View raw


On Tue, 08 Feb 2011 09:50:32 -0800, dalamb@cs.queensu.ca wrote:

> Is there any way around this, or is there just no way to refer to an
> enum's values() method when the enum is a generic parameter?
> 
>     public EnumCodeSet() {
> 	enums = E.values();
>     }

That's not going to work. It's a static method of a class that isn't 
determined at this point in the code, so the compiler simply has no clue 
what to call. Hence the error message.

If you really need to do something like this, you'll need to use 
reflection on a class object. Giving your class's constructors an added 
Class<E> argument that gets stored in a type_token field and gets used 
for reflection will work.

Reflection is slow, so the constructor should probably grab the values 
and store them in an instance field. It looks like you were calling 
values in your constructor anyway, so it turns from

public EnumCodeSet() {
    enums = E.values();
}

to

public EnumCodeSet (Class<E> type_token) {
    enums = type_token.getMethod("values").invoke(null);
}

Of course, when you make an EnumCodeSet you now have to pass the enum 
class as a parameter to the constructor, e.g. new EnumCodeSet
(MyEnum.class).

You'll have to do something similar at the other site where you had an 
error, or (better) store the reflectively-obtained result in an instance 
field in the constructur and use the new instance field where you had the 
error.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

limitations on using enum as generic parameter "dalamb@cs.queensu.ca" <david.alex.lamb@gmail.com> - 2011-02-08 09:50 -0800
  Re: limitations on using enum as generic parameter Lew <noone@lewscanon.com> - 2011-02-11 07:36 -0500
  Re: limitations on using enum as generic parameter Daniele Futtorovic <da.futt.news@laposte.net.invalid> - 2011-02-08 20:36 +0100
  Re: limitations on using enum as generic parameter Ken Wesson <kwesson@gmail.com> - 2011-02-08 19:46 +0100
  Re: limitations on using enum as generic parameter Roedy Green <see_website@mindprod.com.invalid> - 2011-02-10 22:50 -0800
  Re: limitations on using enum as generic parameter Roedy Green <see_website@mindprod.com.invalid> - 2011-02-10 22:47 -0800
  Re: limitations on using enum as generic parameter markspace <nospam@nowhere.com> - 2011-02-08 10:34 -0800
    Re: limitations on using enum as generic parameter Lew <lew@lewscanon.com> - 2011-02-08 10:38 -0800

csiph-web