Reflection in Java:
Reflection is an infrastructure enabling a program can see and manipulate itself. It is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class. Reflection uses special kind of java class: Class. The object of the Class type can hold all the information of the class and have getter methods to extract this information.
Code
The following code example gets the all information about java String library/package.
import java.lang.reflect.*;
public class ReflectionExample {
public static void main(String[] args) {
try {
Class c = null;
c = Class.forName("java.lang.String");
// gets all methods information
Method[] methods = c.getMethods();
// gets all constructors information
Constructor[] constructors = c.getConstructors();
// gets all fields information
Field[] fields = c.getFields();
// Displaying all methodsName on console
for ( Method mthds : methods) { // for each loop
System.out.println(mthds.getName());
}
// Displaying all constructorsName on console
for ( Constructor constructor : constructors) {
System.out.println(constructor.getName());
}
// Displaying all fieldsName on console
for (Field fl : fields) {
System.out.println(fl.getName());
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
Now, create your own class and gets its all information.
public class MyClass {
private int id;
private String name;
public MyClass() {}
public MyClass(int _id , String _name) {
this.id = _id;
this.name = _name;
}
public void showFields() {
System.out.println("ID is : " + id + "\n Name Is : " + name);
}
public void helloWorld() {}
public void hiFi() {}
private void displayResult2() {}
}
Your "MyClass" all information:
import java.lang.reflect.*;
public class ReflectionExample2 {
public static void main(String[] args) {
Class c = null;
c = Class.forName("MyClass");
// gets all methods information
Method[] methods = c.getMethods();
// gets all constructors information
Constructor[] constructors = c.getConstructors();
// Displaying all public methodsName and parameters also on console
System.out.println("The following Are MyClass Method Names");
for (int i = 0 ; i < methods.length ; i++ ) {
int modifier = methods[i].getModifiers();
if(Modifier.isPublic(modifier)) {
System.out.print("public" + " " + methods[i].getName() + "(");
Class[] paramTypes = methods[i].getParameterTypes();
for (int j = 0 ; j < paramTypes.length ; j++) {
String params = paramTypes[j].getName();
System.out.print(params + ",");
}
System.out.println(")");
}
}
// Displaying all constructorsName and parameters also on console
System.out.println("The following Are MyClass Constructors Names");
for (int i = 0 ; i < constructors.length ; i++) {
System.out.print(constructors[i].getName() + "(");
Class[] parameterTypes = constructors[i].getParameterTypes();
for(int j = 0 ; j < parameterTypes.length ; j++) {
String params = parameterTypes[j].getName();
System.out.print(params + ",");
}
System.out.println(")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Reflection is an infrastructure enabling a program can see and manipulate itself. It is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class. Reflection uses special kind of java class: Class. The object of the Class type can hold all the information of the class and have getter methods to extract this information.
Code
The following code example gets the all information about java String library/package.
import java.lang.reflect.*;
public class ReflectionExample {
public static void main(String[] args) {
try {
Class c = null;
c = Class.forName("java.lang.String");
// gets all methods information
Method[] methods = c.getMethods();
// gets all constructors information
Constructor[] constructors = c.getConstructors();
// gets all fields information
Field[] fields = c.getFields();
// Displaying all methodsName on console
for ( Method mthds : methods) { // for each loop
System.out.println(mthds.getName());
}
// Displaying all constructorsName on console
for ( Constructor constructor : constructors) {
System.out.println(constructor.getName());
}
// Displaying all fieldsName on console
for (Field fl : fields) {
System.out.println(fl.getName());
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
Now, create your own class and gets its all information.
public class MyClass {
private int id;
private String name;
public MyClass() {}
public MyClass(int _id , String _name) {
this.id = _id;
this.name = _name;
}
public void showFields() {
System.out.println("ID is : " + id + "\n Name Is : " + name);
}
public void helloWorld() {}
public void hiFi() {}
private void displayResult2() {}
}
Your "MyClass" all information:
import java.lang.reflect.*;
public class ReflectionExample2 {
public static void main(String[] args) {
try {
c = Class.forName("MyClass");
// gets all methods information
Method[] methods = c.getMethods();
// gets all constructors information
Constructor[] constructors = c.getConstructors();
// Displaying all public methodsName and parameters also on console
System.out.println("The following Are MyClass Method Names");
for (int i = 0 ; i < methods.length ; i++ ) {
int modifier = methods[i].getModifiers();
if(Modifier.isPublic(modifier)) {
System.out.print("public" + " " + methods[i].getName() + "(");
Class[] paramTypes = methods[i].getParameterTypes();
for (int j = 0 ; j < paramTypes.length ; j++) {
String params = paramTypes[j].getName();
System.out.print(params + ",");
}
System.out.println(")");
}
}
// Displaying all constructorsName and parameters also on console
System.out.println("The following Are MyClass Constructors Names");
for (int i = 0 ; i < constructors.length ; i++) {
System.out.print(constructors[i].getName() + "(");
Class[] parameterTypes = constructors[i].getParameterTypes();
for(int j = 0 ; j < parameterTypes.length ; j++) {
String params = parameterTypes[j].getName();
System.out.print(params + ",");
}
System.out.println(")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
0 comments:
Post a Comment