Class Reflection

java.lang.Object
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, Ivan Hristov
  • Method Details

    • 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.
      Nested field are supported with dot notation, e.g. "person.address.street".

      Let's look how it works on an example :

       Let's say we have the following simple service:
       
       public class BusinessService {
         private NotificationService notificationService = new NotificationService();
         //... logic goes here
       }
        
       Where NotificationService is defined as follows:
       
       public class NotificationService {
         private Logger logger = new Logger();
         private IClientStatusDao clientStatusDao = new ClientStatusDao();
         //... logic goes here
       }
       
       And our ClientStatusDao looks like:
       
       public class ClientStatusDao implements IClientStatusDao {
         private final Session session = new SessionImpl();
          //... logic goes here
       }
           
       Let's say we want to change the logger field of NotificationService within our instance of BusinessService, we can do this:
        
         field("notificationService.logger").ofType(Logger.class).in(businessService).set(loggerMock);
        
       You can also set the deeply nested session field within ClientStatusDao like this:
       
         field("notificationService.clientStatusDao.session").ofType(Session.class).in(businessService).set(sessionMock);
       
      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 Introspection.
      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