黑马程序员技术交流社区
标题:
分享一个代理中invoke底层调用
[打印本页]
作者:
浪无痕-陈文坤
时间:
2013-8-11 23:33
标题:
分享一个代理中invoke底层调用
本帖最后由 浪无痕-陈文坤 于 2013-8-13 20:21 编辑
出处
http://hi.baidu.com/malecu/item/9e0edc115cb597a1feded5a0
1.首先先把Proxy.class备份个,或rt.jar备份一个。2.把rt.jar中的Proxy.class去掉(我这用360压缩软件),然后在src中把Proxy.java的503行下面添加
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces);
ByteArrayInputStream ba = new ByteArrayInputStream(proxyClassFile);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int lengs = 0;
while((lengs=ba.read(proxyClassFile)) != -1)
{
bo.write(proxyClassFile,0,lengs);
bo.writeTo(new FileOutputStream("c:\\txt.class"));
}
3.在方法中直接抛出异常,没几个方法,为了避免出错,全抛算了。
4.然后进行编译,再把此Proxy.class用压缩软件导进去就行。
5.写代理类,出处中有,
6.用反编译器查看结果如下:(反编译器我有分享过)
import com.ml.test.Manager;
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 Manager
{
private static Method m1;
private static Method m0;
private static Method m3;
private static Method m2;
public $Proxy0(InvocationHandler paramInvocationHandler)
throws
{
super(paramInvocationHandler);
}
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 void modify()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
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
{
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]);
m3 = Class.forName("com.ml.test.Manager").getMethod("modify", 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());
}
}
好东西!!感谢出处作者,红色部分是自己写的,也许有不足之处(io刚刚有点入门),希望对大家有所帮助
cwk.zip
2013-8-11 23:33 上传
点击文件名下载附件
705.74 KB, 下载次数: 59
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2