·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » 在java调试中如何获得被装载类的实际路径

在java调试中如何获得被装载类的实际路径

类别: JAVA教程  评论数:0 总得分:0
java.security.CodeSource;
java.security.ProtectionDomain;

CodeSource 可以给出类的原始URL的位置,但并不是每个类都有CodeSource。如果类通过引导类装载器装载,CodeSource为空。

import java.lang.reflect.Method;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;


public class Debug
{

public static void displayClassInfo(Class clazz, StringBuffer results)
{
displayClassInfo(clazz, results, true);
}
public static void displayClassInfo(Class clazz, StringBuffer results,
boolean showParentClassLoaders)
{

ClassLoader cl = clazz.getClassLoader();
results.append("/n"+clazz.getName()+"("+Integer.toHexString(clazz.hashCode())+").ClassLoader="+cl);
ClassLoader parent = cl;
while( parent != null )
{
results.append("/n.."+parent);
URL[] urls = getClassLoaderURLs(parent);
int length = urls != null ? urls.length : 0;
for(int u = 0; u < length; u ++)
{
results.append("/n...."+urls[u]);
}
if( showParentClassLoaders == false )
break;
if( parent != null )
parent = parent.getParent();
}
CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource();
if( clazzCS != null )
results.append("/n++++CodeSource: "+clazzCS);
else
results.append("/n++++Null CodeSource");

results.append("/nImplemented Interfaces:");
Class[] ifaces = clazz.getInterfaces();
for(int i = 0; i < ifaces.length; i ++)
{
Class iface = ifaces[i];
results.append("/n++"+iface+"("+Integer.toHexString(iface.hashCode())+")");
ClassLoader loader = ifaces[i].getClassLoader();
results.append("/n++++ClassLoader: "+loader);
ProtectionDomain pd = ifaces[i].getProtectionDomain();
CodeSource cs = pd.getCodeSource();
if( cs != null )
results.append("/n++++CodeSource: "+cs);
else
results.append("/n++++Null CodeSource");
}
}

/** Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so
that non-URLClassLoader class loaders, or class loaders that override
getURLs to return null or empty, can provide the true classpath info.
*/
public static URL[] getClassLoaderURLs(ClassLoader cl)
{
URL[] urls = {};
try
{
Class returnType = urls.getClass();
Class[] parameterTypes = {};
Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
if( returnType.isAssignableFrom(getURLs.getReturnType()) )
{
Object[] args = {};
urls = (URL[]) getURLs.invoke(cl, args);
}
}
catch(Exception ignore)
{
}
return urls;
}

}






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1