Package org.fest.reflect.core


package org.fest.reflect.core

Provides a "fluent" API that makes usage of the Java Reflection API easier and improves code readability.

Here are some examples:

   // import static org.fest.reflect.core.Reflection.*;

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

   // Equivalent to call 'person.setName("Luke")'
   method("setName").withParameterTypes(String.class)
                    .in(person)
                    .invoke("Luke");
 
   // 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.getCommonPowers()'
   List<String> powers = staticMethod("getCommonPowers").withReturnType(new TypeRef<List<String>>() {})
                                                        .in(Jedi.class)
                                                        .invoke();   

  • Classes
    Class
    Description
    Understands the entry point for the classes in this package.