How to use Java Reflection API example


Reflection API allows developers to manipulate the internal properties of the class such as fields and methods. It also allows developers to call private methods in other packages or use any private fields defined in different packages.

Lets examine the following code and the output of the program;

First create a DummyClass.javaclas

package javareflectionexample;

public class DummyClass {

private int value1 = 0;
private String value2 = "value2 content";
private Integer value3 = 1;
private boolean value4 = true;
private int value5 = 5;

public void add(int a,int b){
System.out.println("a+b --> " + (a+b));
}

public void minus(int a,int b) {
System.out.println("a-b --> " + (a-b));
}

public void divide(int a,int b) throws ArithmeticException {
System.out.println("a/b --> " + (a/b));
}
public void adding(Integer a,Integer b, Integer c, Double d) {
System.out.println("a + b + c + d --> " + (a + b + c + d));
}
private void printValues() {
System.out.println("value1 --> " + value1);
System.out.println("value2 --> " + value2);
System.out.println("value3 --> " + value3);
System.out.println("value4 --> " + value4);
System.out.println("value5 --> " + value5);
}

}

And create main program MainProgram.java

package javareflectionexample;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainProgram {

public static void main(String[] args) {

try {

Class clazz = Class.forName("javareflectionexample.DummyClass");
Method[] methods = clazz.getDeclaredMethods();

//printing all methods inside javareflectionexample.DummyClass including
//private methods

System.out.println("START PRINTING METHOD NAMES\n");

for (int i = 0; i < methods.length; i++) {

Method method = methods[i];
System.out.println("[method name] --> " +">method.getName()
+ " [long desc] --> " + method.toString());

}

//Getting all Parameter types for all methods
//includes private methods

//if you want to get only public methods and methods inside Object class
//use claz.getMethods()

System.out.println("\nSTART PRINTING PARAMETER NAMES AND RETURN TYPES");

for (int i = 0; i < methods.length; i++) {

System.out.println("\n[method " + i + " name] --> " +methods[i].getName()
+ " [return type] --> " +methods[i].getReturnType());

for (int j = 0; j < methods[i].getParameterTypes().length; j++) {
Class parameter = methods[i].getParameterTypes()[j];
System.out.println(" [parameter " + j + " type] --> " +parameter.getName());
}

}

System.out.println("\nSTART PRINTING FIELD NAMES AND VALUES\n");

//getting all fields including private ones inside DummyClass
Field fields[] = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++){
fields[i].setAccessible(true);
System.out.println("[field " + i + " name] -->" +fields[i].getName()
+ " [field " + i + " value] --> " + fields[i].get(clazz.newInstance()));
}

//calling all private methods inside DummyClass
System.out.println("\nSTART CALLING ONLY PRIVATE METHODS\n");

for (int i = 0; i < methods.length; i++) {
if (methods[i].getModifiers() == Modifier.PRIVATE) {
System.out.println("[method " + i + " name] --> " +methods[i].getName()
+ " [modifier] --> " + "Modifier.PRIVATE");
methods[i].setAccessible(true);
//SYNTAX for calling a method with parameters
//methods[i].invoke(obj, args);
//this will call private void printValues()
methods[i].invoke(clazz.newInstance());
}
}

} catch (Exception ex) {
Logger.getLogger(MainProgram.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

Output of the program;

START PRINTING METHOD NAMES

[method name] –> minus [long desc] –> public void javareflectionexample.DummyClass.minus(int,int)
[method name] –> adding [long desc] –> public void javareflectionexample.DummyClass.adding(java.lang.Integer,java.lang.Integer, java.lang.Integer,java.lang.Double)
[method name] –> printValues [long desc] –> private void javareflectionexample.DummyClass.printValues()
[method name] –> add [long desc] –> public void javareflectionexample.DummyClass.add(int,int)
[method name] –> divide [long desc] –> public void javareflectionexample.DummyClass.divide(int,int) throws java.lang.ArithmeticException

START PRINTING PARAMETER NAMES AND RETURN TYPES

[method 0 name] –> minus [return type] –> void
[parameter 0 type] –> int
[parameter 1 type] –> int

[method 1 name] –> adding [return type] –> void
[parameter 0 type] –> java.lang.Integer
[parameter 1 type] –> java.lang.Integer
[parameter 2 type] –> java.lang.Integer
[parameter 3 type] –> java.lang.Double

[method 2 name] –> printValues [return type] –> void

[method 3 name] –> add [return type] –> void
[parameter 0 type] –> int
[parameter 1 type] –> int

[method 4 name] –> divide [return type] –> void
[parameter 0 type] –> int
[parameter 1 type] –> int

START PRINTING FIELD NAMES AND VALUES

[field 0 name] –>value1 [field 0 value] –> 0
[field 1 name] –>value2 [field 1 value] –> value2 content
[field 2 name] –>value3 [field 2 value] –> 1
[field 3 name] –>value4 [field 3 value] –> true
[field 4 name] –>value5 [field 4 value] –> 5

START CALLING ONLY PRIVATE METHODS

[method 2 name] –> printValues [modifier] –> Modifier.PRIVATE
value1 –> 0
value2 –> value2 content
value3 –> 1
value4 –> true
value5 –> 5

Download Eclipse Project Source

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