org.fest.reflect.core
Class Reflection

java.lang.Object
  extended by org.fest.reflect.core.Reflection

public final class Reflection
extends Object

Understands the entry point for the classes in this package. The following is an example of proper usage of the classes in this package:

   // 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();

   // Gets the inner class 'Master' in the declaring class 'Jedi':
   Class<?> masterClass = staticInnerClass("Master").in(Jedi.class).get();

   // Equivalent to call 'new Person()'
   Person p = constructor().in(Person.class).newInstance();

   // Equivalent to call 'new Person("Yoda")'
   Person p = constructor().withParameterTypes(String.class).in(Person.class).newInstance("Yoda");

   // Retrieves the value of the field "name"
   String name = field("name").ofType(String.class).in(person).get();

   // Sets the value of the field "name" to "Yoda"
   field("name").ofType(String.class).in(person).set("Yoda");

   // Retrieves the value of the field "powers"
   List<String> powers = field("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).get();

   // Equivalent to call 'person.setName("Luke")'
   method("setName").withParameterTypes(String.class)
                    .in(person)
                    .invoke("Luke");

   // Equivalent to call 'jedi.getPowers()'
   List<String> powers = method("getPowers").withReturnType(new TypeRef<List<String>>() {})
                                            .in(person)
                                            .invoke();

   // Retrieves the value of the static field "count" in Person.class
   int count = staticField("count").ofType(int.class).in(Person.class).get();

   // Sets the value of the static field "count" to 3 in Person.class
   staticField("count").ofType(int.class).in(Person.class).set(3);

   // Retrieves the value of the static field "commonPowers" in Jedi.class
   List<String> commmonPowers = staticField("commonPowers").ofType(new TypeRef<List<String>>() {}).in(Jedi.class).get();

   // Equivalent to call 'person.concentrate()'
   method("concentrate").in(person).invoke();

   // Equivalent to call 'person.getName()'
   String name = method("getName").withReturnType(String.class)
                                  .in(person)
                                  .invoke();

   // Equivalent to call 'Jedi.setCommonPower("Jump")'
   staticMethod("setCommonPower").withParameterTypes(String.class)
                                 .in(Jedi.class)
                                 .invoke("Jump");

   // Equivalent to call 'Jedi.addPadawan()'
   staticMethod("addPadawan").in(Jedi.class).invoke();

   // Equivalent to call 'Jedi.commonPowerCount()'
   String name = staticMethod("commonPowerCount").withReturnType(String.class)
                                                 .in(Jedi.class)
                                                 .invoke();

   // Equivalent to call 'Jedi.getCommonPowers()'
   List<String> powers = staticMethod("getCommonPowers").withReturnType(new TypeRef<List<String>>() {})
                                                        .in(Jedi.class)
                                                        .invoke();

   // Retrieves the value of the property "name"
   String name = property("name").ofType(String.class).in(person).get();

   // Sets the value of the property "name" to "Yoda"
   property("name").ofType(String.class).in(person).set("Yoda");
 

Author:
Alex Ruiz, Yvonne Wang

Method Summary
static TargetType constructor()
          Starting point of the fluent interface for invoking constructors via reflection.
static FieldName field(String name)
          Starting point of the fluent interface for accessing fields via reflection.
static MethodName method(String name)
          Starting point of the fluent interface for invoking methods via reflection.
static PropertyName property(String name)
          Starting point of the fluent interface for accessing properties via Bean Instrospection.
static StaticFieldName staticField(String name)
          Starting point of the fluent interface for accessing static fields via reflection.
static StaticInnerClassName staticInnerClass(String name)
          Starting point of the fluent interface for accessing static inner class via reflection.
static StaticMethodName staticMethod(String name)
          Starting point of the fluent interface for invoking static methods via reflection.
static Type type(String name)
          Starting point of the fluent interface for loading a class dynamically.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

type

public static Type type(String name)
Starting point of the fluent interface for loading a class dynamically.

Parameters:
name - the name of the class to load.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.
Since:
1.1

staticInnerClass

public static StaticInnerClassName staticInnerClass(String name)
Starting point of the fluent interface for accessing static inner class via reflection.

Parameters:
name - the name of the static inner class to access.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.
Since:
1.1

field

public static FieldName field(String name)
Starting point of the fluent interface for accessing fields via reflection.

Parameters:
name - the name of the field to access.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.

staticField

public static StaticFieldName staticField(String name)
Starting point of the fluent interface for accessing static fields via reflection.

Parameters:
name - the name of the static field to access.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.

method

public static MethodName method(String name)
Starting point of the fluent interface for invoking methods via reflection.

Parameters:
name - the name of the method to invoke.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.

staticMethod

public static StaticMethodName staticMethod(String name)
Starting point of the fluent interface for invoking static methods via reflection.

Parameters:
name - the name of the static method to invoke.
Returns:
the starting point of the static method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.

constructor

public static TargetType constructor()
Starting point of the fluent interface for invoking constructors via reflection.

Returns:
the starting point of the method chain.

property

public static PropertyName property(String name)
Starting point of the fluent interface for accessing properties via Bean Instrospection.

Parameters:
name - the name of the property to access.
Returns:
the starting point of the method chain.
Throws:
NullPointerException - if the given name is null.
IllegalArgumentException - if the given name is empty.
Since:
1.2


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