keyongtech


  keyongtech > java > 11/2006

 #1  
11-05-06, 09:56 PM
Martin Lorentzson
I would like to determine the class value of a generic type. Is it
possible?

Right now, I'm passing the type as a parameter to the class
constructor:

public class Example<T extends Enum<T>> {

private final Class<T> clazz;

protected Example(Class<T> clazz) {
this.clazz = clazz;
}

public T parse(String text) {
// Can I derive the value of clazz from T?
T value = T.valueOf(clazz, text);
return value;
}
}

But I'm wondering if I can derive the class value of T instead of
using a constructor parameter or is type erasure as play here?

/Martin
 #2  
11-05-06, 10:17 PM
Daniel Pitts
Martin Lorentzson wrote:
[..]
> // Can I derive the value of clazz from T?
> T value = T.valueOf(clazz, text);
> return value;
> }
> }
>
> But I'm wondering if I can derive the class value of T instead of
> using a constructor parameter or is type erasure as play here?
>
> /Martin


In fact, you cannot. That is the whole point(problem) of erasure.
Even the EnumMap and EnumSet classes require a Class object in their
construction.
 #3  
11-06-06, 09:02 PM
Martin Lorentzson
"Daniel Pitts" <googlegroupie> writes:

> In fact, you cannot. That is the whole point(problem) of erasure.


OK, I thought I figured as much from the docs but I just wanted to
make sure. Thanks.
Similar Threads
Java Generics and Erasure foobar

Maybe it's me but I have been reviewing this so called major feature of 1.5 and I cannot see the value. I think the mistakes it detects at compile time are trivial, in that...

Workaround to overcome the generics limitation causes by Erasure

Initially, I thought the introduce of generics in Java would solve me some problem in pattern design. Here, I make use of generic to design Observer/Subject pattern. public...

Generics: struggling against type erasure...

Let me say: generics type erasure is just a misfeature that's embarrassingly hiped as a "feature". Is there any wizard that can solve this annoying problem...

Type erasure bug?

Consider the following code: Vector<String> s=new Vector<String>(10); Vector o=s; o.add(new Object()); Object p=s.get(0); Obviously this gives an unchecked warning about...

Java Generics, Type Erasure and Frameworks

I have started looking at Java Generics recently. At first, what I struck me as valuable was that type parameters allow one to express constraints that cannot (or not easily)...


All times are GMT. The time now is 08:28 PM. | Privacy Policy