DR_WH 2016-03-31 10:37 采纳率: 0%
浏览 1248

java动态代理的基础问题


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;

public class VectorProxy implements InvocationHandler {
    private Object proxyObj;

    public VectorProxy(Object obj) {
        proxyObj = obj;
    }


    public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
        System.out.println("Before calling : " + method);
        if (args != null) {
            for (Object o : args) {
                System.out.println(o);
            }
        }

        Object object = method.invoke(proxyObj, args);

        System.out.println("After calling : " + method);

        return object;
    }

    public static Object factory(Object obj) {
        Class<?> classType = obj.getClass();

        return Proxy.newProxyInstance(classType.getClassLoader(), obj.getClass().getInterfaces(), new VectorProxy(obj));
    }

    public static void main(String[] args) {
        List list = (List) factory(new Vector());

        list.add("hello");
    }
}

public Object invoke      这个方法的返回值能决定什么?为什么改成null就不对了
  • 写回答

2条回答 默认 最新

  • threenewbee 2016-03-31 10:42
    关注

    这个方法返回的是你执行的那个method方法的返回值。

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)