Java — Reflection in Java | Code Factory

Donate : Link

WordPress Blog : Link

Applications… : Link

  • 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. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

Reflection can be used to get information about

  • Class: The getClass() method is used to get the name of the class to which an object belongs.
package com.example.java.programming;/* A simple Java program to demonstrate the use of reflection */
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author code.factory
*/
/* class whose object is to be created */
class Demo {

/* creating a private field */
private String s;

/* creating a public constructor */
public Demo() {
s = "Code Factory";
}

/* creating a public method with no arguments */
public void method1() {
System.out.println("The string is: " + s);
}

/* creating a public method with int as argument */
public void method2(int n) {
System.out.println("The number is: " + n);
}

/* creating a private method */
private void method3() {
System.out.println("Private method invoked");
}
}
public class Test {
public static void main(String... strings) throws Exception {

/* creating object whose property is to be checked */
Demo demo = new Demo();

/* creating class object from the object using getClass() method */
Class cls = demo.getClass();
System.out.println("The name of class is: " + cls.getName());

/* getting the constructor of the class through the object of the class */
Constructor constructor = cls.getConstructor();
System.out.println("The name of constructor is: " + constructor.getName());

System.out.println("The public methods of class are:");

/* getting methods of the class through the object
* of the class by using getMethods() */
Method[] methods = cls.getMethods();

for(Method method : methods) {
System.out.println(" - " + method.getName());
}

/* creates object of desired method by providing the
* method name and parameter class as arguments to
* the getDeclaredMethod() */
Method methodCall1 = cls.getDeclaredMethod("method2", int.class);

/* invokes the method at runtime */
methodCall1.invoke(demo, 34);

/* creates object of the desired field by providing
* the name of field as argument to the
* getDeclaredField() method */
Field field = cls.getDeclaredField("s");

/* allows the object to access the field irrespective
* of the access specifier used with the field */
field.setAccessible(true);

/* takes object and the new value to be assigned
* to the field as arguments */
field.set(demo, "CODE");

/* Creates object of desired method by providing the
* method name as argument to the getDeclaredMethod() */
Method methodCall2 = cls.getDeclaredMethod("method1");

/* invokes the method at runtime */
methodCall2.invoke(demo);

/* Creates object of the desired method by providing
* the name of method as argument to the
* getDeclaredMethod() method */
Method methodCall3 = cls.getDeclaredMethod("method3");

/* allows the object to access the method irrespective
* of the access specifier used with the method */
methodCall3.setAccessible(true);

/* invokes the method at runtime */
methodCall3.invoke(demo);
}
}

Output:

The name of class is: com.example.java.programming.Demo
The name of constructor is: com.example.java.programming.Demo
The public methods of class are:
- method2
- method1
- wait
- wait
- wait
- equals
- toString
- hashCode
- getClass
- notify
- notifyAll
The number is: 34
The string is: CODE
Private method invoked

Uses of Reflection

  • Extensibility Features: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

  • Performance Overhead: Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Reference:

https://docs.oracle.com/javase/tutorial/reflect/index.html

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store