Reflection and access to type parameter?  
Author Message
Jim Garrison





PostPosted: 2008-5-23 4:28:00 Top

java-programmer, Reflection and access to type parameter? Given the SSCCE below, the need to pass A.class and B.class in
lines 25 and 25 seems redundant. However, I can find nothing in
the language that would let that be done in the constructor,
between lines 18 and 19. The obvious would be

Class<T> x = T.class;

but that of course does not work. Is there any bridge at all
between reflection and generics? I suspect the answer is no
and the code below is the best that can be done, but I'm
not sure.

1 import java.lang.reflect.Method;
2 public class TestEnums
3 {
4 public static enum A
5 {
6 V1,
7 V2;
8 }
9 public static enum B
10 {
11 X1,
12 X2,
13 X3;
14 }
15 public static class C<T extends Enum<?>>
16 {
17 public C(Class<T> x) throws Exception
18 {
19 T[] eVal = x.getEnumConstants();
20 for (Enum<?> v : eVal) System.out.println(v.toString());
21 }
22 }
23 public static void main(String[] args) throws Exception
24 {
25 C<A> ca = new C<A>(A.class);
26 C<B> cb = new C<B>(B.class);
27 }
28 }
 
Kenneth P. Turvey





PostPosted: 2008-5-23 5:34:00 Top

java-programmer >> Reflection and access to type parameter? On Thu, 22 May 2008 15:28:12 -0500, Jim Garrison wrote:

> Class<T> x = T.class;
>
> but that of course does not work. Is there any bridge at all between
> reflection and generics? I suspect the answer is no and the code below
> is the best that can be done, but I'm not sure.

The type is completely gone by the time the program is running. There is
no way to retrieve it.

I haven't had a chance to look at the code you provided, but if you are
trying to extract the generic type, you will fail.


--
Kenneth P. Turvey <email***@***.com>
 
Owen Jacobson





PostPosted: 2008-5-23 7:47:00 Top

java-programmer >> Reflection and access to type parameter? On May 22, 4:28็ˆŒm, Jim Garrison <email***@***.com> wrote:
> Given the SSCCE below, the need to pass A.class and B.class in
> lines 25 and 25 seems redundant. However, I can find nothing in
> the language that would let that be done in the constructor,
> between lines 18 and 19. ็‡ญhe obvious would be
>
> ???Class<T> x = T.class;
>
> but that of course does not work. ็‡Ÿs there any bridge at all
> between reflection and generics? ็‡Ÿ suspect the answer is no
> and the code below is the best that can be done, but I'm
> not sure.
>
> ?1 import java.lang.reflect.Method;
> ?2 public class TestEnums
> ?3 {
> ?4 ??public static enum A
> ?5 ??{
> ?6 ????V1,
> ?7 ????V2;
> ?8 ??}
> ?9 ??public static enum B
> 10 ??{
> 11 ????X1,
> 12 ????X2,
> 13 ????X3;
> 14 ??}
> 15 ??public static class C<T extends Enum<?>>
> 16 ??{
> 17 ????public C(Class<T> x) throws Exception
> 18 ????{
> 19 ??????T[] eVal = x.getEnumConstants();
> 20 ??????for (Enum<?> v : eVal) System.out.println(v.toString());
> 21 ????}
> 22 ??}
> 23 ??public static void main(String[] args) throws Exception
> 24 ??{
> 25 ????C<A> ca = new C<A>(A.class);
> 26 ????C<B> cb = new C<B>(B.class);
> 27 ??}
> 28 }

You can, at least, refactor some of the repetition:

public static <T> C<T> createC (Class<T> clazz) {
return new C<T>(clazz);
}

...
C<A> = createC(A.class);
C<B> = createC(B.class);
...

Java allows type parameters to be inferred in certain contexts.
Unfortunately, constructor invocation isn't one of them...

-o