package com.proxy; import org.junit.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; public class ProxyTest { @Test public void test01(){ List<String> list = new ArrayList<>(); List<String> proxyInstance = (List<String >)Proxy.newProxyInstance( list.getClass().getClassLoader(), list.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.getName().equals("add")){ System.out.println("代理執行..."); } return method.invoke(list,args); } }); proxyInstance.add("你好"); System.out.println(list); proxyInstance.get(0); System.out.println(list); } }