Java Reflection

When I build some framework I have to access and modify anonymous classes parameters and functions. For that, in Java we use reflection.

In a lot of my projects I found myself repeatedly implementing similar code. So finally I encapsulated this functionality into one simple library. It can read and write Maps, Lists and simple Objects.

Library is downloadable from http://code.google.com/p/foxjunior-commons/downloads/detail?name=fj-reflection.jar&can=2&q= and source code http://code.google.com/p/foxjunior-commons/source/browse/#svn%2Ffoxjunior-commons%2Freflection

Usage of this library is simple:

ReflectionHelper.getValue(Object o, "name");

If o is map library tries to get key “name” otherwise:

  • public function getName()
  • public parameter name
  • private function getName()
  • private parameter name
ReflectionHelper.setValue(Object o, "name", "John Doe");

If o is Map value with key “name” and value “John Doe” is set, otherwise:

  • public function setName()
  • public parameter name
  • private method setName()
  • private object parameter name

Also you can use more deep keys. For Instance:

ReflectionHelper.setValue(Object o, "person.name.first", "John");

The pattern is the same. Only if for example person.name is null then Exception is thrown.

  • Facebook
  • Twitter
  • Google
  • Google Plus
  • LinkedIn
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Comments are closed.