org.fest.reflect.type
Class Type

java.lang.Object
  extended by org.fest.reflect.type.Type

public final class Type
extends Object

Understands loading a class dynamically.

The following is an example of proper usage of this class:

   // Loads the class 'org.republic.Jedi'
   Class<?> jediType = type("org.republic.Jedi").load();

   // Loads the class 'org.republic.Jedi' as 'org.republic.Person' (Jedi extends Person)
   Class<Person> jediType = type("org.republic.Jedi").loadAs(Person.class);

   // Loads the class 'org.republic.Jedi' using a custom class loader
   Class<?> jediType = type("org.republic.Jedi").withClassLoader(myClassLoader).load();
 

Since:
1.1
Author:
Alex Ruiz

Method Summary
 Class<?> load()
          Loads the class with the name specified in this type, using this class' ClassLoader.
<T> Class<? extends T>
loadAs(Class<T> type)
          Loads the class with the name specified in this type, as the given type, using this class' ClassLoader.
static Type newType(String name)
          Creates a new Type: the starting point of the fluent interface for loading classes dynamically.
 TypeLoader withClassLoader(ClassLoader classLoader)
          Specifies the ClassLoader to use to load the class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newType

public static Type newType(String name)
Creates a new Type: the starting point of the fluent interface for loading classes dynamically.

Parameters:
name - the name of the class to load.
Returns:
the created Type.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.

load

public Class<?> load()
Loads the class with the name specified in this type, using this class' ClassLoader.

Returns:
the loaded class.
Throws:
ReflectionError - wrapping any error that occurred during class loading.

loadAs

public <T> Class<? extends T> loadAs(Class<T> type)
Loads the class with the name specified in this type, as the given type, using this class' ClassLoader.

The following example shows how to use this method. Let's assume that we have the class Jedi that extends the class Person:

 Class<Person> type = type("org.republic.Jedi").loadAs(Person.class);
 

Type Parameters:
T - the generic type of the type.
Parameters:
type - the given type.
Returns:
the loaded class.
Throws:
NullPointerException - if the given type is null.
ReflectionError - wrapping any error that occurred during class loading.

withClassLoader

public TypeLoader withClassLoader(ClassLoader classLoader)
Specifies the ClassLoader to use to load the class.

Example:

 Class<?> type = type("org.republic.Jedi").withClassLoader(myClassLoader).load();
 

Parameters:
classLoader - the given ClassLoader.
Returns:
an object responsible of loading a class with the given ClassLoader.
Throws:
NullPointerException - if the given ClassLoader is null.


Copyright © 2007-2010 FEST (Fixtures for Easy Software Testing). All Rights Reserved.