TUM CCSM Commons

edu.tum.cs.commons.reflect
Class GenericTypeResolver

java.lang.Object
  extended by edu.tum.cs.commons.reflect.GenericTypeResolver

public class GenericTypeResolver
extends java.lang.Object

This is a class for helping with the resolution of generic types in the context of reflection. Unfortunately there is no easy way to get the actual return type or parameter type of a method if some generic class is within the class hierarchy. This class handles the messy aspects of this.

The instances of this class are bound to single classes, for which they are constructed. They only work correctly when querying parameters originating from this class or one of its methods. Furthermore this class does not work, if the class the lookup is performed for is generic itself.

The error handling of this class is rather crude. If any of the assumptions (either specified above or we learned from playing with reflection) is not met, an exception is thrown (currently IllegalStateException).

Version:
$Rev: 26268 $
Author:
Benjamin Hummel, $Author: juergens $
Rating:
GREEN Hash: 4364A48643065AF4C13524C2A465E19B

Constructor Summary
GenericTypeResolver(java.lang.Class<?> clazz)
          Creates a new generic type resolver for the given class.
 
Method Summary
 java.lang.Class<?> resolveGenericType(java.lang.reflect.Type genericType)
          Returns the actual type from a (potentially) generic type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericTypeResolver

public GenericTypeResolver(java.lang.Class<?> clazz)
Creates a new generic type resolver for the given class.

Throws:
java.lang.IllegalArgumentException - if called for generic class.
Method Detail

resolveGenericType

public java.lang.Class<?> resolveGenericType(java.lang.reflect.Type genericType)
Returns the actual type from a (potentially) generic type. If the argument is a plain class, it is returned, otherwise a lookup in the internal generic parameter map is performed. For parameterized types (e.g. List<String> the raw type (here: List) is returned.

Note that this only works for return values and parameters of methods belonging to the class for which this instance was constructed for. Otherwise the behaviour is underfined (either returning nonsense or throwing an exception).

Parameters:
genericType - a type such as returned from Method.getGenericReturnType() or Method.getGenericParameterTypes().

TUM CCSM Commons

TUM CCSM Commons - 2.7