Welcome to the FEST Reflection Module


FEST-Reflect is a Java library that provides a fluent interface that simplifies the usage of Java Reflection, resulting in improved readability and type safety.

It can be downloaded here. For Maven 2 users, the project's repository can be found at http://fest.googlecode.com/svn/trunk/fest/m2/repository/ (groupId: fest, artifactId: fest-reflect).

Why FEST-Reflect?


In our opinion, Reflection, when used with caution, can be very useful. For example, in FEST-Swing, there are a couple of special cases where we don't have enough platform-related information to simulate user input on a Swing component. To achieve our goal, our last resource is to access the UI delegate of such component (e.g. JTree) using reflection.

The problem with Reflection is that its API is not very intuitive and quite verbose. For example, to call the method:

  String name = names.get(0);

using reflection, we need the following code:

  Method method = Names.class.getMethod("get", int.class);

  AccessController.doPrivileged(new PrivilegedAction<Void>() {
    public Void run() {
      method.setAccessible(true);
      return null;
    }
  });    

  String name = (String) method.invoke(names, 8);

and with FEST-Reflect:

  String name = method("get").withReturnType(String.class)
                             .withParameterTypes(int.class)
                             .in(names)
                             .invoke(8);

which, in our opinion, is more compact, readable and type safe.

Examples


The following examples demonstrate how easy is to access constructors, methods and fields using FEST-Reflect:

  Person person = constructor().withParameterTypes(String.class)
                               .in(Person.class)
                               .newInstance("Yoda");

  method("setName").withParameterTypes(String.class)
                   .in(person)
                   .invoke("Luke");

  field("name").ofType(String.class)
               .in(person)
               .set("Anakin");


References




Documentation

Reports

edit SideBar