谁调用了InvocationHandler 中的invoke方法?
来源:互联网 发布:windows关机指令 编辑:程序博客网 时间:2024/06/10 10:11
关键问题是它们内在的联系。虽然可以看源代码,包括类库。但是把它们抽象出来,有助于理解。
在客户类(即以下代码的Client类),语句Manager managerProxy=
$Proxy0对象调用过,怎么能执行invoke方法呢?但是请看$Proxy0中的static代码块,这个模块是特殊的,因为当newProxyInstance创建$Proxy0时,它就被初始化。而这个static模块中的getMethod方法加载了这个三个方法,因而它们里面的代码(h.invoke())被执行。
一.相关类及其方法:
java.lang.reflect.Proxy类的newProxyInstance(),是用于创建动态代理类和实例的静态方法.返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
java.lang.reflect.InvocationHandler接口中的invoke(),在代理实例上处理方法调用并返回结果。当与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法。
二.探讨的源代码:
被代理对象的接口及实现类:
public interface Manager {
public void modify();
}
public class ManagerImpl implements Manager {
}
动态代理类:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class BusinessHandler implements InvocationHandler {
private Object object = null;
public BusinessHandler(Object object) {
}
@Override
public Object invoke(Object proxy, Method method, Object[]args)throws Throwable {
}
客户端类:
import java.lang.reflect.Proxy;
public class Client {
public static void main(String[] args) {
}
三.执行结果:
do something before method
*******modify()方法被调用
do something after method
四.机制分析:
Proxy.newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)做了以下几件事.
(1)根据参数loader和interfaces调用方法 getProxyClass(loader,interfaces)创建代理类$Proxy.$Proxy0类 实现了interfaces的接口,并继承了Proxy类.
(2)实例化$Proxy0并在构造方法中把BusinessHandler传过去,接着$Proxy0调用父类Proxy的构造器,为h赋值,如下:
class Proxy{
}
下面是本例的$Proxy0类的源码(好不容易才把它提出来):
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableExcep
public final class $Proxy0 extends Proxy implements Manager{
private static Method m1;
private static Method m0;
private static Method m3;
private static Method m2;
static {
} //static
public $Proxy0(InvocationHandler invocationhandler) {
}
@Override
public final boolean equals(Object obj) {
}
@Override
public final int hashCode() {
}
public final void modify() {
}
@Override
public final String toString() {
}
- 谁调用了InvocationHandler 中的invoke方法?
- 谁调用了InvocationHandler 中的invoke方法?
- InvocationHandler的invoke方法如何被调用?
- InvocationHandler的invoke方法如何被调用?
- InvocationHandler的invoke方法如何被调用?
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler的invoke方法如何被调用?
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler的invoke方法如何被调用?
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- InvocationHandler中invoke()方法的调用问题
- 让精灵按照自己设定的运动轨迹行动(曲线移动)。(以椭圆轨迹为例)。
- Matlab中的simulink\If else模块
- 一些读书笔记(4)
- DB2常用命令
- 举例说明如何使用正则表达式去掉字符串中的一些特殊符号
- 谁调用了InvocationHandler 中的invoke方法?
- WPF之Binding基础一 UI Binding Source
- 备份:优秀博客文章
- wpa_supplicant-2.0交叉编译
- ptrace(PTRACE_PEEKUSER,pid,ORIG_EAX*4,NULL)
- Oracle 恢复 Java 时区数据工具tzupdater
- get和post区别
- Eclipse/MyEclipse 最最常用的快捷键
- php 中 Notice:undefined index 解决方法