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

2008-02-23 09:18:50来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

下面的一个小工具类提供了对系统类加载器和扩展类加载器的动态控制能力.
可以在程序中加入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) {

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:各种排序算法java实现

下一篇:JDBC系列教程(五)---准备语句