日期:2014-05-20 浏览次数:20945 次
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 ArrayListTest implements InvocationHandler { private Object target; public ArrayListTest() { } public ArrayListTest(Object target) { this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(target, args); } public static Object factory(Object target) { ArrayListTest alt = new ArrayListTest(target); return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), alt); } public static void main(String[] args) { @SuppressWarnings("unchecked") List<String> arraylist = (List<String>) factory(new ArrayList<String>()); arraylist.add("hello"); System.out.println(arraylist); } }