JDK class loading source code

Source: Internet
Author: User

 

 

 

Public class launcher {
Static class appclassloader extends urlclassloader {

Public static classloader getappclassloader (classloader)
Throws ioexception {
String S = system. getproperty ("Java. Class. Path ");
File afile [] = s! = NULL? Launcher. getclasspath (s): new file [0];
Return (appclassloader) accesscontroller
. Doprivileged (New privilegedaction (S, afile, classloader ){

Public object run (){
URL aurl [] = s! = NULL? Launcher. pathtourls (PATH)
: New URL [0];
Return new appclassloader (aurl, extcl );
}

Final string Val $ S;

Final file Val $ path [];

Final classloader Val $ extcl;

{
S = S1;
Path = afile;
Extcl = classloader;
Super ();
}
});
}

Public synchronized class loadclass (string S, Boolean flag)
Throws classnotfoundexception {
Int I = S. lastindexof ('.');
If (I! =-1 ){
Securitymanager = system. getsecuritymanager ();
If (securitymanager! = NULL)
Securitymanager. checkpackageaccess (S. substring (0, I ));
}
Return super. loadclass (S, flag );
}

Protected permissioncollection getpermissions (codesource ){
Permissioncollection = super
. Getpermissions (codesource );
Permissioncollection. Add (New runtimepermission ("exitvm "));
Return permissioncollection;
}

Private Static accesscontrolcontext getcontext (File afile [])
Throws malformedurlexception {
Pathpermissions = new pathpermissions (afile );
Protectiondomain = new protectiondomain (
New codesource (pathpermissions. getcodebase (),
(Java. Security. cert. Certificate []) null ),
Pathpermissions );
Accesscontrolcontext = new accesscontrolcontext (
New protectiondomain [] {protectiondomain });
Return accesscontrolcontext;
}

Appclassloader (URL aurl [], classloader ){
Super (aurl, classloader, launcher. Factory );
}
}

Static class extclassloader extends urlclassloader {

Public static extclassloader getextclassloader ()
Throws ioexception
{
File afile [] = getextdirs ();
Try
{
Return (extclassloader) accesscontroller. doprivileged (New privilegedexceptionaction (afile ){

Public object run ()
Throws ioexception
{
Return new extclassloader (dirs );
}

Final file Val $ dirs [];


Throws ioexception
{
Dirs = afile;
Super ();
}
}
);
}
Catch (privilegedactionexception)
{
Throw (ioexception) privilegedactionexception. getexception ();
}
}

Void addexturl (URL ){
Super. addurl (URL );
}

Private Static file [] getextdirs (){
String S = system. getproperty ("Java. Ext. dirs ");
File afile [];
If (s! = NULL ){
Stringtokenizer = new stringtokenizer (S,
File. pathseparator );
Int I = stringtokenizer. counttokens ();
Afile = new file [I];
For (Int J = 0; j <I; j ++)
Afile [J] = new file (stringtokenizer. nexttoken ());

} Else {
Afile = new file [0];
}
Return afile;
}

Private Static URL [] getexturls (File afile []) throws ioexception {
Vector vector = new vector ();
For (INT I = 0; I <afile. length; I ++ ){
String as [] = afile [I]. List ();
If (as = NULL)
Continue;
For (Int J = 0; j <as. length; j ++ ){
File file = new file (afile [I], as [J]);
Vector. Add (launcher. getfileurl (File ));
}

}

URL aurl [] = new URL [vector. Size ()];
Vector. copyinto (aurl );
Return aurl;
}

Public String findlibrary (string s ){
S = system. maplibraryname (s );
For (INT I = 0; I <dirs. length; I ++ ){
String S1 = system. getproperty ("OS. Arch ");
If (S1! = NULL ){
File file = new file (dirs [I], S1), S );
If (file. exists ())
Return file. getabsolutepath ();
}
File file1 = new file (dirs [I], S );
If (file1.exists ())
Return file1.getabsolutepath ();
}

Return NULL;
}

Private Static accesscontrolcontext getcontext (File afile [])
Throws ioexception {
Pathpermissions = new pathpermissions (afile );
Protectiondomain = new protectiondomain (
New codesource (pathpermissions. getcodebase (),
(Java. Security. cert. Certificate []) null ),
Pathpermissions );
Accesscontrolcontext = new accesscontrolcontext (
New protectiondomain [] {protectiondomain });
Return accesscontrolcontext;
}

Private file dirs [];

Public extclassloader (File afile []) throws ioexception {
Super (getexturls (afile), null, launcher. Factory );
Dirs = afile;
}
}

Private Static class factory implements urlstreamhandlerfactory {

Public urlstreamhandler createurlstreamhandler (string s ){
String S1 = (New stringbuilder (). append (prefix). append (".")
. Append (s). append (". Handler"). tostring ();
Try {
Class class1 = Class. forname (S1 );
Return (urlstreamhandler) class1.newinstance ();
} Catch (classnotfoundexception ){
Classnotfoundexception. printstacktrace ();
} Catch (instantiationexception ){
Instantiationexception. printstacktrace ();
} Catch (illegalaccessexception ){
Illegalaccessexception. printstacktrace ();
}
Throw new internalerror (New stringbuilder (). append (
"Cocould not load"). append (s). append (
"System protocol handler"). tostring ());
}

Private Static string prefix = "sun.net. www. Protocol ";

Private Factory (){
}

}

Public static launcher getlauncher (){
Return launcher;
}

Public launcher (){
Extclassloader;
Try {
Extclassloader = extclassloader. getextclassloader ();
} Catch (ioexception ){
Throw new internalerror ("cocould not create extension class loader ");
}
Try {
Loader = appclassloader. getappclassloader (extclassloader );
} Catch (ioexception ioexception1 ){
Throw new internalerror ("cocould not create application Class Loader ");
}
Thread. currentthread (). setcontextclassloader (loader );
String S = system. getproperty ("Java. Security. Manager ");
If (s! = NULL ){
Securitymanager = NULL;
If ("". Equals (s) | "default". Equals (s ))
Securitymanager = new securitymanager ();
Else
Try {
Securitymanager = (securitymanager) loader. loadclass (s)
. Newinstance ();
} Catch (illegalaccessexception ){
} Catch (instantiationexception ){
} Catch (classnotfoundexception ){
} Catch (classcastexception ){
}
If (securitymanager! = NULL)
System. setsecuritymanager (securitymanager );
Else
Throw new internalerror (New stringbuilder (). append (
"Cocould not create securitymanager:"). append (s)
. Tostring ());
}
}

Public classloader getclassloader (){
Return loader;
}

Public static urlclasspath getbootstrapclasspath (){
String S = (string) accesscontroller
. Doprivileged (New getpropertyaction ("Sun. boot. Class. Path "));
URL aurl [];
If (s! = NULL ){
String S1 = s;
Aurl = (URL []) (URL []) accesscontroller
. Doprivileged (New privilegedaction (S1 ){

Public object run (){
Return launcher. pathtourls (Launcher
. Getclasspath (PATH ));
}

Final string Val $ path;

{
Path = s;
Super ();
}
});
} Else {
Aurl = new URL [0];
}
Return new urlclasspath (aurl, factory );
}

Private Static URL [] pathtourls (File afile []) {
URL aurl [] = new URL [afile. Length];
For (INT I = 0; I <afile. length; I ++)
Aurl [I] = getfileurl (afile [I]);

Return aurl;
}

Private Static file [] getclasspath (string s ){
File afile [];
If (s! = NULL ){
Int I = 0;
Int J = 1;
Boolean flag = false;
Int K;
For (INT I1 = 0; (k = S. indexof (file. pathseparator, i1 ))! =-1; I1 = k + 1)
J ++;

Afile = new file [J];
Int L;
Int J1;
For (J1 = L = 0; (L = S. indexof (file. pathseparator, J1 ))! =-1; J1 = L + 1)
If (L-J1> 0)
Afile [I ++] = new file (s. substring (J1, L ));
Else
Afile [I ++] = new file (".");

If (J1 <S. Length ())
Afile [I ++] = new file (s. substring (J1 ));
Else
Afile [I ++] = new file (".");
If (I! = J ){
File afile1 [] = new file [I];
System. arraycopy (afile, 0, afile1, 0, I );
Afile = afile1;
}
} Else {
Afile = new file [0];
}
Return afile;
}

Static URL getfileurl (File file ){
Try {
File = file. getcanonicalfile ();
} Catch (ioexception ){
}
Try {
Return parseutil. filetoencodedurl (File );
} Catch (malformedurlexception ){
Throw new internalerror ();
}
}

Private Static urlstreamhandlerfactory factory = new factory ();

Private Static launcher = new launcher ();

Private classloader loader;

Private Static urlstreamhandler filehandler;

}

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.