Java Se: Custom ClassLoader

Source: Internet
Author: User
Tags type casting java se

How does the JVM know the classes in the Java.lang package? How does the JVM know the classes in our application? There is a class in our application, but the JVM throws ClassNotFoundException, which is why? Xxximpl class has implemented interface Xxx, but throws Xxximpl does not extend from Xxx, which is why? When using type conversion, it is possible to throw Aa.bb.cc.XXX can not cast to aa.bb.cc, which is why? And so many seemingly bizarre situations, in fact, all because of ClassLoader.

Everyone who understands the reflection knows that each class has a class object corresponding to it, so where does this class object come from?

If you want to know the basics of class, you can refer to:

http://blog.csdn.net/irelandken/article/details/7048817http://  tyrion.iteye.com/blog/1958814http://www.blogjava.net/lihuaxajh/articles/94371.html  http://www.javaworld.com/article/2077332/core-java/get-a-load-of-that-name.html  http://www.javaworld.com/article/2075796/java-platform/ Java-101--class-and-object-initialization.html

My understanding of ClassLoader

Here's what I read after reading these blogs, articles, and customizing a classloader after testing:

1) When ClassLoader loads a class, it does not load all classes in the class, but instead dynamically loads it at run time according to the class being used, i.e. loading on demand.

2) When ClassLoader loads a class, its parent class (including interfaces) is also loaded

3) The current Class A at run time, other classes to be used in the class (B,c,d class, etc.), the default is loaded by the current Class A loader.

4) Type casting, the first decision is not the same class loader, if not, can not be converted. When an object A is converted to a class loaded by Bootstrapclassloader (the parent class or interface of a), it does not seem to follow this process.

5) Thread#contextclassloader can be used in switch class loaders.

6) By default, the context loader of the thread is the same as the context loader of the parent thread.

7) class.forname ("xxx"), Xxx.class loader, is using the class loader of the current class.

The following is a list of custom ClassLoader codes:

Here's a simple explanation of these classes:

Stringutil.java

There are only two methods in Stringutil, which are used to determine the null value of a string. It is not necessary to write this class, but you can see the effect in the test.

Debugutil.java

Just to make some information.

Person.java

This is a typical Java Bean and there is nothing to say.

Classloadertestrunner.java

This is the business class. From the code is also very simple, just loaded the person class, create a person object, output some information.

This class can either use the system's default ClassLoader to load tests (the main method) or use a custom class loader to test.

Custom ClassLoader

There are two kinds of loading mechanisms commonly used: delegate loading mechanism, sub-class priority loading

1) The process of the delegate loading mechanism loadclass ():

(1) Determine if this class has been loaded

(2) If not loaded, the current class loader's parent loader is loaded, that is, the LoadClass () method is executed.

(3) If all the parent loaders of the current class loader are not loaded, let the current loader call Findclass () to load.

2) Sub-class priority loader process:

(1) Determine if this class has been loaded

(2) The current class loader loads the class directly

(3) The parent class loading process is not loaded.

The default loading method in JDK is 1), which is the delegate loading mechanism.

Custom ClassLoader, typically overriding LoadClass, Findclass.

The delegate field is used to specify whether to use the default delegate-loading mechanism.

This is the method of the custom load class. The process is:

1) determine if the loader has loaded this class, in order to avoid repeated loading.

2) If delegate=true, the delegate loads, the default load mode is used.

3) If delegate=false, exclude the protected class, and then use the following custom Findclass to load the class. If an exception occurs, the default Load method is still used.

Findclass is to load and define a class object based on its name. This method is written very rough, just to test ClassLoader.

You can then use the Classloadertest.java to test:

A brief description of this test:

1) The relevant classes in this test, such as String, URLClassLoader, Classloader,object, thread,runnable,exception, etc. are all by default system loader (Systemclassloader) or its parent class (Extclassloader, Bootstrapclassloader) to load. At this runtime, the loader for this class (Classloadertest) is Systemclassloader.

2) When loading the Classloadertestrunner class, use the custom class loader (that is, Systemclassloader's sub-loader). When the Classloadertestrunner class is loaded, its parent class is also loaded, which is the object class and the Runable interface.

3) The process of this test execution:

(1) Create a custom loader instance that specifies its parent loader as the system loader.

(2) Load class Classloadertestrunner.

(3) Start a thread to perform related tasks.

4) According to the above, if the object task = Myappclassloader. LoadClass (Classfullname). Newinstance ();

Switch

Classloaderrunner task= (Classloaderrunner) Myappclassloader. LoadClass (Classfullname). Newinstance (); it will go wrong.

The Classloaderrunner is loaded by the system loader, and Myappclassloader is a custom loader. Therefore, both types cannot be converted.

An error can also occur if you use a custom ClassLoader to load the person class directly in this class.

If interested, you can according to my summary above, I have this test, my own analysis of the thread T execution process, which classes are loaded by the custom ClassLoader, which classes are loaded by the system loader, the results of the above task execution?

If you do not understand, you can also refer to:

Reference

http://blog.csdn.net/irelandken/article/details/7048817

http://tyrion.iteye.com/blog/1958814

Http://www.blogjava.net/lihuaxajh/articles/94371.html

Http://www.javaworld.com/article/2077332/core-java/get-a-load-of-that-name.html

Http://www.javaworld.com/article/2075796/java-platform/java-101--class-and-object-initialization.html

Java Se: Custom ClassLoader

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.