手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

类加载器工具类:动态设置classpath,获得加载类列表等

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

下面的一个小工具类提供了对系统类加载器和扩展类加载器的动态控制能力.
可以在程序中加入classpath,当然也可以获得类加载器加载的类列表,相信
Java的动态能力!


package org.rut.core;

import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import org.rut.RootException;

import sun.misc.Launcher;

/**
* @author treeroot
* @since 2006-2-12
* @version 1.0
*/
public class ClassLoaderUtil {
private static Field classes;

private static Method addURL;
static {
try {
classes = ClassLoader.class.getDeclaredField("classes");
addURL = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[] { URL.class });
} catch (Exception e) {
//won't happen ,but remain it
throw new RootException(e);
}
classes.setAccessible(true);
addURL.setAccessible(true);
}

private static URLClassLoader system = (URLClassLoader) getSystemClassLoader();

private static URLClassLoader ext = (URLClassLoader) getExtClassLoader();

public static ClassLoader getSystemClassLoader() {
return ClassLoader.getSystemClassLoader();
}

public static ClassLoader getExtClassLoader() {
return getSystemClassLoader().getParent();
}

/**
* 获得加载的类
*
* @return
*/
public static List getClassesLoadedBySystemClassLoader() {
return getClassesLoadedByClassLoader(getSystemClassLoader());
}

public static List getClassesLoadedByExtClassLoader() {
return getClassesLoadedByClassLoader(getExtClassLoader());
}

public static List getClassesLoadedByClassLoader(ClassLoader cl) {
try {
return (List) classes.get(cl);
} catch (Exception e) {
throw new RootException(e);
}
}

public static URL[] getBootstrapURLs() {
return Launcher.getBootstrapClassPath().getURLs();
}

public static URL[] getSystemURLs() {
return system.getURLs();
}

public static URL[] getExtURLs() {
return ext.getURLs();
}

private static void list(PrintStream ps, URL[] classPath) {
for (int i = 0; i < classPath.length; i ) {
ps.println(classPath[i]);
}
}

public static void listBootstrapClassPath() {
listBootstrapClassPath(System.out);
}

public static void listBootstrapClassPath(PrintStream ps) {
ps.println("BootstrapClassPath:");
list(ps, getBootstrapClassPath());
}

public static void listSystemClassPath() {
listSystemClassPath(System.out);
}

public static void listSystemClassPath(PrintStream ps) {
ps.println("SystemClassPath:");
list(ps, getSystemClassPath());
}

public static void listExtClassPath() {
listExtClassPath(System.out);
}

public static void listExtClassPath(PrintStream ps) {
ps.println("ExtClassPath:");
list(ps, getExtClassPath());
}

public static URL[] getBootstrapClassPath() {
return getBootstrapURLs();
}

public static URL[] getSystemClassPath() {
return getSystemURLs();
}

public static URL[] getExtClassPath() {
return getExtURLs();
}

public static void addURL2SystemClassLoader(URL url) {
try {
addURL.invoke(system, new Object[] { url });
} catch (Exception e) {
throw new RootException(e);
}
}

public static void addURL2ExtClassLoader(URL url) {
try {
addURL.invoke(ext, new Object[] { url });
} catch (Exception e) {
throw new RootException(e);
}
}

public static void addClassPath(String path) {
addClassPath(new File(path));
}

public static void addExtClassPath(String path) {
addExtClassPath(new File(path));
}

public static void addClassPath(File dirOrJar) {
try {
addURL2SystemClassLoader(dirOrJar.toURL());
} catch (MalformedURLException e) {
throw new RootException(e);
}
}

public static void addExtClassPath(File dirOrJar) {

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!