Alex is a Software Engineer working on Android development tools, especially Android Studio, at Google. His interests include Java, API design, OOP, IDEs and testing. Alex spends some of his spare time working on Open Source, blogging, writing technical articles, and speaking at international conferences. The opinions expressed here represent his own and not those of his employer. Alex is a DZone MVB and is not an employee of DZone and has posted 49 posts at DZone. You can read more from them at their website. View Full User Profile

FEST-Reflect 0.3: Java Reflection Simplified

01.31.2008
| 6507 views |
  • submit to reddit

FEST-Reflect is a Java library that provides a Fluent Interface-based API that simplifies the usage of Java Reflection, resulting in improved readability and type safety. It supports access to constructors, methods and fields.

We are proud to announce that FEST-Reflect 0.3 is out!

Although I heard/read many times that reflection is "evil" (whatever that means,) I still think reflection can be useful in certain cases. 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. Our last resource is to access the UI delegate of such component (e.g. JTree) using reflection to achieve our goal.

As any tool, reflection has its uses. The "evil" part, IMHO, comes from abuse.

The following example compares FEST-Reflect with plain reflection. Let's start by defining the class Names:

class Names {
private final List<String> names = new ArrayList<String>();

String get(int index) {
return names.get(index);
}
}

The following code listing shows how to call the method "get" using reflection:

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);

The same example using FEST-Reflect:

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

You can download the latest release here (file fest-reflect-0.3.zip.) FEST-Assert requires Java SE 5.0 or later.

Here are some useful links:

Feedback is always appreciated :)

References
Published at DZone with permission of Alex Ruiz, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andy DePue replied on Fri, 2008/02/01 - 11:21am

This looks interesting... I wonder what performance is like?

 

Alex Ruiz replied on Fri, 2008/02/01 - 11:57pm

Hi Andy,

We haven't done any performance testing on this API yet. We create a couple of "intermediate" objects to hold state in the fluent interface. My guess is, if there is any negative performance impact, it would be negligible. We'll do some performance testing for our next version.

Best regards,

-Alex.

 

 

aslan ozturk replied on Thu, 2008/07/03 - 3:18am

nice posting.

nakliyat 

Mehmet Cinar replied on Fri, 2010/04/23 - 8:38am

site is great evden eve nakliyat evden eve nakliye nakliye

Fatih Yılmaz replied on Mon, 2013/04/15 - 11:21am in response to: Mehmet Cinar

 Issue in our wonderful and very helpful thank you as the source.


kamyonetçi

Mehmet Cinar replied on Sun, 2013/08/18 - 1:57pm

Let's think a little sensitive to the issue contributor friends   istanbul evden eve nakliyat 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.