类加载器
Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader,AppClassLoaderimport java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class MyClassLoader extends ClassLoader { public static void main(String[] args)throws Exception { String srcPath=args[0]; String destDir=args[1]; FileInputStream fis=new FileInputStream(srcPath); String destFileName=srcPath.substring(srcPath.lastIndexOf('/')+1);//路径的File,加1是说明从盘符下面开始 String destPath=destDir+"\\"+destFileName; FileOutputStream fos=new FileOutputStream(destPath); cypher(fis,fos); fis.close(); fos.close(); } private static void cypher(InputStream ips,OutputStream ops) throws Exception{ int b=-1; while(ips.read()!=-1){ ops.write(b); ops.write(b^0xff); } } private String classDir; @Override//类加载器 protected Class<?> findClass(String name) throws ClassNotFoundException { // TODO Auto-generated method stub String classFileName=classDir+"\\"+name+".class";//通过类找出硬盘上的文件。 try { FileInputStream fis=new FileInputStream(classFileName); ByteArrayOutputStream bos=new ByteArrayOutputStream();//定义一个字节数据流 cypher(fis,bos);//解密 fis.close(); byte[] bytes=bos.toByteArray(); return defineClass(bytes, 0, bytes.length); } catch (Exception e) {//子类不能被父类抛出 e.printStackTrace(); }//加载这个文件 return super.findClass(name);//调用父类的class } //去哪个目录下寻找那份文件 public MyClassLoader(){ } public MyClassLoader(String clasPath){ this.classDir=classDir; } }
/* Exception in thread "main" java.lang.IllegalAccessException: Class MyClassLoader can not access a member of class MyTest with modifiers "" */ /* class MyTest { public void test() { System.out.println("hello,www.it315.org"); } } */
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Collection; import javax.xml.ws.spi.Invoker; public class ProxyTest { public static void main(String[] args) throws Exception{ Class clazzProxy1=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class); //对于clazz,我们通常认为它是字节码 System.out.println(clazzProxy1.getName()); System.out.println("begin constructors list-----:"); Constructor[] constructors=clazzProxy1.getConstructors();//得到它的构造方法 for(Constructor constructor:constructors){ String name=constructor.getName(); StringBuilder sBuilder=new StringBuilder();//用 StringBuilder效率更高一点 sBuilder.append('('); Class [] clazzParams=constructor.getParameterTypes();//得到参数的类型,返回的是一个class的数组。 for(Class clazzParam: clazzParams){//取出每个参数的名字 sBuilder.append(clazzParam.getName()).append(','); } if(clazzParams!=null&&clazzParams.length!=0) sBuilder.deleteCharAt(sBuilder.length()-1);//去掉最后一个参数 sBuilder.append(')'); System.out.println(sBuilder.toString()); } //StringBuilder与StringBuffered的区别: //在动态上,都是往字符串中添加字符,在单线程下,用StringBuilder效率要高一点,在多线程下StringBufferd要高点 System.out.println("----------begin methods list----------"); /*$Proxy0() $Proxy0(InvocationHandler,int)*/ Method[] methods = clazzProxy1.getMethods(); for(Method method : methods){ String name = method.getName(); StringBuilder sBuilder = new StringBuilder(name); sBuilder.append('('); Class[] clazzParams = method.getParameterTypes(); for(Class clazzParam : clazzParams){ sBuilder.append(clazzParam.getName()).append(','); } if(clazzParams!=null && clazzParams.length != 0) sBuilder.deleteCharAt(sBuilder.length()-1); sBuilder.append(')'); System.out.println(sBuilder.toString()); } //创建动态类的实例对象用调用方法 System.out.println("-----begin create instance-----"); //Object obj=clazzProxy1.newInstance();//不能这能调用构造参数的实例化方法。 //构造方法接受一个参数,然后再去调用构造方法。 Constructor constructor=clazzProxy1.getConstructor(InvocationHandler.class); class MyInvocationHander1 implements InvocationHandler{ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } } Collection proxy1=(Collection) constructor.newInstance(new MyInvocationHander1()); System.out.println(proxy1); proxy1.clear();//如果不报空指针异常,就说明这个对象是有的。 //proxy1.size();//出错了,那么就判定size方法出问题了,因为size方法有返回值,clear方法没有。 Collection proxy2= (Collection) constructor.newInstance(new InvocationHandler(){ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }); //代理对象 Collection proxy3=(Collection) Proxy.newProxyInstance( Collection.class.getClassLoader(), new Class[] {Collection.class}, new InvocationHandler(){ public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{ ArrayList target=new ArrayList(); long beginTime=System.currentTimeMillis(); Object retVal=method.invoke(target, args); long endTime=System.currentTimeMillis(); System.out.println(method.getName()+" running time of: "+(endTime-beginTime)+"ms"); return retVal; } } ); proxy3.add("zxx");//每调用一个add方法,invoke就被执行 proxy3.add("lhm"); proxy3.add("hjl"); System.out.println(proxy3.size()); } }
import java.lang.reflect.Method; public class MyAdvice implements Advice { long beginTime = 0; public void afterMethod(Method method) { // TODO Auto-generated method stub System.out.println("开始啦!"); long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); } public void beforeMethod(Method method) { // TODO Auto-generated method stub System.out.println("结束啦!"); beginTime = System.currentTimeMillis(); } }
import java.lang.reflect.Method; public interface Advice { void beforeMethod(Method method); void afterMethod(Method method); }
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import cn.itcast.day3.Advice; public class BeanFactory { Properties props = new Properties(); public BeanFactory(InputStream ips){ try { props.load(ips); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Object getBean(String name){ String className = props.getProperty(name); Object bean = null; try { Class clazz = Class.forName(className); bean = clazz.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(bean instanceof ProxyFactoryBean){ Object proxy = null; ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean; try { Advice advice = (Advice)Class.forName(props.getProperty(name + ".advice")).newInstance(); Object target = Class.forName(props.getProperty(name + ".target")).newInstance(); proxyFactoryBean.setAdvice(advice); proxyFactoryBean.setTarget(target); proxy = proxyFactoryBean.getProxy(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return proxy; } return bean; } }
import java.io.InputStream; import java.util.Collection; public class AopFrameworkTest { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties"); Object bean = new BeanFactory(ips).getBean("xxx"); System.out.println(bean.getClass().getName()); ((Collection)bean).clear(); } }
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyFactoryBean { private Advice advice; private Object target; public Advice getAdvice() { return advice; } public void setAdvice(Advice advice) { this.advice = advice; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public Object getProxy() { // TODO Auto-generated method stub Object proxy3 = Proxy.newProxyInstance( target.getClass().getClassLoader(), /*new Class[]{Collection.class},*/ target.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { /*long beginTime = System.currentTimeMillis(); Object retVal = method.invoke(target, args); long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); return retVal;*/ advice.beforeMethod(method); Object retVal = method.invoke(target, args); advice.afterMethod(method); return retVal; } } ); return proxy3; } }
xxx.target=java.util.ArrayList//目标
---------------------- ASP.Net+Unity开发-------- .Net培训期待与您交流! ---------------------