黑马程序员技术交流社区
标题:
API文档里有没有ExtClassLoader和APPClassLoader?
[打印本页]
作者:
code_geass
时间:
2013-8-27 17:06
标题:
API文档里有没有ExtClassLoader和APPClassLoader?
在哪个包里?来大神
作者:
范龙波
时间:
2013-8-27 17:21
jdk 的api 应该是查不到,如果你想看源码可以在rt.jar这个包里的下面位置找到
sun.misc.Launcher$ExtClassLoader
sun.misc.Launcher$AppClassLoader
这两个加载器都是由java语言编写的 ,所在的jar包随 classpath下的相关字节码一同加载到内存.
刚好今天整理了下 jvm 的几点小结感兴趣可以看一下.
http://bbs.itheima.com/thread-78838-1-1.html
作者:
张歆明
时间:
2013-8-27 19:05
在rt.jar下的sun.misc包的类Launcher中
AppClassLoader和ExtClassLoader是内部类 所以 类名中含有$ $前面是外部类 后面是内部类
sun.misc.Launcher$AppClassLoader和sun.misc.Launcher.ExtClassLoader
这个包下面的类不是开源的 所以在java安装路径下的src.zip中没有这个包的源码
之前我对这个类做过反编译 代码如下:
AppClassLoader源码 (从Launcher内部摘取)
由于AppClassLoader是静态内部类,依附于Launcher这个外部类,以下是源码。
static class AppClassLoader extends URLClassLoader{
public static ClassLoader getAppClassLoader(ClassLoader paramClassLoader)
throws IOException{
String str = System.getProperty("java.class.path");
File[] arrayOfFile = str == null ? new File[0] : Launcher.access$200(str);
return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction(str, arrayOfFile, paramClassLoader)
{
public Launcher.AppClassLoader run() {
URL[] arrayOfURL = this.val$s == null ? new URL[0] : Launcher.access$300(this.val$path);
return new Launcher.AppClassLoader(arrayOfURL, this.val$extcl);
}
});
}
AppClassLoader(URL[] paramArrayOfURL, ClassLoader paramClassLoader){
super(paramClassLoader, Launcher.factory);
}
public Class loadClass(String paramString, boolean paramBoolean) throws ClassNotFoundException{
int i = paramString.lastIndexOf('.');
if (i != -1) {
SecurityManager localSecurityManager = System.getSecurityManager();
if (localSecurityManager != null) {
localSecurityManager.checkPackageAccess(paramString.substring(0, i));
}
}
return super.loadClass(paramString, paramBoolean);
}
protected PermissionCollection getPermissions(CodeSource paramCodeSource){
PermissionCollection localPermissionCollection = super.getPermissions(paramCodeSource);
localPermissionCollection.add(new RuntimePermission("exitVM"));
return localPermissionCollection;
}
private void appendToClassPathForInstrumentation(String paramString){
assert (Thread.holdsLock(this));
super.addURL(Launcher.getFileURL(new File(paramString)));
}
private static AccessControlContext getContext(File[] paramArrayOfFile)throws MalformedURLException{
PathPermissions localPathPermissions = new PathPermissions(paramArrayOfFile);
ProtectionDomain localProtectionDomain = new ProtectionDomain(new CodeSource(localPathPermissions.getCodeBase(), (Certificate[])null), localPathPermissions);
AccessControlContext localAccessControlContext = new AccessControlContext(new ProtectionDomain[] { localProtectionDomain });
return localAccessControlContext;
}
static
{
ClassLoader.registerAsParallelCapable();
}
}
复制代码
下面是ExtClassLoader的源码
static class ExtClassLoader extends URLClassLoader
{
public static ExtClassLoader getExtClassLoader() throws IOException{
File[] arrayOfFile = getExtDirs();
try{
return (ExtClassLoader)AccessController.doPrivileged(new PrivilegedExceptionAction(arrayOfFile)
{
public Launcher.ExtClassLoader run() throws IOException {
int i = this.val$dirs.length;
for (int j = 0; j < i; j++) {
MetaIndex.registerDirectory(this.val$dirs[j]);
}
return new Launcher.ExtClassLoader(this.val$dirs);
} } );
} catch (PrivilegedActionException localPrivilegedActionException) {
}
throw ((IOException)localPrivilegedActionException.getException());
}
void addExtURL(URL paramURL)
{
super.addURL(paramURL);
}
public ExtClassLoader(File[] paramArrayOfFile)
throws IOException
{
super(null, Launcher.factory);
}
private static File[] getExtDirs() {
String str = System.getProperty("java.ext.dirs");
File[] arrayOfFile;
if (str != null) {
StringTokenizer localStringTokenizer = new StringTokenizer(str, File.pathSeparator);
int i = localStringTokenizer.countTokens();
arrayOfFile = new File[i];
for (int j = 0; j < i; j++)
arrayOfFile[j] = new File(localStringTokenizer.nextToken());
}
else {
arrayOfFile = new File[0];
}
return arrayOfFile;
}
private static URL[] getExtURLs(File[] paramArrayOfFile) throws IOException {
Vector localVector = new Vector();
for (int i = 0; i < paramArrayOfFile.length; i++) {
String[] arrayOfString = paramArrayOfFile[i].list();
if (arrayOfString != null) {
for (int j = 0; j < arrayOfString.length; j++) {
if (!arrayOfString[j].equals("meta-index")) {
File localFile = new File(paramArrayOfFile[i], arrayOfString[j]);
localVector.add(Launcher.getFileURL(localFile));
}
}
}
}
URL[] arrayOfURL = new URL[localVector.size()];
localVector.copyInto(arrayOfURL);
return arrayOfURL;
}
public String findLibrary(String paramString)
{
paramString = System.mapLibraryName(paramString);
URL[] arrayOfURL = super.getURLs();
Object localObject = null;
for (int i = 0; i < arrayOfURL.length; i++)
{
File localFile1 = new File(arrayOfURL[i].getPath()).getParentFile();
if ((localFile1 != null) && (!localFile1.equals(localObject)))
{
String str = VM.getSavedProperty("os.arch");
if (str != null) {
localFile2 = new File(new File(localFile1, str), paramString);
if (localFile2.exists()) {
return localFile2.getAbsolutePath();
}
}
File localFile2 = new File(localFile1, paramString);
if (localFile2.exists()) {
return localFile2.getAbsolutePath();
}
}
localObject = localFile1;
}
return null;
}
private static AccessControlContext getContext(File[] paramArrayOfFile)
throws IOException
{
PathPermissions localPathPermissions = new PathPermissions(paramArrayOfFile);
ProtectionDomain localProtectionDomain = new ProtectionDomain(new CodeSource(localPathPermissions.getCodeBase(), (Certificate[])null), localPathPermissions);
AccessControlContext localAccessControlContext = new AccessControlContext(new ProtectionDomain[] { localProtectionDomain });
return localAccessControlContext;
}
static
{
ClassLoader.registerAsParallelCapable();
}
}
复制代码
这两个类所依附的外部类sun.misc.Launcher的部分源码:
Launcher构造源码.jpg
(184.6 KB, 下载次数: 18)
下载附件
2013-8-27 19:05 上传
作者:
以防万一
时间:
2013-8-30 00:40
{:soso_e176:}
亲,请问下问题是否解决?
如果已解决请及时将未解决改为已解决
如果未解决请回帖追问
三天未回复的将视为已解决
详情参考:
如何更改分类
保持队形,谢谢合作
{:soso_e181:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2