Java reflection in action

ReflectionInherited

Java reflection is all about examining and/or modifying the properties of an object in the run time. It’s important to note that reflection specifically applies to objects – so you need an object of a class to get information for that particular class.

Reflection in Java consists of 2 primary things that you should remember:

  1. Metadata. Metadata literally means data about the data. In this case, metadata means extra data that has to do with your Java program – like data about your Java classes, constructors, methods, fields, etc.
  2. Functionality that allows you to manipulate the metadata as well. So, functionality that would allow you to manipulate those fields, methods, constructors, etc. You can actually call methods and constructors using Java reflection – which is an important fact to remember.

This is the java class we need to get the details of.


package com.technodyne.org;

public class AppTest {

private int counter;

 public void printIt(){
 System.out.println("printIt() no param");
 }

 public void printItString(String temp){
 System.out.println("printIt() with param String : " + temp);
 }

 public void printItInt(int temp){
 System.out.println("printIt() with param int : " + temp);
 }

 public void setCounter(int counter){
 this.counter = counter;
 System.out.println("setCounter() set counter to : " + counter);
 }

 public void printCounter(){
 System.out.println("printCounter() : " + this.counter);
 }
}

And then add this class to get the reflection of appTest class.


package com.technodyne.org;

import java.lang.reflect.Method;
public class ReflectApp {

public static void main(String[]args){

 //no paramater
 Class noparams[] = {};

 //String parameter
 Class[] paramString = new Class[1];
 paramString[0] = String.class;

 //int parameter
 Class[] paramInt = new Class[1];
 paramInt[0] = Integer.TYPE;

 try {
 //load the AppTest at runtime
 Class cls = Class.forName("com.technodyne.org.AppTest");
 Object obj = cls.newInstance();

 //call the printIt method
 Method method = cls.getDeclaredMethod("printIt", noparams);
 method.invoke(obj, null);

 //call the printItString method, pass a String param
 method = cls.getDeclaredMethod("printItString", paramString);
 method.invoke(obj, new String("technodyne"));

 //call the printItInt method, pass a int param
 method = cls.getDeclaredMethod("printItInt", paramInt);
 method.invoke(obj, 123);

 //call the setCounter method, pass a int param
 method = cls.getDeclaredMethod("setCounter", paramInt);
 method.invoke(obj, 999);

 //call the printCounter method
 method = cls.getDeclaredMethod("printCounter", noparams);
 method.invoke(obj, null);

 } catch (Exception e) {
 // TODO: handle exception
 e.printStackTrace();
 }
 }
}

Here is the result.

printIt() no param
printIt() with param String : technodyne
printIt() with param int : 123
setCounter() set counter to : 999
printCounter() : 999

You can find the sourcecode from below repo 🙂

https://github.com/supun/Reflection

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s