0

I would like to be able to detirmine the return type of my method call at runtime, but I can not seem to be able to get the Type of T.

    public <T> T getT()
    {
        Object t = null;
        Class<?> c = t.getClass();
        System.out.println(c.getName());
        return (T) t;
    }

Is there any way to determine the Type of T at runtime in Java?

Milhous
  • 14,473
  • 16
  • 63
  • 82
  • take a look at this explanation of *erasure*:http://stackoverflow.com/questions/313584/what-is-the-concept-of-erasure-in-generics-in-java/313590#313590 (i could have put this in as an answer and attempted to get points using Jon Skeet! – akf Jul 27 '09 at 01:53
  • your second line of the method will throw a NullPointerException. – akf Jul 27 '09 at 01:54
  • akf: I think the point is that the questioner would like code that works, rather than his current attempts that he or she recognises doesn't. (And the point that T might not have a "specific" type is neither here nor there.) – Tom Hawtin - tackline Jul 27 '09 at 14:38

3 Answers3

5

Your function will throw a NullPointerException, because you call "getClass" on a null pointer (since t is initialized with null). Additionally, generics are used for giving added compile-time type-checking. They do not give you anything special at runtime; generics simply use type Object, but cause the code which uses the generic object to perform implicit casts and also causes the compiler to be aware of how you will use it.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • A lot of the 'suckiness' of Java generics happened because the design pragmatically had to be backwards compatible with the JDK 1.4 and earlier libraries. – Stephen C Jul 27 '09 at 03:36
1

Java generics are a static type checking feature. Attempting to retrieve reflection artifacts from generic parameters is typical of poorly thought out design.

In the question example, there is no guarantee that T is a class or even interface. For example

List<? extends Frogs> list = thing.getT();

If you really want to go down this path (and I strongly suggest you don't, not that I expect you to take any notice), then you can supply a reflection object that is statically related to the generic parameter as an argument:

 public <T> T getT(Class<T> clazz) {
     Object value = map.get(clazz);
     return clazz.cast(value);
 }
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • In my current Design, I do exactcly that, but I end up having to pass the Class,when I would really like it to know the type given the return type. – Milhous Jul 27 '09 at 02:33
  • There isn't necessarily an applicable `Class` object, even if he compile time feature was available at runtime. – Tom Hawtin - tackline Jul 27 '09 at 03:15
0

If you have a generic Class you can write a constructor that takes the type and saves it into a member of your class. This way you can check the Type during runtime. All information that are only in the generics are gone after compiling.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • I am working on a database CRUD project, and it has many types, so this solution will not work :-( – Milhous Jul 27 '09 at 02:32