17th reflection and class loader 17.1 using reflection
Reflection: The. Class document reflects the basic information of the class, and the way the class information is obtained from APIs such as class is called reflection.
17.1.1 class and. Class Documents
1. An instance of Java.lang.Class represents a. class document, class, interface, enum, etc. that are loaded when the Java application is run. The class class has no public constructors, and the JVM automatically generates the instance when each. class document is loaded, and the corresponding class object is automatically generated by the JVM.
2, the way to obtain the class object:
(1) through the GetClass method of object
(2) Get the class object corresponding to each object by the. Class constant
(3) If it is a basic type, you can use the corresponding packaging class plus. Type gets the class object
3. Static chunks are executed when the. Class document is first loaded by default.
4. Class information is stored in the. class document at compile time, which is how Java supports the execution of runtime type recognition. Compile time if the relevant class is used, the compiler checks the information in the corresponding. class document to determine whether the compilation can be completed. A class is used for the execution period, and the corresponding class object is checked first, and if not, the corresponding. class document is loaded and the corresponding class instance is generated.
5. The class instance obtained by default with GetClass () or. Class will be the same object.
17.1.2 using Class.forName ()
1. The Class.forName () method implements the dynamic load class, which can be used to specify the class name to obtain class-related information.
2, Class.forName () Another version allows you to specify the class name, whether static chunks and ClassLoader are executed when the class is loaded.
Static Class forname (String name,boolean initialize,classloader loader)
17.1.3 getting information from class
The class object represents the loaded. Class document, and when the class object is obtained, the information loaded in the. Class document can be obtained, such as the type of package, constructor, method member, data member, and so on.
17.1.4 creating objects from class
1. When you do not know the class name beforehand, you can use Class.forName () to dynamically load the. class document, and then use its newinstance () method to build the class instance after the class object is obtained.
Class CLZ = Class.forName (Args[0]); Object obj = clz.newinstance ();
2, if the class definition has more than one constructor, you can also specify which constructor to use to generate the object, which must specify the parameter type when calling class's GetConstructor () method, get the constructor object that represents the constructor, Use Constructor's newinstance () to specify the parameter values at creation time to establish the object.
3. Generate an array: The class instance of the array is generated by the JVM, or the class instance can be obtained through. class or GetClass (). Generate a dynamically generated java.util.ArrayList array of length 10:
Class CLZ = Java.util.ArrayList.class; Object obj = array.newinstance (clz,10);
4, from the first index to get the actual class instance of the mobile phone object, you can use it with array.newinstance () to establish an array instance.
17.1.5 Action Object methods and members
1. You can use the Invoke () method to invoke the specified method dynamically.
2. Call a protected or private method, you can use the class's Getdeclaredmethod () to obtain the method, and specify True when calling Method Setaccessible ().
17.1.6 Dynamic Agent
1, in the reflection API, there is a proxy class, you can dynamically set up an interface operation object.
2, the use of proxy mechanism has two proxy methods: static agents and dynamic agents.
3. Static Agent
In the static proxy implementation, the proxy object and the Proxied object must implement the same interface, in the proxy object can implement the log service, when necessary to invoke the proxy object, you can use the proxy object:
Hello proxy = new Helloproxy (new Hellospeaker ()); Proxy.hello ("Justin");
When you create a proxy object helloproxy you must specify the proxy object Hellospeaker, and the proxy object proxy Hellospeaker executes the Hello method.
Static proxies must operate on individual proxy classes for individual interfaces, and when application behavior is complex, multiple interfaces must define multiple proxy objects, and the operation and maintenance of proxy objects can be burdensome.
4. Dynamic Agent
Using the dynamic proxy mechanism, you can use a handler agent that proxies multiple interfaces, and the processor class must manipulate the Java.lang.reflect.InvocationHandler interface.
Use the Proxy.newprxyinstance () method to establish a proxy object, which must be specified when the class loader is called, the interface to be proxied, and the processor on the interface where the method is defined, Proxy.newprxyinstance () The underlying method generates a class instance of the proxy object using native mode and uses it to generate the proxy object.
17.2 Understanding Class Loaders
The actual duties of the ClassLoader are loaded into the. class document, and the JDK itself has the default ClassLoader.
17.2.1 Class Loader Hierarchical architecture
1. Bootstrap Loader: Generate exrtended Loader and system Loader
Exrtended Loader: The parent loader is bootstrap Loader
System Loader: The parent loader is exrtended Loader
2, Bootstrap loader is usually written by C. If Oracle's Jdk,bootstrap loader searches the class at the specified location in the system parameters Sun.boot.class.path, the default is the. class document in the classes of the JRE directory, or the classes in the. Jar document in the Lib directory.
3, exrtended loader is written by Java, will search the system parameters java.ext.dirs in the specified location of the class, by default, the JRE directory lib\ext\classes. class document.
4. The system loader is written by Java and will search for the class at the specified location in the Java.class.path parameter, that is, the classpath path, which is the. class document under the current working path by default.
5. The load class is looking for the class in the order of Bootstrap loader->exrtended Loader->system Loader, if all class loaders cannot find the specified class, Will throw Java.lang.NoClassDefFoundError.
6. Calling the GetParent () method on null throws a Nullpointedexception exception.
7. ClassLoader can use the LoadClass () method to load a class, and when loading a class using the Localclass method, static chunks are not executed by default, and static chunks are executed only when the class is actually used to establish an instance.
17.2.2 establishing ClassLoader instances
1, using URLClassLoader to generate a new class loader, you need to Java.net.URL as its parameters to specify the class load search path. When the specified class is loaded using the URLClassLoader loadclass () method, the parent loader is commissioned to search on its behalf.
2. The. class document loaded by the same class loader will have only one class instance. If the same. class document is loaded by two different classloader, there will be two different class instances.
3. Path can enter additional paths that are not in the system loader hierarchy class loader search path.
Java JDK8 Learning Notes--17th chapter reflection and ClassLoader