+4  A: 

It's not clear what you're trying to do, but Apache Commons Lang provides a number of convenient utilities classes to make reflection a bit less cumbersome, such as MethodUtils, FieldUtils and ConstructorUtils. These typically provide one-liners to abstract out the often bulky raw reflection code.

As @Lauri pointed out, Commons BeanUtils is also available, providing more high-level functions, such as copying properties between objects.

skaffman
I'd like to add http://commons.apache.org/beanutils/ to the mix
Lauri Lehtinen
@Lauri: Yes, that's useful also, although some of the more useful utilities from BeanUtils were migrated into Lang.
skaffman
@Skaffman: mmhhhhhh I a bit abstract to be explain, but let's say that I just want to use reflection. Every time I need to use it I found my self with these `Method m = clazz.getDeclaredMethod` kind of patterns, and knowing a bunch of OS projects use them, I was wondering if is there a *de facto* library for this.
OscarRyz
+1  A: 

Spring Framework contains some helpers, not all that different from Commons Lang/BeanUtils ...

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/util/ReflectionUtils.html

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/BeanUtils.html

Both classes are marked as 'intended for internal use', but still worth a look.

You may even find that you're really looking for a framework such as Spring to manage your objects ... hth

amir75
A: 

I don't know, but if you find yourself using the standard Java reflection APIs a lot, maybe there is something not quite right about the way you design and implement Java programs.

Java is primarily designed as a staticly typed language, and works very well when used that way. While dynamic typing and (particularly) reflection can get you out of difficult problems, it is generally a good idea to restrict their use because:

  • they reduces performance,
  • they makes your source code more complex and less readable, and
  • they make your code more fragile.

Hiding the reflection behind a library layer or within a Sping-like framework addresses the readability issue but not the performance or fragility issues. (There are other approaches that do address all of the issues ... but without more information about why you are doing so much reflective programming, it is not clear they are relevant.)

Stephen C
Thank you for the comment. Yes, I'm aware of the nature of reflection and I agree with you in the penalty it brings. This wasn't in the spirit of *is it good to use it or not* type of question. But thanks anyway.
OscarRyz
+1  A: 

See also java.beans.Expression and java.beans.Statement.

EJP
A: 

Apache Commons Lang is as straight forward as it gets. There is also Whitebox which is PowerMock's Reflection Helper - here is the Quick Summary from the wiki.

Quick summary

  1. Use Whitebox.setInternalState(..) to set a private member of an instance or class.
  2. Use Whitebox.getInternalState(..) to get a private member of an instance or class.
  3. Use Whitebox.invokeMethod(..) to invoke a private method of an instance or class.
  4. Use Whitebox.invokeConstructor(..) to create an instance of a class with a private constructor.

I like it better than ACL as you don't have to be explicit with the staticness of the Fields and Methods.

Cem Catikkas