org.javaruntype.type
Class Type<T>

Object
  extended by org.javaruntype.type.Type<T>
All Implemented Interfaces:
Serializable

public final class Type<T>
extends Object
implements Serializable

Basic class of the type system. A Type object representes a Java type including its type parameters (if it has any), as specified by the TypeDef corresponding to the type's component class.

Every possible Java type can be represented by a Type object. Some examples:

Objects of this class are never created directly. To obtain a Type object, the diverse methods in the Types class should be used.

Objects of this class are immutable, and thus thread-safe. Also, in order to avoid excessive memory usage, an internal synchronized cache exists which prevents the same Type from being instantiated more than once (so, if two Type objects are equal, this will mean that they are the same object).

Since:
1.0
Author:
Daniel Fernández
See Also:
Serialized Form

Method Summary
 boolean equals(Object obj)
           
 Set<Type<?>> getAllTypesAssignableFromThis()
           Returns a set of the types corresponding to all the interfaces and superclasses that this type implements or extends.
 int getArrayDimensions()
           Returns the type's array dimensions.
 Class<?> getComponentClass()
           Returns the type's component class.
 String getName()
           Returns the type's name.
 Class<? super T> getRawClass()
           Returns the type's raw equivalent class representation.
 Type<?> getRawEquivalent()
           Returns a type corresponding with the one on which this method is called, but substituting all its type parameters by "unknown".
 String getSimpleName()
           Returns the type's name excluding package.
 List<TypeParameter<?>> getTypeParameters()
           Returns the type parameters of the type.
 int hashCode()
           
 boolean isAbstract()
           Returns whether the type is abstract or not.
 boolean isArray()
           Returns whether the type is an array or not.
 boolean isAssignableFrom(Type<?> type)
           Returns whether type type is assignable from the type that is passed as a parameter.
 boolean isInterface()
           Returns whether the type represents an interface or not.
 boolean isRaw()
           Returns whether this type would be considered "raw".
 Object newInstance()
           Creates a new instance of the type.
 String toString()
           
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getComponentClass

public Class<?> getComponentClass()

Returns the type's component class. For instance, it will return List.class for "List<String>[]" (and not List[].class, which would be returned by getRawClass()).

Returns:
the component class.

getArrayDimensions

public int getArrayDimensions()

Returns the type's array dimensions. For instance, it will return 1 for "List<String>[]"

Returns:
the array dimensions

isArray

public boolean isArray()

Returns whether the type is an array or not. For instance, it will return "true" for "List<String>[]".

Returns:
whether the type is an array or not.

isInterface

public boolean isInterface()

Returns whether the type represents an interface or not. For instance, it will return "false" for "List<String>[]" as, although List.class is an interface, List[].class is a instantiable class. On the contrary, "List<String>" would return "true".

Returns:
whether the type represents an interface.

isAbstract

public boolean isAbstract()

Returns whether the type is abstract or not. For instance, it will return "false" for "AbstractList<String>[]" as, although AbstractList.class is abstract, AbstractList[] is an instantiable class. On the contrary, "AbstractList<String>" would return "true".

Returns:
whether the type is abstract or not.

getTypeParameters

public List<TypeParameter<?>> getTypeParameters()

Returns the type parameters of the type.

Returns:
the type parameters.

getName

public String getName()

Returns the type's name.

Returns:
the type's name.

getSimpleName

public String getSimpleName()

Returns the type's name excluding package.

Returns:
the type's simepl name.

getRawClass

public Class<? super T> getRawClass()

Returns the type's raw equivalent class representation. For instance, if the type is "List<String>[]", this method will return List[].class (the class representing the raw version of the type), in contrast with getComponentClass() which would return List.class (the component class).

Returns:
the type's raw equivalent class representation

isAssignableFrom

public boolean isAssignableFrom(Type<?> type)

Returns whether type type is assignable from the type that is passed as a parameter.

Assignability is computed in the following terms: a type A is considered to be assignable from another type B if a method declared as receiving a parameter of type A can be called with an object of type B as a parameter.

Parameters:
type - the type on which assignability is going to be tested
Returns:
whether the type is assignable from the parameter type

newInstance

public Object newInstance()
                   throws InstantiationException,
                          IllegalAccessException

Creates a new instance of the type.

The returned instances are created using the default (no-arg) constructor if this Type does not represent an array. On the contrary, if this Type represents an array, an array object of zero-size dimensions is returned (like "String[0][0][0]").

Returns:
an object created with the default constructor or a zero-dimensions array
Throws:
InstantiationException - if the type cannot be instantiated
IllegalAccessException - if the type cannot be instantiated

getAllTypesAssignableFromThis

public Set<Type<?>> getAllTypesAssignableFromThis()

Returns a set of the types corresponding to all the interfaces and superclasses that this type implements or extends.

For instance, for "List<String>", this method will return "Collection<String>", "Iterable<String>" and "Object".

Returns:
the set of implemented interfaces and extended superclass types.

getRawEquivalent

public Type<?> getRawEquivalent()

Returns a type corresponding with the one on which this method is called, but substituting all its type parameters by "unknown". For instance: "List<String>" -> "List<?>".

Returns:
the corresponding raw type

isRaw

public boolean isRaw()

Returns whether this type would be considered "raw". A type is raw if the result of calling getRawEquivalent() on it is the type itself.

Returns:
whether the type is raw or not

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2012 The JAVARUNTYPE team. All Rights Reserved.