Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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:

Code Block
java
java
String name = names.get(8);

using reflection, we need the following code:

Code Block
java
java
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:

Code Block
java
java
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]

Section
Column
width50%

Loading Classes

Column
width50%

Accessing Fields

Section
Column
width50%

Accessing Static Inner Classes

Column
width50%

Accessing Static Fields

Section
Column
width50%

Invoking Constructors

Column
width50%

Invoking Methods

Section
Column
width50%

Overcoming Type Erasure

Column
width50%

Invoking Static Methods

Section
Column
width100%

Accessing JavaBean Properties