Java Reflection

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine.

Reflection enables us to:

  • Examine an object’s class at runtime
  • Construct an object for a class at runtime
  • Examine a class’s field and method at runtime
  • Invoke any method of an object at runtime
  • Change accessibility flag of Constructor, Method and Field

For example, JUnit use reflection to look through methods tagged with the @Test annotation, and then call those methods when running the unit test.

How to use reflection?
To use reflection API we need class object, which we can obtain it in two ways:
If we know the class name at compile time:

Class aClass = MyObject.class

Or if we can obtain the class object at runtime with fully qualified class name.

Class aClass = Class.forName(className)

Common Reflection Methods to access Class Information:

String className = aClass.getName();
String simpleClassName = aClass.getSimpleName();
int modifiers = aClass.getModifiers();
Package package = aClass.getPackage();
Class superclass = aClass.getSuperclass();
Class[] interfaces = aClass.getInterfaces();
Constructor[] constructors = aClass.getConstructors();
Method[] method = aClass.getMethods();
Field[] method = aClass.getFields();
Annotation[] annotations = aClass.getAnnotations();

Reflection accessing Constructor :

Constructor[] constructors = aClass.getConstructors();
// Retrieving the constructor that takes a String parameter
Constructor constructor = aClass.getConstructor(new Class[]{String.class});
// Retrieving the Constructor parameter types
Class[] parameterTypes = constructor.getParameterTypes();
// Instantiating an object with construtor
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");

 

Reflection accessing class fields:

Field[] fields = aClass.getFields();
// accessing the field named "name" in the class
Field field = aClass.getField("name");
String fieldName = field.getName();  // getting field name
Object fieldType = field.getType();  // getting field type
// Getting and setting field values

Class  aClass = MyObject.class
Field field = aClass.getField("someField");

MyObject objectInstance = new MyObject();
Object value = field.get(objectInstance);
field.set(objetInstance, value);

 

Reflection accessing private fields and methods:
To access a private field you will need to call the Class.getDeclaredField(String name) or Class.getDeclaredFields() method along with Field.setAcessible(true. The methods Class.getField(String name) and Class.getFields() methods only return public fields, so they won’t work.

To access a private method you will need to call the Class.getDeclaredMethod(String name, Class[] parameterTypes) or Class.getDeclaredMethods() method along with Method.setAcessible(true. The methods Class.getMethod(String name, Class[] parameterTypes) and Class.getMethods() methods only return public methods, so they won’t work.

 

Refer to this tutorial for detailed description of Reflection API’s
http://tutorials.jenkov.com/java-reflection/index.html, http://www.programcreek.com/2013/09/java-reflection-tutorial/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s