在java调试中如何获得被装载类的实际路径
2008-01-05 19:19:01 来源:WEB开发网核心提示:java.security.CodeSource;java.security.PRotectionDomain;CodeSource 可以给出类的原始URL的位置,但并不是每个类都有CodeSource,在java调试中如何获得被装载类的实际路径,假如类通过引导类装载器装载,CodeSource为空
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.
更多精彩
赞助商链接