本文实例为大家分享了手动实现的一个简单JDK版动态代理,供大家参考,具体内容如下
成都创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为柳河企业提供专业的成都网站建设、网站建设,柳河网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
一.实现步骤
1.根据目标类的接口类型生成代理类的java文件。
2.编译代理类java文件为.class字节码文件。
3.将编译好的字节码文件加载到jvm中。
4.生成代理类对象并返回。
二.代码实现
1.Proxy类
public class CLProxy { private static final String ENTER= "\r\n"; private static final String PAKAGE=CLProxy.class.getPackage().toString()+";"; private static final String CLASS_NAME="$Proxy"; private static final AtomicInteger NUMBER= new AtomicInteger(0); public static Object newProxyInstance(CLClassLoader classLoader, Class<?>[] interfaces,CLInvocationHandler h) throws Exception{ String className =CLASS_NAME+NUMBER.getAndIncrement(); //遍历所有的接口生成java 文件 String javaString = createJavaString(interfaces, className); String parentPath = CLProxy.class.getResource("").getPath(); File file =new File(parentPath,className+".java" ); FileWriter writer = new FileWriter(file); writer.write(javaString); writer.flush(); writer.close(); //System.out.println(file); //编译 JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager standardFileManager = systemJavaCompiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> javaFileObjects = standardFileManager.getJavaFileObjects(file); JavaCompiler.CompilationTask task = systemJavaCompiler.getTask(null, standardFileManager, null, null, null, javaFileObjects); task.call(); standardFileManager.close(); //创建实例 Class<?> aClass = classLoader.findClass(className); Constructor<?> constructor = aClass.getConstructor(CLInvocationHandler.class); Object instance = constructor.newInstance(h); //file.delete(); return instance; } /** * 生成java 文件 * @param interfaces * @return */ private static String createJavaString(Class<?>[] interfaces , String className ){ StringBuffer buffer = new StringBuffer(); buffer.append(PAKAGE+ENTER); buffer.append("import java.lang.reflect.Method;"+ ENTER); StringBuffer interfaceString= new StringBuffer(); int length= interfaces.length; for (int i = 0; i<length ; ++i){ interfaceString.append(interfaces[i].getName()); if (i!=length-1){ interfaceString.append(","); } } buffer.append("public final class "); buffer.append(className); buffer.append(" implements "); buffer.append(interfaceString); buffer.append(" {"+ENTER); buffer.append("private CLInvocationHandler handler;"+ENTER); buffer.append("public "+className+"(CLInvocationHandler handler) {"+ENTER); buffer.append(" this.handler= handler;"+ENTER); buffer.append("}"+ENTER); for (int i =0 ;i<length;++i){ Class<?> clazz= interfaces[i]; Method[] methods = clazz.getMethods(); for (Method method : methods){ String returnTypeString = method.getReturnType().getName(); Class<?>[] parameterTypes = method.getParameterTypes(); StringBuffer paramTypeString = new StringBuffer(); StringBuffer methodParamString = new StringBuffer(); StringBuffer invokeParamString = new StringBuffer(); paramTypeString.append("new Class[]{"); int paramLength= parameterTypes.length; for (int j =0 ; j<paramLength ;++j){ Class<?> paramClazz= parameterTypes[j]; paramTypeString.append(paramClazz.getName()+".class"); String paramFieldName = "var"+j; methodParamString.append(paramClazz.getName() +" "+paramFieldName); invokeParamString.append(paramFieldName); if (j!= paramLength-1){ paramTypeString.append(","); methodParamString.append(","); invokeParamString.append(","); } } paramTypeString.append("}"); int modifiers = method.getModifiers(); if (Modifier.isPublic(modifiers)){ buffer.append("public"); }else if (Modifier.isPrivate(modifiers)){ buffer.append("private"); }else if (Modifier.isProtected(modifiers)){ buffer.append("protected"); } buffer.append(" final "+returnTypeString+" "+ method.getName()+"("+methodParamString+"){"+ ENTER); buffer.append("try{"+ENTER); buffer.append("Method method = "+clazz.getName()+".class.getMethod(\""+method.getName()+"\","+paramTypeString+" );"+ENTER); if (!"void".equals(returnTypeString)){ buffer.append("return ("+returnTypeString+")"); } if (invokeParamString.toString().length()==0){ invokeParamString.append("null"); }else{ invokeParamString = new StringBuffer("new Object[]{"+invokeParamString.toString()+"}"); } buffer.append("this.handler.invoke(this,method,"+invokeParamString+");"+ENTER); buffer.append("}catch(Throwable e){"+ENTER); buffer.append("e.printStackTrace();"+ENTER); buffer.append("}"+ENTER); if (!"void".equals(returnTypeString)){ buffer.append("return null;"+ENTER); } buffer.append("}"+ENTER); } } buffer.append("}"); return buffer.toString(); } public static void main(String[] args) throws Exception { Person person = (Person)CLProxy.newProxyInstance(new CLClassLoader(), XiaoMing.class.getInterfaces(), new CLInvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before"); Object result= method.invoke(new XiaoMing(), args); System.out.println("after"); return result; } }); String laoxu = person.call("laoxu"); System.out.println(laoxu); /* person.eat(); Class<?>[] interfaces = person.getClass().getInterfaces(); for (Class<?> in:interfaces){ System.out.println(in.getName()); } */ Person person2= (Person)CLProxy.newProxyInstance(new CLClassLoader(), XiaoMing.class.getInterfaces(), new CLInvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before"); Object result= method.invoke(new XiaoMing(), args); System.out.println("after"); return result; } }); System.out.println(person2.getClass()); } }
2.InvocationHandler接口
public interface CLInvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable; }
3.ClassLoader类加载器
public class CLClassLoader extends ClassLoader { private File classPathFile; public CLClassLoader(){ String classPath = CLClassLoader.class.getResource("").getPath(); this.classPathFile= new File(classPath); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { String className = CLClassLoader.class.getPackage().getName()+"."+name; if (classPathFile!= null ){ File classFile = new File(classPathFile, name.replace("\\.", "/") + ".class"); if (classFile.exists()){ FileInputStream inputStream =null; ByteArrayOutputStream outputStream = null; try{ inputStream=new FileInputStream(classFile); outputStream= new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len=inputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); } return defineClass(className,outputStream.toByteArray(),0,outputStream.size()); }catch (Exception e){ e.printStackTrace(); }finally { if (inputStream!= null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream!=null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } return super.findClass(name); } }
4.测试使用的接口与目标类
//测试使用的接口 public interface Person { void eat(); String call(String name); } //测试使用目标类 public class XiaoMing implements Person { @Override public void eat() { System.out.println("吃东西"); } //@Override public String call(String name) { return name; } }
注意测试方法在CLProxy 的main 方法中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
本文名称:一个简单JDK版动态代理
网站URL:https://www.cdcxhl.com/article12/jdiidc.html
成都网站建设公司_创新互联,为您提供企业网站制作、微信小程序、做网站、App开发、品牌网站设计、网站收录
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联