25. Create and use your own class loaderExcept for the root loader, the JVM is an instance of the ClassLoader subclass. programmers can use the extends ClassLoader subclass and override its method to implement a custom class loader.Obtain the detailed information of the corresponding Class through the Class Object
Public class ClassTest {private ClassTest () {} public ClassTest (String s) {System. out. println ("constructor with Parameters");} public void info () {System. out. println ("No param
static Car initByDefaultConst () throws Throwable
{
// ① Get the Car class object through the Class Loader
ClassLoader loader = Thread. currentThread (). getContextClassLoader ();
Class clazz = loader. loadClass (com. baobaotao. reflect. Car );
// ② Obtain the default constructor object of the class and instantiate the Car through it
Constructor cons = clazz
not conducive to the extension of the program, because it is necessary to use a specific class to complete the program.3: The method used in class, the static forname method .Specifies what class name to get what class bytecode file object, this way is strongest, as long as the class name of the string passed in.//1. based on the given class name to obtain for class loadingString classname = "Cn.itcast.reflect.Person";//From configuration fileClass clazz
What is reflection? Reflection is the mapping of the various components in the bytecode into the corresponding Java class, Java reflection technology since JDK1.1 has emerged, most popular frameworks have adopted this technology, it is important to see, this article will detail my research on the Java Reflection technology.Class that represents the byte-code objectAll classes in Java have their own unique bytecode, and when the program calls that class, the JVM loads the bytecode into memory . I
Today's contentReflectionBeanutils classReflectionAt run time, you can get and invoke members of a class (constructs, member variables, member methods, and so on), including private.The premise of Reflection:Gets the bytecode object of the class (class object)How to get a bytecode object:(1) object. GetClass ()Like what:Student s = new Student ();Class clazz = S.getclass ();(2) class name. classLike what:Class cla
InternalName = "Org.apache.struts.action.ActionResources"; ①initinternal ();
The Initinternal method gets a messageresources object from the following
Internal = messageresources.getmessageresources (InternalName);
This resource file mainly includes the definition of some message information, which can refer to the Actionresources.properties file under Org.apache.struts.action.
The Getmessageresources method in the Messageresources.java,
if (defaultfactory = = null) {
Defaultfactory = Message
Hot Swap Class Loader implementation:Java code
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Lang. Reflect. method;
Import java.net. url;
Import java.net. urlclassloader;
/**
* You can re-load a Class Loader with the same name.
*
* The chain Loading Mode assigned by both parents is abandoned.
* The State of the member variables of the class after the overload needs to be maintained externally.
*
* @ Author Ken. Wu
* @ Mail ken.wug@gmail.com
* 01:37:43
mbatteryvoltage; private int mbatterytemperature; private string mbatterytechnology;
// The variables declared in batteryservice. Java are used in com_android_server_batteryservice.cpp. That is, the variables declared in com_android_server_batteryservice.cpp are also the variables declared in batteryservice. java.
Gfieldids. maconline = env-> getfieldid (clazz ," Maconline "," Z "); Gfieldids. musbonline = env-> getfieldid (
Import Java. Lang. annotation. annotation; Import Java. Lang. Reflect. constructor; Import Java. Lang. Reflect. method; // Use two annotations to modify the class @ Suppresswarnings (value = "unchecked" ) @ Deprecated Public Class Classtest { // Define a private constructor for this class Private Classtest (){} // Define a constructor with Parameters Public Classtest (string name) {system. Out. println ( "Execute the constructor with Parameters" );} // Define a info m
, clazzid: {type: mongoose. schema. objectid, Ref: 'clazz '}) studentschema. statics = {findclazznamebystudentid: function (studentid, callback) {return this. findone ({_ ID: studentid00000000.populate('clazzid'0000.exe C (callback)} // other methods are omitted ..} module. exports = studentschema
As you can see, the master needs to set the clazzid to ref to clazz, and the dependency is the name of the mode
property, a public property, a private method, and a public method. and three different parametric construction methods are prepared for this class. We'll use this class later.(1) Get the full package name and class name from an object Package Com.myreflex; Public class Testmain { publicstaticvoid main (string[] args) { new Demo (); // get the full package name and class name from an object System.out.println (Demo.getclass (). GetName ()); } }The result
Parsing the XML summary (SAX, pull, Dom three ways)A brief introduction to reflection:Reflection1, what is the reflection technology?dynamically gets the contents of the specified class and class ( member ) , and run its contents. The object cannot be used if the application is already running and cannot be created with the new object. You can then find the corresponding bytecode file based on the class name of the configuration file, load it into memory, and create an instance of the class obje
class must be identified first. The first two methods are not conducive to the extension of the program, because it is necessary to use a specific class to complete the program.3: The method used in class, the static forname method . Specifies what class name to get what class bytecode file object, this way is strongest, as long as the class name of the string passed in.//1. obtained for class loading based on the given class nameString classname = "Cn.itcast.reflect.Person";// from config
What is the Java reflection mechanism反射机制是在运行状态中,可以知道任何一个类的属性和方法,并且调用类的属性和方法;What the reflection mechanism can do1, Judge the class of the running object2. Construct an object of any class3. Get the properties and methods of any class4. Call any property and method5. Generate Dynamic AgentUse reflection to encapsulate a result set as an object or collection (actual measurements available) PackageCoral.base.util;ImportJava.beans.IntrospectionException;ImportJava.beans.PropertyDescriptor;ImportJav
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.