您的位置:首页 > 其它

反射与接口

2014-06-21 22:14 197 查看
今天看了动态代理的原理
<span style="white-space:pre">		</span>Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class);
Collection collection2 = (Collection)constructor.newInstance(new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object object = method.invoke(arrayList, args);
return object;
}

});
collection2.add("aa");
collection2.add("bb");
System.out.println(collection2.toString());

可以猜测动态生成的代理类的结构如下:

<span style="white-space:pre">		</span>public class ProxyClass{
<span style="white-space:pre">			</span>public ProxyClass(InvocationHandle handle){
      <span style="white-space:pre">				</span>this.handle = handle;
<span style="white-space:pre">			</span>}

<span style="white-space:pre">			</span>public void add(Object obj){
      <span style="white-space:pre">				</span>handle.invoke(this,this.getClass().getMethod("add“),obj);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}


上面有行代码method.invoke(arrayList, args)

我想为什么这个method.invoke()可以接受arrayList对象

我开始是这样认为的,认为通过这个proxyClass类得到Class对象,然后再得到的"add"这个方法对象,调用时是只能传入ProxyClass类产生的实例。

但结果不是,这个方法还能接受ArrayList对象的实例。

后面通过如下代码发现:

public class GenericMethod implements Fu{
public static void main(String[] args) throws Exception {
//Class clazz = GenericMethod.class;
//由这个GenericMethod类的字节码得到的show方法调用时只能传入GenericMethod对象实例。否则调用会出错
Class clazz = Fu.class;//由这个父类的直字节码得到的show方法调用时只能所有的其子类对象实例,并能成功调用show方法
Method method = clazz.getMethod("show", null);
GenericMethod genericMethod = new GenericMethod();
GenericMethod2 genericMethod2 = new GenericMethod2();
method.invoke(genericMethod2, null);
}
@Override
public void show() {
System.out.println("GenericMethod");
}
}
class GenericMethod2 implements Fu{

@Override
public void show() {
System.out.println("GenericMethod2");
}
}
interface Fu{
void show();
}


否则的话必须一一对应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐