reflex
With reflection, you can directly obtain the type, attribute and method in the class bytecode file.
Demo code:
Create a new class named User as the reflecting operation object
public class User { private int id; private String name; private String password; public User() { } /** * @return the id */ public int getId() { return id; } /** * @return the name */ public String getName() { return name; } /** * @return the password */ public String getPassword() { return password; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
Demonstration
class
Acquisition class
Object u = new User(); Class class1 = u.getClass();
Get class name
class1.getName()
Get class access
int modifier = class1.getModifiers(); boolean flag = Modifier.isPublic(modifier); System.out.println("yes public?: "+flag);
attribute
Get all properties of the class
Access to private can also be obtained
Field[] arr = class1.getDeclaredFields(); for (Field field:arr) { System.out.println ("Properties in class:" + field); }
Get the value of all properties of the class
Private properties cannot be obtained by default, but can be obtained directly without checking access rights.
// Get values for all properties for (Field field:arr) { // Do not check access rights field.setAccessible(true); // Get the value of field in u object Object o = field.get(u); System.out.println("Property values in class:" +o); }
Specify property name to get property
Field f = class1.getDeclaredField("name"); f.setAccessible(true); f.set(u, "Zhang San"); Object o = f.get(u); System.out.println("name: " + o);
Method
Get all methods (including methods inherited from the parent class)
Method[] allMethods = class1.getMethods(); System.out.println("All methods of class:"); for (Method method:allMethods) { System.out.println(method); }
Get the class's own methods
Method[] onlyMethods = class1.getDeclaredMethods(); for (Method method:onlyMethods) { System.out.println(method); }
Call the return value method without parameters
Method method2 = class1.getDeclaredMethod("getName"); String str = (String)(method2.invoke(u)); System.out.println ("Return value:"+ str);
Call method with parameter and no return value
Method method = class1.getDeclaredMethod("setName", String.class); method.invoke(u, "Li Si");
Full demo code:
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class ReflectTest { public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException, InvocationTargetException { Object u = new User(); // Get class to User Class class1 = u.getClass(); System.out.println("Class name:"+class1.getName()); // Get class access int modifier = class1.getModifiers(); boolean flag = Modifier.isPublic(modifier); System.out.println("yes public?: "+flag); // Get all properties Field[] arr = class1.getDeclaredFields(); for (Field field:arr) { System.out.println ("Properties in class:" + field); } // Get values for all properties for (Field field:arr) { // Do not check access rights field.setAccessible(true); // Get the value of field in u object Object o = field.get(u); System.out.println("Property values in class:" +o); } // Specify the field name to get the property Field f = class1.getDeclaredField("name"); f.setAccessible(true); f.set(u, "Zhang San"); Object o = f.get(u); System.out.println("name: " + o); // Get all methods (including inheritance methods) Method[] allMethods = class1.getMethods(); System.out.println("All methods of class:"); for (Method method:allMethods) { System.out.println(method); } // Get the current class's own methods Method[] onlyMethods = class1.getDeclaredMethods(); for (Method method:onlyMethods) { System.out.println(method); } // Call a parameterized method Method method = class1.getDeclaredMethod("setName", String.class); method.invoke(u, "Li Si"); // Call no parameter return value method Method method2 = class1.getDeclaredMethod("getName"); String str = (String)(method2.invoke(u)); System.out.println ("Return value:"+ str); } }