org.beanfabrics.util
Class GenericType

java.lang.Object
  extended by org.beanfabrics.util.GenericType

public class GenericType
extends java.lang.Object

The GenericType is a facade for a Type object representing a Java class. It provides convenient functions for generic type reflection.


Constructor Summary
GenericType(java.lang.Class<?> cls)
          Creates a GenericType for the given Class.
 
Method Summary
 java.lang.Class<?> asClass()
          Returns the class that this type represents.
 GenericType getFieldType(java.lang.String fieldname)
          Returns the type of the field with the given name as a GenericType.
 GenericType getMethodReturnType(java.lang.String methodName)
          Returns the return type of the method with the given name (having no parameters) as a GenericType.
 java.lang.reflect.Type getType()
          Returns the type represented by this GenericType.
 GenericType getTypeParameter(java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
          Returns the value of the given TypeVariable as a GenericType.
 boolean isClass()
          Returns true if this type represents a Class.
 boolean isParameterizedType()
          Returns true if this type represents a ParameterizedType.
 boolean isTypeVariable()
          Returns true if this type represents a TypeVariable.
 boolean isWildcardType()
          Returns true if this type represents a WildcardType.
 java.lang.reflect.Type narrow(java.lang.reflect.Type aType, java.lang.Class<?> targetType)
           
 java.lang.reflect.Type tryResolve(java.lang.reflect.Type type)
           
 java.lang.reflect.Type tryResolve(java.lang.reflect.TypeVariable<?> var)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericType

public GenericType(java.lang.Class<?> cls)
Creates a GenericType for the given Class.

Parameters:
cls -
Method Detail

getType

public java.lang.reflect.Type getType()
Returns the type represented by this GenericType. This can be one of Class, ParameterizedType, and TypeVariable.

Returns:
the type represented by this GenericType.

isTypeVariable

public boolean isTypeVariable()
Returns true if this type represents a TypeVariable.

Returns:
true if this type represents a TypeVariable

isClass

public boolean isClass()
Returns true if this type represents a Class.

Returns:
true if this type represents a Class

asClass

public java.lang.Class<?> asClass()
                           throws java.lang.IllegalStateException
Returns the class that this type represents.

Returns:
the class this type represents
Throws:
java.lang.IllegalStateException - if this type does not represent a class

isParameterizedType

public boolean isParameterizedType()
Returns true if this type represents a ParameterizedType.

Returns:
true if this type represents a ParameterizedType

isWildcardType

public boolean isWildcardType()
Returns true if this type represents a WildcardType.

Returns:
true if this type represents a WildcardType

getTypeParameter

public GenericType getTypeParameter(java.lang.reflect.TypeVariable<? extends java.lang.Class<?>> variable)
                             throws java.lang.IllegalArgumentException,
                                    java.lang.IllegalStateException
Returns the value of the given TypeVariable as a GenericType.

Parameters:
variable - must parameterize this type or one of it's super types
Returns:
the actual value of the given TypeVariable as a GenericType
Throws:
java.lang.IllegalArgumentException - if the variable does not parameterize this type
java.lang.UnsupportedOperationException - if the type parameter resolution is not supported for this type
java.lang.IllegalStateException

getFieldType

public GenericType getFieldType(java.lang.String fieldname)
Returns the type of the field with the given name as a GenericType.

Parameters:
fieldname - must denote a field in this type or one of it's super types.
Returns:
the type of the field with the given name as a GenericType
Throws:
java.lang.UnsupportedOperationException - if field type resolution is not supported for this type

getMethodReturnType

public GenericType getMethodReturnType(java.lang.String methodName)
Returns the return type of the method with the given name (having no parameters) as a GenericType.

Please note:
Only methods without parameters are supported right now!

Parameters:
methodName -
Returns:
the return type of the method as a GenericType.
Throws:
java.lang.UnsupportedOperationException - if method return type resolution is not supported for this type

tryResolve

public java.lang.reflect.Type tryResolve(java.lang.reflect.Type type)

tryResolve

public java.lang.reflect.Type tryResolve(java.lang.reflect.TypeVariable<?> var)

narrow

public java.lang.reflect.Type narrow(java.lang.reflect.Type aType,
                                     java.lang.Class<?> targetType)