最近在学习java.lang.reflect.Proxy包,但对网上的有段代码是在看不懂,请高手指教
//FooProxy 实现了 接口FooI
FooProxy fooProxy = new FooProxy();
InvocationHandler invoker = new Invoker(fooProxy);// FooI fooI = (FooI)Proxy.newProxyInstance(FooI.class.getClassLoader(),new Class[]{FooI.class},invoker);
FooI fooI = (FooI)Proxy.newProxyInstance(fooProxy.getClass().getClassLoader(),fooProxy.getClass().getInterfaces(),invoker); 我对于Proxy.newProxyInstance(FooI.class.getClassLoader(),new Class[]{FooI.class},invoker)中的new Class[]{FooI.class},不清楚是什么意思,以前也没这么写过,难道它和fooProxy.getClass().getInterfaces()是相同的吗?
//FooProxy 实现了 接口FooI
FooProxy fooProxy = new FooProxy();
InvocationHandler invoker = new Invoker(fooProxy);// FooI fooI = (FooI)Proxy.newProxyInstance(FooI.class.getClassLoader(),new Class[]{FooI.class},invoker);
FooI fooI = (FooI)Proxy.newProxyInstance(fooProxy.getClass().getClassLoader(),fooProxy.getClass().getInterfaces(),invoker); 我对于Proxy.newProxyInstance(FooI.class.getClassLoader(),new Class[]{FooI.class},invoker)中的new Class[]{FooI.class},不清楚是什么意思,以前也没这么写过,难道它和fooProxy.getClass().getInterfaces()是相同的吗?
哈哈 new了个Class类型的数组而已 里面只有一个元素
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。此方法相当于:
Proxy.getProxyClass(loader, interfaces).
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
Proxy.newProxyInstance 抛出 IllegalArgumentException,原因与 Proxy.getProxyClass 相同。
参数:
loader - 定义代理类的类加载器
interfaces - 代理类要实现的接口列表
h - 指派方法调用的调用处理程序
返回:
一个带有代理类的指定调用处理程序的代理实例,它由指定的类加载器定义,并实现指定的接口
抛出:
IllegalArgumentException - 如果违反传递到 getProxyClass 的参数上的任何限制
NullPointerException - 如果 interfaces 数组参数或其任何元素为 null,或如果调用处理程序 h 为 null