-
-
-
-
-
-
- public class ProxyStudy {
-
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws Exception {
-
-
- Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- System.out.println("动态产生的类名为:" + clazzProxy.getName());
- System.out.println("----------获取动态产生的类的构造方法---------");
- Constructor[] constructors = clazzProxy.getConstructors();
- int i = 1;
- for (Constructor constructor : constructors) {
- System.out.println("第" + (i++) + "个构造方法名:" + constructor.getName());
- Class[] parameterClazz = constructor.getParameterTypes();
- System.out.println("第" + (i++) + "个构造方法参数:" + Arrays.asList(parameterClazz));
- }
- System.out.println("----------获取动态产生的类的普通方法---------");
- Method[] methods = clazzProxy.getDeclaredMethods();
- for (int j = 0; j < methods.length; j++) {
- Method method = methods[j];
- System.out.println("第" + (j + 1) + "个普通方法名:" + method.getName());
- Class[] parameterClazz = method.getParameterTypes();
- System.out.println("第" + (j + 1) + "个普通方法参数:" + Arrays.asList(parameterClazz));
- }
- System.out.println("---------获取动态代理对象的构造方法---------");
-
-
-
- Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class);
-
-
- Collection proxyBuildCollection = (Collection) constructor
- .newInstance(new InvocationHandler() {
-
-
-
-
- ArrayList target = new ArrayList();
-
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable {
- System.out.println("执行目标" + method.getName() + "方法之前:"
- + System.currentTimeMillis());
- Object result = method.invoke(target, args);
- System.out.println("执行目标" + method.getName() + "方法之后:"
- + System.currentTimeMillis());
- return result;
- }
-
- });
- proxyBuildCollection.clear();
- proxyBuildCollection.add("abc");
- proxyBuildCollection.add("dbc");
- System.out.println(proxyBuildCollection.size());
- System.out.println(proxyBuildCollection.getClass().getName());
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- System.out.println("-------------------下面的写法更简便--------------------");
-
-
- Collection proxyBuildCollection2 = (Collection) Proxy.newProxyInstance(
- Collection.class.getClassLoader(),
- new Class[] { Collection.class },
-
- new InvocationHandler() {
- ArrayList target = new ArrayList();
-
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable {
- System.out.println(method.getName() + "执行之前...");
- if (null != args) {
- System.out.println("方法的参数:" + Arrays.asList(args));
- } else {
- System.out.println("方法的参数:" + null);
- }
- Object result = method.invoke(target, args);
- System.out.println(method.getName() + "执行之后...");
- return result;
- }
- });
- proxyBuildCollection2.add("abc");
- proxyBuildCollection2.size();
- proxyBuildCollection2.clear();
- proxyBuildCollection2.getClass().getName();
-
- System.out.println("-------------------对JDK动态代理的重构--------------------");
- Set proxySet = (Set) buildProxy(new HashSet(), new MyAdvice());
- proxySet.add("abc");
- proxySet.size();
- }
-
-
-
-
-
-
-
- public static Object buildProxy(final Object target,final AdviceInter advice) {
- Object proxyObject = Proxy.newProxyInstance(
- target.getClass().getClassLoader(),
- target.getClass().getInterfaces(),
-
- new InvocationHandler() {
-
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable {
- advice.beforeMethod(target, method, args);
- Object result = method.invoke(target, args);
- advice.afterMethod(target, method, args);
- return result;
- }
- });
- return proxyObject;
- }
-
- }
-
-
-
-
-
-
- public class MyAdvice implements AdviceInter {
-
- public void afterMethod(Object target, Method method, Object[] args) {
- System.out.println("目标对象为:" + target.getClass().getName());
- System.out.println(method.getName() + "执行完毕!");
- }
-
- public void beforeMethod(Object target, Method method, Object[] args) {
- System.out.println(method.getName() + "开始执行");
- if (null != args) {
- System.out.println("参数为:" + Arrays.asList(args));
- } else {
- System.out.println("参数为:" + null);
- }
- }
- }
-
-
-
-
-
-
- public interface AdviceInter {
-
-
-
-
- public void beforeMethod(Object target, Method method, Object[] args);
-
-
-
-
-
-
-
-
-
-
-
- public void afterMethod(Object target, Method method, Object[] args);
- }
转自:http://zmx.iteye.com/blog/678416