标签:
package classLoader;import java.io.File;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.MalformedURLException;import java.net.URL;import org.apache.log4j.Logger;/*** @author eblly*/public class ClassLoaderTs {public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{IsolatedClassLoader loaderA = null;IsolatedClassLoader loaderB = null;File file = new File("F:/WIN_git/java_learning/target/classes");URL[] urls = new URL[] { file.toURL()};loaderA = new IsolatedClassLoader(urls);loaderB = new IsolatedClassLoader(urls);Class aClass = loaderA.findClass2("classLoader.CannotCastBean");Object obj = aClass.newInstance();Class bClass = loaderB.findClass2("classLoader.CannotCastBean");Object obj2 = bClass.newInstance();System.out.println(" obj class loader : " + obj.getClass().getClassLoader());System.out.println(" obj2 class loader : " + obj2.getClass().getClassLoader());/*** ERROR A: java.lang.IllegalArgumentException: object is not an instance of declaring class* obj2不是loaderA的实例。*/// Method method = aClass.getMethod("test", null);// method.invoke(obj2, null);/*** ERROR B: classLoader.CannotCastBean cannot be cast to classLoader.CannotCastBean* ccb的ClassLoader是AppClassLoader,obj的ClassLoader是loaderA* 不同类加载器实例的实体不可相互转换。*/// System.out.println("CannotCastBean ClassLoader:" + CannotCastBean.class.getClassLoader());// CannotCastBean ccb = (CannotCastBean)obj;/*** ERROR C: java.lang.NoSuchMethodException: classLoader.CannotCastBean.test(classLoader.CannotCastBean)* 此处的CannotCastBean的类加载器是AppClassLoader,* CannotCastBean中的test方法的CannotCastBean类加载器是loaderA(IsolatedClassLoader).* 因此找不到对应的方法。* 如果将CannotCastBean换成String,则正常运行,因为此处和CannotCastBean的String的加载器都为Bootstrap loader。*/Method method = aClass.getMethod("test", CannotCastBean.class);method.invoke(obj,new CannotCastBean());}}
package classLoader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLClassLoader;import java.security.AccessController;import java.security.PrivilegedExceptionAction;import org.apache.log4j.Logger;import com.sun.org.apache.bcel.internal.util.ClassPath;import sun.misc.Resource;/**** @author eblly* @date 2016年2月23日 下午3:16:50* @version 1.0*/public class IsolatedClassLoader extends URLClassLoader {private static final Logger log = Logger.getLogger(IsolatedClassLoader.class);public IsolatedClassLoader(URL[] urls) {super(urls);}@Overrideprotected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {Class loadedClass = findLoadedClass(name);if (loadedClass == null) {try {loadedClass = findClass(name);} catch (ClassNotFoundException e) {// don‘t do anything. Catching this exception is the normal// protocol for// this classloaders telling use they couldn‘t find a class.}}if (loadedClass == null) {loadedClass = super.loadClass(name, resolve);}if (resolve) {resolveClass(loadedClass);}return loadedClass;}public URL getResource(String resName) {URL url = findResource(resName);if (url == null) {url = super.getResource(resName);}return url;}protected Class<?> findClass(String name) throws ClassNotFoundException{return super.findClass(name);}public Class<?> findClass2(String name) throws ClassNotFoundException{return findClass(name);}protected Class<?> findLoaded2Class(String name) {return super.findLoadedClass(name);}}
/*** @author eblly*/public class CannotCastBean {public void test(){System.out.println(" test cannot cast!!!");System.out.println(" this class loader : " + this.getClass().getClassLoader());}public void test(CannotCastBean ccb){System.out.println(" test cannot cast!!!");System.out.println(" this class loader : " + this.getClass().getClassLoader());System.out.println(" ccb class loader : " + ccb.getClass().getClassLoader());CannotCastBean2 ccb2 = new CannotCastBean2();System.out.println(ccb.getClass().getClassLoader());System.out.println(ccb2.getClass().getClassLoader());}public void test(String ccb){System.out.println(" test cannot cast!!!");System.out.println(" ccb class loader : " + ccb.getClass().getClassLoader());//ccb class loader : nullCannotCastBean2 ccb2 = new CannotCastBean2();System.out.println("this:" + this.getClass().getClassLoader());//this:classLoader.IsolatedClassLoader@2e81d81eSystem.out.println("ccb2:" + ccb2.getClass().getClassLoader());//ccb2:classLoader.IsolatedClassLoader@2e81d81eAbean a = new Abean();System.out.println("a:" + a.getClass().getClassLoader());//a:classLoader.IsolatedClassLoader@2e81d81eHashMap map = new HashMap();System.out.println("map:" + map.getClass().getClassLoader());//map:null}}
标签:
原文地址:http://www.cnblogs.com/eblly/p/5550662.html