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, details about the project's repository can be found at here.

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.

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

String name = names.get(8);

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.

Documentation [more]

Loading Classes

Accessing Fields

Accessing Static Inner Classes

Accessing Static Fields

Invoking Constructors

Invoking Methods

Overcoming Type Erasure

Invoking Static Methods

Accessing JavaBean Properties