Dobrý den,

1)doporučuji prostudovat:
http://download.oracle.com/javase/tutorial/java/generics/gentypes.html

It's important to understand that type variables are not actually types themselves. In the above examples, you won't find T.java or T.class anywhere on the file system. Furthermore, T is not a part of the Box class name. In fact during compilation, all generic information will be removed entirely, leaving only Box.class on the file system. We'll discuss this later in the section on Type Erasure.

Přímo to tedy nelze.

2) Zamýšleného chování se dá dosáhnout přes java.lang.reflect.ParameterizedType. Dobré počtení je třeba:
http://www.artima.com/weblogs/viewpost.jsp?thread=208860

3) Hotové řešení na principu ad 2) lze najít ve Spring 3
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/GenericTypeResolver.html#resolveTypeArguments(java.lang.Class,%20java.lang.Class)

S pozdravem
Jiří Havelka

Dne 12.6.2011 12:21, Libor Jelinek napsal(a):
Hezké nedělní poledne!
Možná jsem málo studoval genericitu, ale proč není možné použít T.class
(a T.class.getName() apod.) v tomto případě?

public class NewClass<T> {

     public NewClass() {
          System.out.println("Zavolali jste mě s typem " +
T.class.getName());
     }

     public static void main(String[] args) {
         new NewClass<String>();
         new NewClass<Integer>();
     }
}

NetBeans varuje hláškou: "Cannot select from a type variable".

Pokus o překlad dopadne chybou:

Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: <any>
     at beantablemodel.NewClass.<init>(NewClass.java:14)
     at beantablemodel.NewClass.main(NewClass.java:18)
Java Result: 1

Vždyť to je známo jaký objekt jsem dodal (new NewClass<String>()) a na
každý objekt je možné použít .class...

Díky za odpovědi!
Libor

Odpovedet emailem