Code explanation for implementing the Class Loader Mechanism Using equinox (2)

Source: Internet
Author: User

 

 

Why javax. * package does not need to be introduced when developing bundle in equinox Environment

I. Prerequisites classloader

Classloader is an abstract class provided by Java. It is the object responsible for loading classes. Classloader is used to load classes into the memory in JVM. When the JVM needs to use a class, it requests this class to classloader Based on the name, and then classloader returns a class object that represents this class.

Classes in all java. * packages are loaded by classloader, and classloaer is loaded into the memory when the Java command is executed. Generally, you do not need to import these classes when using classes in the Java. * package during development.

Ii. classloader in Bundle

In the Equinox environment, each bundle has an independent classloader. The corresponding class is bundleloader.
The classloader of each bundle has a parent. This parent (parent classloader) is the classloader of the previous layer in the class delegate model. The corresponding class name is parentclassloader, which is defined in this way

Private Static class parentclassloader extends classloader {
Protected parentclassloader (){
Super (null );
}
}

This is an empty classloader that inherits java. Lang. classloader directly. All classes in the Java package are loaded by Java. Lang. classloader. According to the class delegate model, you can find the required Java class by inheriting classloader.

If (name. startswith (java_package ))
// 1) If startswith "Java." delegate to parent and terminate search
// We Want to throw classnotfoundexceptions if a java. * class cannot be loaded from the parent.
Return parent. loadclass (name );

The above Code is the first step in the flowchart for bundle to load the class. Use parentclassloader to obtain the class in the required Java. * package, as shown in.

This is why the java. * package does not need to be introduced in the bundle.

3. How do I know that Java. * these classes have been loaded?

1. Write a class as follows:

Public Class
{
Public static void main (string [] ARGs)
{
System. Out. println ("Hello world! ");
}
}

2. Compile

Javac A. Java

3. Run

Java-verbose

The displayed content is as follows:

[Loaded java. Lang. object from shared objects file]
[Loaded java. Io. serializable from shared objects file]
[Loaded java. Lang. Comparable from shared objects file]
[Loaded java. Lang. charsequence from shared objects file]
[Loaded java. Lang. String from shared objects file]
[Loaded java. Lang. Reflect. genericdeclaration from shared objects file]
[Loaded java. Lang. Reflect. Type from shared objects file]
[Loaded java. Lang. Reflect. annotatedelement from shared objects file]
[Loaded java. Lang. Class from shared objects file]
[Loaded java. Lang. cloneable from shared objects file]
[Loaded java. Lang. classloader from shared objects file]
)

 

4. Why should I introduce the javax. * package?

The following code is another articleCode explanation for implementing the Class Loader Mechanism Using equinoxAs mentioned above, the code snippet of the flowchart loaded by the bundle class.

...
String pkgname = getpackagename (name );
// Follow the osgi Delegation Model
If (checkparent & parent! = NULL )...{
If (name. startswith (java_package ))
// 1) If startswith "Java." delegate to parent and terminate search
// We Want to throw classnotfoundexceptions if a java. * class cannot be loaded from the parent.
Return parent. loadclass (name );
Else if (isbootdelegationpackage (pkgname ))
// 2) if part of the bootdelegation list then delegate to parent and continue of failure
Try ...{
Return parent. loadclass (name );
} Catch (classnotfoundexception cnfe )...{
// We want to continue
}
}
...

From the code above, we can see that the second condition is used if it is not the first case (the package name does not start with Java.

Isbootdelegationpackage (pkgname) returns true (this configuration will be added later), so the execution is

Parent. loadclass (name );

This line of code is the same as the code for the first condition, that is, to load the javax. * package using parentclassloader. Because classloader only loads the java. * package, classes in the javax. * package cannot be obtained here, leading to classnotfoundexception errors. As you can see from the code above, classnotfoundexception is not processed in the code, but is executed continuously. Obtain javax. * packages from import package, required bundles, bundle internal classpath, and dynamic import respectively.

Normally, these javax. * packages are provided as required bundle, and only need to be provided once, and avoid providing (export) again/repeatedly in other bundle ).

Packagesource source = findimportedsource (pkgname); If (source! = NULL ){
// 3) found import source Terminate search at the source
Result = source. loadclass (name );
If (result! = NULL)
Return result;
Throw new classnotfoundexception (name );
}
// 4) search the required Bundles
Source = findrequiredsource (pkgname );
If (source! = NULL)
// 4) attempt to load from source but continue on Failure
Result = source. loadclass (name );
// 5) search the local bundle
If (result = NULL)
Result = findlocalclass (name );
If (result! = NULL)
Return result;
// 6) attempt to find a dynamic import source; only do this if a required source was not found
If (Source = NULL ){
Source = finddynamicsource (pkgname );

This is why the javax. * package needs to be introduced.

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.