Java Reflection explained
Java Reflection is a feature in the Java programming language that allows code to examine or modify the behavior of methods, classes, and interfaces at runtime. It is used to inspect, instantiate, and invoke classes and methods, as well as to examine and modify fields, and annotations.
Reflection is implemented in the java.lang.reflect package, which contains a set of classes and interfaces that provide the ability to reflect on the structure of a class and its members. Some of the key classes and interfaces in this package include:
- Class: The Class class represents a class or interface in the running Java application. It can be used to obtain information about the class, such as its name, package, and superclass, as well as to create new instances of the class.
- Field: The Field class represents a field (i.e., a class variable or instance variable) in a class. It can be used to obtain information about the field, such as its name, type, and value, as well as to set or get the value of the field.
- Method: The Method class represents a method in a class. It can be used to obtain information about the method, such as its name, return type, and parameter types, as well as to invoke the method.
- Constructor: The Constructor class represents a constructor in a class. It can be used to obtain information about the constructor, such as its parameter types, and to create new instances of the class using the constructor.
Here's an example of how to use reflection to get the name of a class:
import java.lang.reflect.*;
public class ReflectionExample {
public static void main(String[] args) {
SomeClass sc = new SomeClass();
Class<?> cls = sc.getClass();
System.out.println("Class name: " + cls.getName());
}
}
class SomeClass {
private int someField;
public void someMethod(){}
}
In this example, we create an instance of the SomeClass
class, and then use the getClass()
method of the instance to get a Class
object representing the class. Then, we use the getName()
method of the Class
object to get the name of the class, and print it to the console.
The output of this example will be:
Class name: SomeClass
Alternatively, you can use SomeClass.class
to get the class object and then use the getName()
method, it doesn't require any instance of the class.
System.out.println("Class name: " + SomeClass.class.getName());
In this way you can get the name of any class, regardless of whether or not you have an instance of that class.
here's an example of how you can use reflection to invoke a private method of a class:
import java.lang.reflect.*;
public class ReflectionExample {
public static void main(String[] args) {
SomeClass sc = new SomeClass();
try {
Method privateMethod = SomeClass.class.getDeclaredMethod("privateMethod");
privateMethod.setAccessible(true);
privateMethod.invoke(sc);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
class SomeClass {
private int someField;
public void someMethod(){}
private void privateMethod(){
System.out.println("This is a private method.");
}
}
In this example, we use the getDeclaredMethod()
method of the Class
class to get a Method
object representing the private method privateMethod()
. Then, we use the setAccessible(true)
method to make the method accessible, even though it is private. Finally, we use the invoke()
method to invoke the method on an instance of the SomeClass
class.
Here, we are able to invoke a private method which is not accessible to other classes.