更新時間:2015年12月29日13時36分 來源:傳智播客Java培訓(xùn)學(xué)院 瀏覽次數(shù):
package cn.itcast.jdkproxy; public interface IUserDao { public void save(); } |
package cn.itcast.jdkproxy; public class UserDaoImpl implements IUserDao{ public void save() { System.out.println("完成保存操作"); } } |
package cn.itcast.jdkproxy; import java.io.File; import java.io.FileOutputStream; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import sun.misc.ProxyGenerator; public class JdkProxyTest { public static void main(String[] args) throws Exception { final IUserDao target = new UserDaoImpl(); IUserDao proxy = (IUserDao) Proxy.newProxyInstance(target.getClass() .getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println(method.getName()); return method.invoke(target, args); } }); byte[] proxyClass = ProxyGenerator.generateProxyClass(proxy.getClass() .getSimpleName(), proxy.getClass().getInterfaces()); //將字節(jié)碼文件保存到D盤,文件名為$Proxy0.class FileOutputStream outputStream = new FileOutputStream(new File( "d:\\$Proxy0.class")); outputStream.write(proxyClass); outputStream.flush(); outputStream.close(); } } |
import cn.itcast.jdkproxy.IUserDao; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements IUserDao { private static Method m3; private static Method m1; private static Method m0; private static Method m2; public $Proxy0(InvocationHandler paramInvocationHandler) throws { super(paramInvocationHandler); } public final void save() throws { try { this.h.invoke(this, m3, null); return; } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final boolean equals(Object paramObject) throws { try { return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue(); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final int hashCode() throws { try { return ((Integer)this.h.invoke(this, m0, null)).intValue(); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final String toString() throws { try { return (String)this.h.invoke(this, m2, null); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } static { try { m3 = Class.forName("cn.itcast.jdkproxy.IUserDao").getMethod("save", new Class[0]); m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); return; } catch (NoSuchMethodException localNoSuchMethodException) { throw new NoSuchMethodError(localNoSuchMethodException.getMessage()); } catch (ClassNotFoundException localClassNotFoundException) { } throw new NoClassDefFoundError(localClassNotFoundException.getMessage()); } } |
Java培訓(xùn)實(shí)戰(zhàn)教程之jdk7的新特性之一
2015-12-29Java培訓(xùn)實(shí)戰(zhàn)教程之Java的動態(tài)代理
2015-12-29Java培訓(xùn)實(shí)戰(zhàn)教程之JavaNIO
2015-12-29Java培訓(xùn)實(shí)戰(zhàn)教程之Hibernate使用Oracle數(shù)據(jù)庫遇到的幾個問題
2015-12-29Java培訓(xùn)實(shí)戰(zhàn)教程之CKEditor+CKFinder整合項目開發(fā)文本編輯器功能
2015-12-29Java培訓(xùn)實(shí)戰(zhàn)教程之a(chǎn)ngularJS的雙向數(shù)據(jù)綁定
2015-12-29