Monday, February 9, 2009

Java Reflection API

Java Reflection is a powerful tools to dynamically instantiate objects. It is especially helpful and reduce the amount of code to be written. Fields, Methods and Constructors are the members of the reflectors.

Sample codes from Java

http://java.sun.com/docs/books/tutorial/reflect/member/fieldModifiers.html

http://java.sun.com/docs/books/tutorial/reflect/member/methodModifiers.html

http://java.sun.com/docs/books/tutorial/reflect/member/ctorModifiers.html

Java Tips:

The Class.getField() and Class.getFields() methods return the public member field(s) of the class, enum, or interface represented by the Class object. To retrieve all
fields declared (but not inherited) in the Class, use the Class.getDeclaredFields() method.

But, one note is that

An IllegalAccessException may be thrown if an attempt is made to
get or set the value of a private or otherwise inaccessible field or to set the
value of a final field (regardless of its access modifiers).

The same goes to Method. This is kind of a setback.

http://java.sun.com/docs/books/tutorial/reflect/TOC.html

No comments:

Post a Comment

Hadoop - How to setup a Hadoop Cluster

Below is a step-by-step guide which I had used to setup a Hadoop Cluster Scenario 3 VMs involved: 1) NameNode, ResourceManager - Host...