reflection
Reflection: Map the properties and methods of a class into corresponding classes.
Basic use of reflection
There are three ways to get Class class:
Just write according to the API, the general process is:
Use the parameter type of the method to uniquely identify a method, based on: method overloading
Reflection of arrays
The following example mainly illustrates several points:
public class ReflectTest { public static void main(String[] args) { int [] a1 = new int[]{1,2,3}; int [] a2 = new int[5]; int [][] a3 = new int[2][3]; System.out.println(a1.getClass() == a2.getClass());//true System.out.println(a1.getClass());//class [I System.out.println(a3.getClass());//class [[I System.out.println(a1.getClass().getSuperclass() == a3.getClass().getSuperclass());//true System.out.println(a2.getClass().getSuperclass());//class java.lang.Object //The next sentence does not compile: Error:(15, 42) java: Uncomparable types: java.lang.Class<capture#1, total? extends int[]> and java.lang.Class<capture#2, total? extends int[][]> //System.out.println(a1.getClass() == a3.getClass()); Object []b3 = a3;//Compiled by //The next sentence does not pass Error:(17, 24) java: Incompatible type: int[] cannot be converted to java.lang.Object[] //Object [] b1 = a1; String s1 = "abc"; System.out.println(Arrays.asList(a1));//[[I@1540e19d] System.out.println(Arrays.asList(s1));//[abc] }}Output:
trueclass [Iclass [[Itrueclass java.lang.Object[[I@1540e19d][abc]
Input: Hashcode and memory leak problem refer to java api:
If the member variable participating in hascode calculation changes in the middle, the subsequent removal fails, resulting in memory leakage
Configuration file loading
The class loader loads the read-only configuration file class name.class.getClassLoader().getResourceAsStream(str);
The class name.class.getResourceAsStream(str), is essentially calling the class loader. Source code intercept (Class.java under java.lang package):
public InputStream getResourceAsStream(String name) { name = resolveName(name); ClassLoader cl = getClassLoader0(); if (cl==null) { // A system class. return ClassLoader.getSystemResourceAsStream(name); } return cl.getResourceAsStream(name);}Regarding the path str, the writing method is a bit particular.
In the past, when compiling Java code, some conf/ folders had to be added to dependencies or marked as source folders. They were obviously XML files, but there was no Java source code. From here, I now know that it is because of using reflection to load the configuration file
Inspector & JavaBean
The process of JavaBean reading the value of attribute x: capitalization, prefix, and obtaining methods.
"x"-->"X"-->"getX"-->"MethodGetX"
I haven't used it to operate it myself with introspection at the moment, so I won't paste the code anymore, only the core class is attached
Simple implementation: Use java.beans.PropertyDescriptor class
Trouble implementation: Use the java.beans.Introspector class to traverse the return value of the getBeanInfo method
JavaBean must have a constructor without parameters
Using the BeanUtils toolkit