Last week I sent out my resume as a Java backend development engineer. I had an interview with a Meituan interviewer this week, during which he asked me about reflection in Java. (I have to boast that Meituan’s efficiency is really high, the morning after the first meeting, the evening of the second meeting immediately arranged.)
No matter what the Java technology position, Java reflection principle, in the interview appeared many times, so I have prepared before the interview, so today combined with the interview questions first detailed talk about Java reflection principle.
JAVA reflection mechanism
JAVA reflection is implemented in runtime state,
For any class, you can know all the attributes and methods of that class;
For any object, can call any of its properties and methods;
This ability to dynamically retrieve information and invoke methods on objects is called the Reflection mechanism of the Java language.
Usage scenarios
IDE autoprompt, object (prompt: properties, methods)
Do not know the specific information of the class or object, should use reflection to implement.
For example, the name of the class is stored in the XML file, and the attributes and attribute values are stored in the XML file. The XML file needs to be read at runtime to dynamically obtain the information of the class.
The principle of
Java generates a class file after compilation, and reflection uses the bytecode file to find methods, properties, and so on in its classes
function
The key class
Class object
Type identifier, which is retained in the JVM for each object.
Contains all information about the class
Constructor, member variables, member methods, and interfaces can be obtained from this object
Obtaining method:
-
Fetching a literal directly, such as xxx.class, does not trigger class initialization but the XXX class has already been loaded into the method area.
-
Through the getClass method of the Object class, such as Object.getClass(). Triggers the initialization of the class
-
Static methods of Class, such as class.forname (). Triggers the initialization of the class
Field
A member variable, an attribute object in a class.
Get this from the Class’s getDeclaredField() or getDeclaredFields() methods
Field’s methods are mainly divided into two categories, namely getXXX and setXXX
Method
Class. Static methods and member methods (including abstract methods) are included.
Invoke () is used to accomplish the purpose for which the method is called dynamically.
A non-static variable that requires an object parameter
The setAccessible() method does not affect other objects or the original method
getDeclaredMethod
You can get the Method object Method that specifies the Method name and parameters.
@CallerSensitive
public Method getDeclaredMethod(String name, Class
... parameterTypes)
throws NoSuchMethodException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
// Find a method object from the returned method list that matches the name and parameters.
Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
}
return method;
}
Copy the code
privateGetDeclaredMethods
Get the list of methods declared in this Class from the cache or JVM.
searchMethods
Find a method object from the returned method list that matches the name and parameters.
If a matching Method is found, a new copy is returned, the method.copy () Method.
ReflectionData
Used to cache the following attribute data for classes read from the JVM.
Constructor
Constructor. Class constructor
GetConstructor () : Gets the matching constructor
Steps:
-
Get all the constructors first, and then compare them by parameter type;
-
When a match is found, copy a constructor return via ReflectionFactory;
-
Otherwise throw NoSuchMethodException;
Parent class/parent interface
advantages
Through reflection, Java can dynamically load unknown external configuration objects and temporarily generate bytecode for loading and use, which makes the code more flexible and greatly improves the extensibility of applications.
conclusion
Let’s have fun. Let’s have fun. Don’t joke about the interview.
The reflection feature of Java is an advanced feature of Java that comes up frequently in interviews. The questions an interviewer asks are often relevant, and once they are asked, it’s important to get your answers right, not lose track, and to get to the point. Before your interview, memorize the principles, application scenarios, functions and key classes (Class, Fileds, Methods, Constructor, etc.) of reflection features.
Refer to the link: www.icourse163.org/learn/ECNU-…