Practice on how to invoke specified methods through reflection
1, Background
The general background is like this. Our company has a scenario in which an entity is queried by calling the RPC interface, and then there are multiple attributes in the entity. Then, the attributes of the entity are defined and bound with the corresponding types by defining enumeration, and then the entity is transmitted to judge and obtain the value of the specified attribute by if... else, In this context, it is better to obtain one or two attributes. If you need to add multiple if... else, it may not be easy to expand later. Of course, we can also use the policy mode. Reflection is mainly used here.
2, What is reflection
1. Concept: reflection is actually a mechanism, which can be used to dissect classes and operate methods, attributes, construction methods and other members of classes during program operation
2. There are four ways to obtain reflection:
2.1 Class clazz = Student.class;
2.2 Class<? extends Student> clazz = new Student().getClass();
2.3 Class.forName("com.ljm.reflect.test"), the fully qualified class name of the class is in parentheses
2.4 obtain from the loader of the current thread:
Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass("com.ljm.reflect.test");
3. Application of reflection:
In fact, I think the most common application of reflection is that it can be seen in Spring code. In addition, some serialized components operate objects through reflection to convert some attributes in an object into another object.
4. Disadvantages of reflection:
Reflection actually affects performance. Why does it affect performance? First, when we use reflection to create object instances and methods, we need to create a large number of Class objects and constructor objects. These objects are temporary objects, which will cause GC. We all know that frequent GC will affect applications. In addition, when we call methods, we need to traverse the array for acquisition, which will also consume a little time.
3, How to call the specified method through reflection to obtain the property of the object
3.1 Student object
package com.ljm;/** * @Author LJM * @Date 2021/11/7 12:28 * @Version 1.0 */ /** * @ClassName Student * @Description Student object test * @Author ljm * @Date 12:28 2021/11/7 * @Version 2.1 **/ public class Student { private Integer id; private String address; private String name; private String hobby; private String gender; public Student(Integer id, String address, String name, String hobby, String gender) { this.id = id; this.address = address; this.name = name; this.hobby = hobby; this.gender = gender; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Student{" + "id=" + id + ", address='" + address + '\'' + ", name='" + name + '\'' + ", hobby='" + hobby + '\'' + ", gender='" + gender + '\'' + '}'; } }
3.2 testing
package com.ljm;/** * @Author LJM * @Date 2021/11/7 12:35 * @Version 1.0 */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * @ClassName ReflectTest * @Description Reflection test * @Author ljm * @Date 12:35 2021/11/7 * @Version 2.1 **/ public class ReflectTest { public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { //Get bytecode object Class<Student> clazz = Student.class; Student student = new Student(1, "Shangrao, Jiangxi", "ljm", "Basketball", "male"); Object fieldTest = getAttributesByReflect(clazz, "getId",student); System.out.println(field); } protected static Object getAttributesByReflect(Class<Student> clazz, String methodName, Student student) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = clazz.getMethod(methodName, null); //Set brute force cracking method.setAccessible(true); return method.invoke(student); } }
3.3 when we need to call private member methods, we only need to set the corresponding properties to brute force cracking
4, Summary
In fact, how to say reflection? There are many ways to use it. I don't write a blog to show others, but just make a simple record. If I want to study deeply, I have to practice more and read more books. At least you have to know that there is this thing. I hope I will get better and better in the future. Come on, I can't write because I'm going to play basketball. Ha ha ha, basketball is a good thing. He plays higher and higher with more effort, Learning is also a good thing. The harder you work, the luckier you are!