net.sf.mmm.util.reflect.api
Interface GenericType<T>

Type Parameters:
T - is the generic type of the retrieval class.
All Superinterfaces:
Type
All Known Implementing Classes:
AbstractGenericType, GenericTypeImpl, SimpleGenericTypeImpl

public interface GenericType<T>
extends Type

This is the interface of a generic type and allows simple and powerful access to the complex generic type-system introduced in Java5.
It represents a Type (available via getType()) but allows easy access to resolve the actual erasure Class for assignment and retrieval. This includes resolving TypeVariables as far as possible.
Have a look at the following example:

 public class Foo<A, B> { 
   A getA() { ... } 
   B getB() { ... } 
 }
 public class Bar<X> extends Foo<X, String> { 
   ... 
 }
 public class Some extends Bar<Long> { 
   ... 
 }
 
If you want to determine the type of Some.getA() reflectively, you will have to dive into the deepest and trickiest part of the reflection API and might step into one of the many pitfalls on this way. All this is solved for you, if you use what is offered via this API.
LIMITATIONS:
This solution will only support one upper and one lower bound but NOT multiple bounds of the same kind. However this is more a feature than a limitation as it makes the usage simple and IMHO using multiple bounds is a quite uncommon feature that should be avoided.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
GenericTypeFactory.createGenericType(Type), GenericTypeFactory.createGenericType(Type, GenericType)

Field Summary
static GenericType<?>[] NO_TYPES
          An empty GenericType-array.
 
Method Summary
 Class<? extends T> getAssignmentClass()
          This method gets the Class to be used for assignment (as parameter) of a value of this GenericType.
 GenericType<?> getComponentType()
          This method gets the component-type of this GenericType if it represents an array, Collection or Map.
 GenericType<?> getKeyType()
          This method gets the key-type of this GenericType if it represents a Map.
 Class<T> getRetrievalClass()
          This method gets the Class to be used for retrieval (the return-type) of a value of this GenericType.
 Type getType()
          This method gets the Type represented by this GenericType.
 GenericType<?> getTypeArgument(int index)
          This method gets the type-argument at the given index.
 int getTypeArgumentCount()
          This method gets the number of type-arguments .
 boolean isAssignableFrom(GenericType<?> subType)
          This method determines if this GenericType is equal to or a super-type of the given subType.
 String toString()
          This method gets the string representation of this GenericType.
 

Field Detail

NO_TYPES

static final GenericType<?>[] NO_TYPES
An empty GenericType-array.

Method Detail

getAssignmentClass

Class<? extends T> getAssignmentClass()
This method gets the Class to be used for assignment (as parameter) of a value of this GenericType.
It will only differ from the retrieval-class if this GenericType is a WildcardType.
Unlike the lower-bound, the assignment-class is never null. If there is no lower-bound, the assignment-class is the same as the retrieval-class. Therefore the assignment-class is always equal or more specific to the retrieval-class.
Here are some examples:
Type getAssignmentClass()
<? super Integer> Integer
<? extends CharSequence> CharSequence
String String

Returns:
the Class that is the lower bound.

getRetrievalClass

Class<T> getRetrievalClass()
This method gets the Class to be used for retrieval (the return-type) of a value of this GenericType.
It will only differ from the assignment-class if this GenericType is a WildcardType.
The retrieval-class is the upper-bound, however for usability and simplicity only one bound is supported.
Here are some examples:
Type getAssignmentClass()
<? super Integer> Object
<? extends CharSequence> CharSequence
String String

Returns:
the Class that is the upper bound.

getType

Type getType()
This method gets the Type represented by this GenericType.

Returns:
the value-type.
See Also:
Class.getGenericSuperclass(), Class.getGenericInterfaces(), Method.getGenericReturnType(), Method.getGenericParameterTypes(), Field.getGenericType(), Constructor.getGenericParameterTypes()

getComponentType

GenericType<?> getComponentType()
This method gets the component-type of this GenericType if it represents an array, Collection or Map.
Here are some examples:
type getComponentType()
List<Map<String, Long>> Map<String, Long>
List Object
Foo[] Foo
Foo<Bar>[] Foo<Bar>
Foo<Bar> null
Map<String, Long> Long

Returns:
the component-type of this GenericType or null if this GenericType does NOT represent an array, Collection or Map.

getKeyType

GenericType<?> getKeyType()
This method gets the key-type of this GenericType if it represents a Map.
Here are some examples:
type getComponentType()
List<Map<String, Long>> null
Map Object
Foo[] null
Foo<K,V> null
Map<String, Long> String

Returns:
the key-type of this GenericType or null if this GenericType does NOT represent a Map .
Since:
2.0.0

getTypeArgumentCount

int getTypeArgumentCount()
This method gets the number of type-arguments .

Returns:
the type-argument count.

getTypeArgument

GenericType<?> getTypeArgument(int index)
This method gets the type-argument at the given index.
E.g. for the GenericType representing Map<String, List<Integer>> this method would return String for an index of 0 and List<Integer> for an index of 1.

Parameters:
index - is the position of the requested type-argument. It has to be in the range from 0 to getTypeArgumentCount() - 1.
Returns:
the type-argument at the given index.
See Also:
getTypeArgumentCount(), ParameterizedType.getActualTypeArguments()

isAssignableFrom

boolean isAssignableFrom(GenericType<?> subType)
This method determines if this GenericType is equal to or a super-type of the given subType.
If X.isAssignableFrom(Y) is true, then an instance of Y can be casted to X.
NOTE:
In case of strange and deeply cascaded generic constructs this can be an expensive operation with many recursive invocations.

Parameters:
subType - is the potential sub-type of this GenericType.
Returns:
true if objects of the type subType can be assigned to this GenericType.
See Also:
Class.isAssignableFrom(Class)

toString

String toString()
This method gets the string representation of this GenericType. In case the underlying value-type is a regular Class, this method will return its qualified name otherwise it will return the string representation of the generic type information (e.g. java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>[] ).

Overrides:
toString in class Object
Returns:
this GenericType as string.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.