Reflection
Reflection: Maps the properties and methods of a class to the appropriate class.
Reflection Basic Use
Get the three methods of class classes:
- Class name. class
- Object name. GetClass ()
- Class.forName ("Class name to load")
It is written according to the API, and the general process is:
- To get the class class of a particular class in one of three ways, that is, the byte code for that class
- Invokes the GetConstructor (Class<?>. parametertypes) of the Class object to get the constructor method object
- The call is a new object created by the Newinstance (Object ... Initargs) method of the construction method class constructor
- Invokes the GetMethod (String name, Class<?> ... parametertypes) of the Class object to get the method object
- Invokes the Invoke (Object obj, Object ... args) method of method object class methods, calling the corresponding method on the object
Uniquely identifies a method with the parameter type of the method, based on: The overload of the method
Reflection of an array
The following example illustrates a few points:
- For an array of the same type of elements, the same dimension array, class
- Different dimension, different class
- Different dimensions, the parent class is object, same
- The base type is that the array cannot be directly converted to object[]
- Java.util.Arrays's Aslist method API look at
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//Next sentence compilation does not pass: Error: (MB) ja VA: Non-comparable types: java.lang.class<capture#1, total? Extends int[]> and java.lang.class<capture#2, altogether?
Extends Int[][]>//system.out.println (a1.getclass () = = A3.getclass ());
Object []B3 = a3;//through//The following sentence is compiled without Error: (a) 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:
True
class [I
class [[I
True
class Java.lang.Object
[[i@1540e19d] [
ABC]
Random entry: Hashcode and memory leak issues reference Java API:
- Hashcode once generated, do not change
- Object Equals method returns True, the Hascode should be consistent
- Conversely, the Equals method returns False,hascode does not necessarily differ from each other
If the member variable that participates in the Hascode calculation changes Midway, the subsequent remove fails, causing a memory leak
Configuration file Loading
class loader load read-only configuration file
Class name. Class.getclassloader (). getResourceAsStream (str);
Class name. Class.getresourceasstream (str), or call the class loader in essence. SOURCE interception (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);
With regard to path STR, the wording is somewhat fastidious.
- No slash, relative path: str = "Config.properties";
- Plus slash, from classpath root path: str = "/org/iot/ui/config.properties";
Before compiling Java code, some conf/folders are added to rely on or marked as the source folder, which is clearly XML files, no Java source code. From here, I now know that it is the use of reflection to load a configuration file.
Introspection (instropector) & JavaBean
JavaBean the process of reading the value of property x: Capitalize, prefix, get method.
"X"--> "x"--> "GetX"--> "Methodgetx"
Self-operation with introspection
I'm not working on it right now, so I'm not going to post code, just attach the core class
Simple implementation: Using the 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 with no parameters
Using the Beanutils Toolkit
- String and Integer conversions (propertyutils)
- Attribute Cascade operation
- Manipulating map