Java reflection and java reflection
JAVA reflection mechanism: in the running state, all attributes and methods of the class can be obtained, and any method and attribute of the class can be called, the reflection mechanism is used to dynamically obtain class information and call methods of objects.
The following code uses reflection to obtain the class information.
Person class
public class PersonBean { private String Name; private int age; private double price; private boolean isMarry; protected String mobile; public String address; public void setIsMarry(boolean isMarry) { this.isMarry = isMarry; } public boolean isMarry() { return isMarry; } public void setAddress(String address) { this.address = address; } public void setAge(int age) { this.age = age; } public void setMobile(String mobile) { this.mobile = mobile; } public void setName(String name) { Name = name; } public void setPrice(double price) { this.price = price; } public String getAddress() { return address; } public int getAge() { return age; } public String getMobile() { return mobile; } public String getName() { return Name; } public double getPrice() { return price; }}
Get related code through reflection:
Public class Test {public static void main (String [] args) {System. out. println (); System. out. println ("setField:"); setField (); System. out. println (); System. out. println ("getFields:"); getFields (); System. out. println (); System. out. println ("invokeMethod:"); invokeMethod (); System. out. println (); System. out. println ("getMethod:"); getMethod ();}/*** call Method */private static void invokeMethod () {Class c Lazz = getClass (PersonBean. class. getName (); try {Object object = clazz. newInstance (); System. out. println ("age is:" + (PersonBean) object ). getAge (); Method method = clazz. getMethod ("setAge", int. class); method. invoke (object, 123456); Method method1 = clazz. getMethod ("getAge"); int age = (int) method1.invoke (object); System. out. println ("age is:" + age);} catch (Exception e) {e. printStackTrace () ;}}/*** Set attribute */private static void setField () {Class clazz = getClass (PersonBean. class. getName (); try {PersonBean personBean = (PersonBean) clazz. newInstance (); Field [] fields = clazz. getDeclaredFields (); System. out. println ("set attributes:"); for (int I = 0; I <fields. length; I ++) {Field field = fields [I]; field. setAccessible (true); if (field. getType (). equals (int. class) {field. setInt (personBe An, 24);} else if (field. getType (). equals (double. class) {field. setDouble (personBean, 24);} else if (field. getType (). equals (float. class) {} else if (field. getType (). equals (boolean. class) {field. setBoolean (personBean, true);} else if (field. getType (). equals (String. class) {field. set (personBean, "Guangzhou") ;}} System. out. println (personBean. getAge (); System. out. println (personBean. getAddress ());} Catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace () ;}}/*** get all attributes */private static void getFields () {Class clazz = getClass (PersonBean. class. getName (); Field [] fields = clazz. getDeclaredFields (); System. out. println ("attributes:"); for (int I = 0; I <fields. length; I ++) {System. out. println (Modifier. toString (fields [I]. getModifiers () + "" + Fields [I]. getType (). getSimpleName () + "" + fields [I]. getName () ;}} private static String getModifiers (int modifiers) {String modifierName; Modifier modifier = new Modifier (); if (modifier. isPublic (modifiers) {modifierName = "public";} else if (modifier. isProtected (modifiers) {modifierName = "protected";} else if (modifier. isPrivate (modifiers) {modifierName = "private";} else {modifie RName = modifiers + "";} return modifierName;}/*** get all methods */private static void getMethod () {Class clazz = getClass (PersonBean. class. getName (); Method [] methods = clazz. getDeclaredMethods (); for (Method method: methods) {String methodModifier = getModifiers (method. getModifiers (); String methodName = method. getName (); String methodReturnType = method. getReturnType (). getName (); Class <?> [] MethodParam = method. getParameterTypes (); String methodParamName = ""; for (int I = 0; I <methodParam. length; I ++) {Class param = methodParam [I]; methodParamName = methodParamName + param. getName () + "arg" + I + ",";} if (methodParamName. length ()> 1) {methodParamName = methodParamName. substring (0, methodParamName. length ()-1);} methodParamName = "(" + methodParamName + ")"; System. out. println (methodModifier + "" + methodReturnType + "" + methodName + "" + methodParamName );}} /*** get the class Object * @ param clazzName * @ return */private static Class getClass (String clazzName) {Class clazz = null; try {clazz = Class. forName (clazzName);} catch (ClassNotFoundException e) {e. printStackTrace () ;}return clazz ;}}
Output result: