Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.reflect
Class InstantiableTypeLiteral<T>

java.lang.Object
  extended by dk.rode.thesis.meta.reflect.TypeLiteral<T>
      extended by dk.rode.thesis.meta.reflect.InstantiableTypeLiteral<T>
Type Parameters:
T - The type represented by this instantiable type literal.
Direct Known Subclasses:
Factory

public abstract class InstantiableTypeLiteral<T>
extends TypeLiteral<T>

Represents the instantiable type realised as the type parameter T, where T may be a generic type.

New instances of T can be created using the newInstance(Object...) method. Hence, interfaces and abstract classes cannot be used as the raw type of T.

The constructor to create new instances of T is fetched ignoring primitive types, e.g. a formal parameter type of Integer will match both Integer and int.

Author:
Gunni Rode / rode.dk
See Also:
TypeLiteral

Field Summary
private  Constructor<?> constructor
          The constructor used to create new instances of the type represented by the type parameter T.
 
Fields inherited from class dk.rode.thesis.meta.reflect.TypeLiteral
type
 
Constructor Summary
protected InstantiableTypeLiteral(Class<?>... parameterTypes)
          Constructor, which creates this instantiable type literal to represent the type realised by the type parameter T.
  InstantiableTypeLiteral(InstantiableTypeLiteral<T> typeLiteral)
          Copy constructor, which creates this instantiable type literal based on the generic type and constructor represented by typeLiteral
private InstantiableTypeLiteral(TypeLiteral<T> typeLiteral, Class<?>... parameterTypes)
          Constructor, which creates this instantiable type literal to represent the type represented by the type literal typeLiteral.
 
Method Summary
static
<S> InstantiableTypeLiteral<S>
create(Class<S> clazz, Class<?>... parameterTypes)
          Returns an instantiable type literal representing the type S supplied via the class literal clazz.
static InstantiableTypeLiteral<?> create(Type type, Class<?>... parameterTypes)
          Returns a instantiable type literal representing the type supplied as type.
static
<S> InstantiableTypeLiteral<S>
create(TypeLiteral<S> typeLiteral, Class<?>... parameterTypes)
          Returns an instantiable type literal representing the type S supplied via the type literal type.
 boolean equals(Object object)
          Returns true of object is an instantiable type literal representing the same type and constructor as this instantiable type literal, false if not.
 Constructor<?> getConstructor()
          Return the constructor represented by this instantiable type literal.
private static Constructor<?> getConstructor(Type type, Class<?>... parameterTypes)
          Returns the constructor having the formal parameter types supplied as parameterTypes in the raw type fetched from type.
static boolean isInstatiable(Type type)
          Returns true if the raw type represented by type is instantiable, false if not.
static boolean isInstatiable(TypeLiteral<?> type)
          Returns true if the raw type represented by type is instantiable, false if not.
 T newInstance(Object... arguments)
          Returns a new instance of the type represented by this instantiable type literal.
 
Methods inherited from class dk.rode.thesis.meta.reflect.TypeLiteral
asClass, asClass, asType, create, create, create, getComponentType, getRawType, getRawType, getType, hashCode, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

constructor

private final Constructor<?> constructor
The constructor used to create new instances of the type represented by the type parameter T.

The constructor is identified by its formal parameter types supplied at construction time.

Never null.

Constructor Detail

InstantiableTypeLiteral

protected InstantiableTypeLiteral(Class<?>... parameterTypes)
                           throws NoSuchMethodException
Constructor, which creates this instantiable type literal to represent the type realised by the type parameter T.

New instances of T can be created using the declared constructor having the formal parameter types supplied parameterTypes.

The constructor is made accessible if not already.

Parameters:
parameterTypes - The formal parameter types for the declared constructor to use.
Throws:
IllegalArgumentException - If this class is not generic, if T has no raw type, or if the raw raw represents a non instantiable class.
NoSuchMethodException - If no such constructor is declared in the raw type of T.

InstantiableTypeLiteral

public InstantiableTypeLiteral(InstantiableTypeLiteral<T> typeLiteral)
Copy constructor, which creates this instantiable type literal based on the generic type and constructor represented by typeLiteral

Parameters:
typeLiteral - The instantiable type literal to copy; cannot be null.
Throws:
NullPointerException - If typeLiteral is null.

InstantiableTypeLiteral

private InstantiableTypeLiteral(TypeLiteral<T> typeLiteral,
                                Class<?>... parameterTypes)
                         throws NoSuchMethodException
Constructor, which creates this instantiable type literal to represent the type represented by the type literal typeLiteral.

New instances of T can be created using the declared constructor having the formal parameter types supplied parameterTypes.

The constructor is made accessible if not already.

Parameters:
typeLiteral - The type literal; cannot be null.
parameterTypes - The formal parameter types for the declared constructor to use.
Throws:
IllegalArgumentException - If this class is not generic, if T has no raw type, or if the raw raw represents a non instantiable class.
NoSuchMethodException - If no such constructor is declared in the raw type of T.
Method Detail

create

public static <S> InstantiableTypeLiteral<S> create(Class<S> clazz,
                                                    Class<?>... parameterTypes)
                                         throws NoSuchMethodException
Returns an instantiable type literal representing the type S supplied via the class literal clazz.

The declared constructor to use is identified by the formal parameter types supplied as parameterTypes. The constructor is made accessible if not already.

Type Parameters:
S - The type.
Parameters:
clazz - The class literal representing the type; cannot be null.
parameterTypes - The formal parameter types for the declared constructor to use.
Returns:
A instantiable type literal representing the type S; never null.
Throws:
NullPointerException - If clazz is null.
IllegalArgumentException - If S is a non-instantiable type.
NoSuchMethodException - If no such constructor is declared in the raw type of S.
See Also:
create(Type, Class...), create(TypeLiteral, Class...), isInstatiable(Type)

create

public static InstantiableTypeLiteral<?> create(Type type,
                                                Class<?>... parameterTypes)
                                         throws NoSuchMethodException
Returns a instantiable type literal representing the type supplied as type. The declared constructor to use is identified by the formal parameter types supplied as parameterTypes. The constructor is made accessible if not already.

Parameters:
type - The type; cannot be null.
parameterTypes - The formal parameter types for the declared constructor to use.
Returns:
A instantiable type literal representing the type; never null.
Throws:
NullPointerException - If type is null.
IllegalArgumentException - If type represents a non-instantiable type.
NoSuchMethodException - If no such constructor is declared in the raw type of type.
See Also:
create(Class, Class...), create(TypeLiteral, Class...), isInstatiable(Type)

create

public static <S> InstantiableTypeLiteral<S> create(TypeLiteral<S> typeLiteral,
                                                    Class<?>... parameterTypes)
                                         throws NoSuchMethodException
Returns an instantiable type literal representing the type S supplied via the type literal type.

The declared constructor to use is identified by the formal parameter types supplied as parameterTypes. The constructor is made accessible if not already.

Type Parameters:
S - The type.
Parameters:
typeLiteral - The type literal representing the type; cannot be null.
parameterTypes - The formal parameter types for the declared constructor to use.
Returns:
A instantiable type literal representing the type S; never null.
Throws:
NullPointerException - If typeLiteral is null.
IllegalArgumentException - If typeLiteral represents a non-instantiable type.
NoSuchMethodException - If no such constructor is declared in the raw type of S.
See Also:
create(Class, Class...), create(Type, Class...), isInstatiable(TypeLiteral)

equals

public boolean equals(Object object)
Returns true of object is an instantiable type literal representing the same type and constructor as this instantiable type literal, false if not.

Overrides:
equals in class TypeLiteral<T>
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

getConstructor

public Constructor<?> getConstructor()
Return the constructor represented by this instantiable type literal.

Returns:
The constructor; never null.
See Also:
TypeLiteral.getType()

getConstructor

private static final Constructor<?> getConstructor(Type type,
                                                   Class<?>... parameterTypes)
                                            throws NoSuchMethodException
Returns the constructor having the formal parameter types supplied as parameterTypes in the raw type fetched from type.

The constructor is made accessible if not already.

Parameters:
type - The type; cannot be null.
parameterTypes - The formal parameter types.
Returns:
The constructor; never null.
Throws:
NullPointerException - If type is null.
IllegalArgumentException - If T has no raw type, or if the raw type represents an interface or abstract class.
NoSuchMethodException - If no such constructor is declared in the raw type.
See Also:
Reflection.getConstructor(Class, Class...)

isInstatiable

public static boolean isInstatiable(Type type)
Returns true if the raw type represented by type is instantiable, false if not.

If no raw exist based on type or if the raw type is abstract or an interface, false is returned. Otherwise type. This method does not test for explicit constructors.

Parameters:
type - The type to test; cannot be null.
Returns:
True if instantiable, false if not.
Throws:
NullPointerException - If type is null.

isInstatiable

public static boolean isInstatiable(TypeLiteral<?> type)
Returns true if the raw type represented by type is instantiable, false if not.

If no raw exist based on type or if the raw type is abstract or an interface, false is returned. Otherwise type. This method does not test for explicit constructors.

Parameters:
type - The type to test; cannot be null.
Returns:
True if instantiable, false if not.
Throws:
NullPointerException - If type is null.

newInstance

public T newInstance(Object... arguments)
              throws Exception
Returns a new instance of the type represented by this instantiable type literal.

Parameters:
arguments - The arguments to supply to the constructor.
Returns:
A new instance of the type represented by this instantiable type literal; never null.
Throws:
Exception - If the construction fails.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.