Reference article:

After learning to reflect, I was accepted (dry goods)

Java Basics 3 – Reflection, Dynamic Proxies, and annotations (juejin. Cn)

1. Four components of Java reflection

package hk.fanshe; public class SmallPineapple { public String name; public int age; private String weight; public SmallPineapple(){ } public SmallPineapple(String name,int age){ this.age=age; this.name=name; } public SmallPineapple(String name,int age,String weight){ this.age=age; this.name=name; this.weight=weight; {} public void getInfo () System. The out. Println (" [" + name + "age is" + age + "] "); } private void getDetailInfo(String name,int age,String weight){ System. Out. Println (" [" + name + "age is" + + "age, weight is" + weight + "] "); }}Copy the code

1.1. The class

Any Class running in memory is an instance of that Class. Remember, for everything you do with reflection, look for the Class first.

Class class1=null; try { class1=Class.forName("hk.fanshe.SmallPineapple"); } catch (Exception e) { e.printStackTrace(); Class class2= smallpine.class; Class class3=new SmallPineapple().getclass ();Copy the code

1.2 the Constructor

Describes a constructor of a class. It contains all the constructor information, such as parameter types, parameter names, and access modifiers

Smallpineapple-instance1 =null; smallPineapple-instance1 =null; smallPineapple-instance1 =null; try { instance1= (SmallPineapple) class1.newInstance(); // The newInstance() construct instance calls the default no-argument constructor. } catch (Exception e) { e.printStackTrace(); } instance1.getInfo(); // the Constructor Constructor calls newInstance() method Constructor = null; SmallPineapple instance2 = null; try { constructor = class2.getConstructor(String.class, int.class); Instance2 = (SmallPineapple) constructor. NewInstance (" SmallPineapple ", 21); constructor.setAccessible(true); instance2.getInfo(); } catch (Exception e) { e.printStackTrace(); }Copy the code

1.3 the Field

Describes a property of a class that contains all information about the property, such as data type, property name, and access modifiers

1.3.1 Getting a Variable in a Class (Field)

Field[] getFields() : Gets all variables in a class that are modified by public

Field getField(String name) : Gets a variable in a class based on the variable name, which must be public

Field[] getDeclaredFields() : getDeclaredFields() : getDeclaredFields() : getDeclaredFields() : getDeclaredFields() : getDeclaredFields() : getDeclaredFields(

Field getDeclaredField(String name) : Obtains a variable from a class by name, cannot obtain an inherited variable

SmallPineapple sp = new SmallPineapple(" SmallPineapple ", 666,"54kg"); Class clazz = sp.getClass(); Field field = clazz.getDeclaredField("weight"); field.setAccessible(true); System.out.println(" + field. Get (sp)); } catch (Exception e) { e.printStackTrace(); }Copy the code

1.4 Method

Methods that describe all methods of a class (including abstract methods) contain all information about that Method, similar to Constructor, except that Method has information about the return value type because constructors do not return values.

//1 public Method Method = null; try { method = class3.getMethod("getInfo"); if (method ! = null) {/ / method. Invoke (class3. GetConstructor (String class, int. J class, String. The class). NewInstance (" little pineapple ", 18, "48 kg"), null);  method.invoke(instance2,null); } } catch (Exception e) { e.printStackTrace(); } //2 Private Method method1 = null; try { method1 = class3.getDeclaredMethod("getDetailInfo",String.class,int.class,String.class); method1.setAccessible(true); if (method1 ! = null) {/ / method. Invoke (class3. GetConstructor (String class, int. J class, String. The class). NewInstance (" little pineapple ", 18, "48 kg"), null);  Method1. invoke(Instance2," Big Pineapple ",36,"48kg"); } } catch (Exception e) { e.printStackTrace(); }Copy the code

1.5 Advantages and disadvantages of reflection

It increases the flexibility of the program, but breaks the encapsulation of the class, and brings performance loss. When using reflection to manipulate objects, the compiler has no way of knowing in advance what type the object is and whether it is accessible legally. Parameter transfer types match. The result is checked, called, and returned from scratch only when the reflected code is called at runtime.