自定义ClassLoader_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 自定义ClassLoader

自定义ClassLoader

 2014/12/14 19:03:00  宋建勇  程序员俱乐部  我要评论(0)
  • 摘要:一、自定义类加载器代码:packagefoo;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importorg.apache.commons.io.FileUtils;importorg.apache.commons.io.IOUtils;importorg.apache.commons.lang.StringUtils;importorg.apache.commons.logging.Log
  • 标签:自定义 class
一、自定义类加载器代码:
class="java" name="code">package foo;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyLocalClassLoader extends ClassLoader {

	private Log log = LogFactory.getLog(this.getClass());

	public MyLocalClassLoader() {
		super();
	}

	/* classes文件存放目录 */
	private String classesDir;

	public MyLocalClassLoader(String classesDir) {
		super();
		this.classesDir = classesDir;
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {

		String classFileName = classesDir + File.separator + StringUtils.replaceChars(name, '.', File.separatorChar) + ".class";

		File classF = new File(classFileName);

		if (classF.exists()) {

			InputStream input = null;

			try {

				input = FileUtils.openInputStream(classF);

				byte[] b = IOUtils.toByteArray(input);

				return defineClass(name, b, 0, b.length);

			} catch (IOException e) {

				log.error(e.getMessage(), e);

			} finally {
				IOUtils.closeQuietly(input);
			}

		} else {
			log.error(classFileName + "文件不存在!");
		}

		return super.findClass(name);

	}

	protected void recursive_loadClass(boolean resolve) throws ClassNotFoundException {
		
		System.out.println("MyLocalClassLoader.recursive_loadClass()");
		
	}

}

测试:
package foo;

import java.lang.reflect.InvocationTargetException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyLocalClassLoaderDemo {

	private static Log log = LogFactory.getLog(MyLocalClassLoaderDemo.class);

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException,
			SecurityException, IllegalArgumentException, InvocationTargetException {

		ResourceBundle res = ResourceBundle.getBundle("foo/conf");

		Class<?> clazz = new MyLocalClassLoader(res.getString("classesDir")).loadClass("demo.ClassDemo");

		Object obj = clazz.newInstance();

		log.info(obj.toString());

		clazz = new MyLocalClassLoader(res.getString("classesDir")).loadClass("demo.Parent");
		clazz = new MyLocalClassLoader(res.getString("classesDir")).loadClass("demo.Child");

		obj = clazz.newInstance();

		Class<?>[] cc = {};
		Object[] oo = {};

		log.info(obj.getClass().getMethod("hello", cc).invoke(obj, oo));

	}

}

测试结果:

辅助测试类:
package demo;

public class ClassDemo {

	@Override
	public String toString() {
		
		return "ClassDemo.toString()";
		
	}

}

package demo;

public abstract class Parent {

	public String hello() {
		return "Parent.hello()";
	}
}

package demo;

public class Child extends Parent {

	@Override
	public String hello() {

		return "Child.hello()";

	}

}
  • 大小: 16.4 KB
  • 查看图片附件
上一篇: 张朝阳:人的智能和人工智能重塑信息排序 下一篇: 没有下一篇了!
发表评论
用户名: 匿名