org.spicefactory.cinnamon.reflect
Class TypeContext

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.TypeContext

public class TypeContext
extends Object

A TypeContext instance reflects on a given supertype from the perspective of a subtype. This is useful if you have parameterized types and want to easily resolve their TypeVariables from the perspective of a subtype. Example: If you have a type MyClass<E> with a method public void doSomethingWith (E element) and then create a subtype MySubclass extends MyClass<String> without overriding the method and want to reflect on this method, you will still get the TypeVariable E and not String as the genericParameter. TypeContext instances offer convenience methods for resolving these TypeVariables. Each TypeContext instance is defined with a (probably parameterized) base type (in the example above MyClass) and a target type (in the example above MySubclass). The target type serves as the "perspective" from which all TypeVariables of the base type will be resolved.

Author:
Jens Halm

Method Summary
 Object convert(Object value)
          Converts (if necessary) the given object to an instance of the target type of this TypeContext.
 Class<?> getBaseType()
          Returns the base type for this TypeContext.
 Type getGenericTargetType()
          Returns the generic target type for this TypeContext.
 Class<?> getRawTargetType()
          Returns the raw target type for this TypeContext.
 boolean isAssignableType(Object value)
          Checks if the given object is an instance of the target type of this TypeContext instance.
 boolean isConvertibleType(Object value)
          Checks if the given object can be converted (if necessary) to an instance of the target type of this TypeContext.
 boolean isEqualType(Object value)
          Checks if the class of the given object equals the raw target type of this TypeContext.
 Type resolve(Type type)
          Resolves all TypeVariables contained in the given Type from the perspective of the target type of this TypeVariable.
 Type resolveTypeVariable(int typeVariableIndex)
          Resolves the TypeVariable at the given index of the base type declaration of this TypeContext from the perspective of the target type.
 Type resolveTypeVariable(String typeVariableName)
          Resolves the specified TypeVariable of the base type of this TypeContext from the perspective of the target type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getBaseType

public Class<?> getBaseType()
Returns the base type for this TypeContext.

Returns:
the base type for this TypeContext

getGenericTargetType

public Type getGenericTargetType()
Returns the generic target type for this TypeContext. The class returned by getBaseType is always assignable from the raw type of this target type. The target type serves as the "perspective" from which all TypeVariables of the base type will be resolved

Returns:
the generic target type for this TypeContext

getRawTargetType

public Class<?> getRawTargetType()
Returns the raw target type for this TypeContext. For a ParameterizedType this will be the Class returned by ParameterizedType.getRawType. For a TypeVariable it will be the raw type of the Type returned by getBounds. An instance of Class will be returned unchanged.

Returns:
the raw target type for this TypeContext

resolveTypeVariable

public Type resolveTypeVariable(String typeVariableName)
Resolves the specified TypeVariable of the base type of this TypeContext from the perspective of the target type. Example: If the base type is List<E> and the target type is MyList extends ArrayList<Integer> then resolveTypeVariable("E") will return Integer.

Parameters:
typeVariableName - the name of the TypeVariable of the parameterized base type
Returns:
the resolved TypeVariable from the perspective of the target type

resolveTypeVariable

public Type resolveTypeVariable(int typeVariableIndex)
Resolves the TypeVariable at the given index of the base type declaration of this TypeContext from the perspective of the target type. Example: If the base type is Map<K,V> and the target type is MyMap implements Map<String, File> then resolveTypeVariable(1) will return File.

Parameters:
typeVariableIndex - the index of the TypeVariable of the parameterized base type
Returns:
the resolved TypeVariable from the perspective of the target type

resolve

public Type resolve(Type type)
Resolves all TypeVariables contained in the given Type from the perspective of the target type of this TypeVariable. The Type instance passed to this method must have been obtained through reflection on methods or fields of the base type of this TypeContext. Example: If the base type is MyClass<E> and the target type is MySubclass extends MyClass and the Type passed to this method is List<E> (presumably obtained through reflecting on a method of MyClass like public void setElements (List<E> elements) then this method would return List<String> as the resolved type.

Parameters:
type - a Type instance obtained through reflection on the base type of this TypeContext
Returns:
the resolved Type from the perspective of the target type

isEqualType

public boolean isEqualType(Object value)
Checks if the class of the given object equals the raw target type of this TypeContext.

Parameters:
value - the instance to be checked
Returns:
true if the class of the given object equals the raw target type of this TypeContext

isAssignableType

public boolean isAssignableType(Object value)
Checks if the given object is an instance of the target type of this TypeContext instance.

Parameters:
value - the instance to be checked
Returns:
true if the given object is an instance of the target type of this TypeContext instance

isConvertibleType

public boolean isConvertibleType(Object value)
Checks if the given object can be converted (if necessary) to an instance of the target type of this TypeContext. Any requred Converter instances must be registered with the ReflectionFactory that created this TypeContext instance.

Parameters:
value - the instance to be checked
Returns:
true if the given object can be converted to an instance of the target type.

convert

public Object convert(Object value)
Converts (if necessary) the given object to an instance of the target type of this TypeContext. Any requred Converter instances must be registered with the ReflectionFactory that created this TypeContext instance.

Parameters:
value - instance that should become an instance of the target type of this TypeContext
Returns:
the converted value