当前位置: 移动技术网 > IT编程>开发语言>Java > 详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

2019年07月22日  | 移动技术网IT编程  | 我要评论

详解java jdk 动态代理类分析(java.lang.reflect.proxy)

/** 
 * jdk 动态代理类分析(java.lang.reflect.proxy使用) 
 * 
 * @author 张明学 
 * 
 */ 
public class proxystudy { 
   
  @suppresswarnings("unchecked") 
  public static void main(string[] args) throws exception { 
    // 动态代理类:通用指定类加载器,和接口产生一类 
    // getproxyclass()返回代理类的 java.lang.class 对象,并向其提供类加载器和接口数组。 
    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("---------获取动态代理对象的构造方法---------"); 
    // 动态代理产生的对象的构造方法需要一个实现java.lang.reflect.invocationhandler接口的对象,故不能通过 
    // clazzproxy.newinstance();产生一个对象,可以根据构造方法产生一个对象 
    // invocationhandler 是代理实例的调用处理程序 实现的接口。 
    constructor constructor = clazzproxy.getconstructor(invocationhandler.class); 
 
    // 代理产生的对象 
    collection proxybuildcollection = (collection) constructor 
        .newinstance(new invocationhandler() { 
          // 为什么这里选择arraylist作为目标对象? 
          // 因为这里的constructor是clazzproxy这个动态类的构造方法,clazzproxy是通过proxy.getproxyclass()方法产生的, 
          // 该方法有两个参数,一个是指定类加载器,一个是指定代理要实现的接口,这个接口我上面指定了collection 
          // 而arraylist实现了collection接口,固可以为该动态类的目标对象 
          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()); 
     
    /** 
     * 动态代理:总结如下: 
     * 1,通过proxy.getproxyclass(classloader,interface)方法产生一个动态类的class字节码(clazz) 
     *  该getproxyclass()方法有两个参数:一个是指定该动态类的类加载器,一个是该动态类的要实现的接口(从这里可以看现jdk的动态代理必须要实现一个接口) 
     *   
     * 2,通过第一步的获取的clazz对象可以获取它的构造方法constructor,那么就可以通用constructor的newinstance()方法构造出一个动态实体对象 
     *  但constructor的newinstance()方法需要指定一个实现了invocationhandler接口的类handler,在该类中需要一个目标对象a和实现invoke方法 
     *  目标对象a要求能对第一步中的接口的实现,因为在invoke方法中将会去调用a中的方法并返回结果。 
     *  过程如下:调用动态代理对象proxyobject的x方法 ————> 进入构造方法传进的handler的invoke方法 ————> invoke方法调用handler中的target对象 
     *      的x方法(所以要求target必须要实现构造动态代理类时指定的接口)并返回它的返回值。(其实如果我们代理p类,那么target就可以选中p类,只是要求p必需实现一个接口) 
     *   
     *  那么上述中x方法有哪些呢?除了从object继承过来的方法中除tostring,hashcode,equals外的方法不交给handler外,其它的方法全部交给handler处理 
     *  如上面proxybuildcollection.getclass().getname()就没有调用handler的getclass方法,而是调用自己的 
     *   
     * 3,在handler的invoke方法中return method.invoke(target,args)就是将方法交给target去完成。那么在这个方法执行之前,之后,异常时我们都可以做一些操作, 
     *  并且可以在执行之前检查方法的参数args,执行之后检查方法的结果 
     */ 
    system.out.println("-------------------下面的写法更简便--------------------"); 
     
    // proxybuildcoll是对arraylist进行代理 
    collection proxybuildcollection2 = (collection) proxy.newproxyinstance( 
        collection.class.getclassloader(),// 指定类加载器 
        new class[] { collection.class },// 指定目标对象实现的接口 
        // 指定handler 
        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(); 
  } 
  /** 
   * 构造一个目标对象的代理对象 
   * 
   * @param target 
   *      目标对象(需要实现某个接口) 
   * @return 
   */ 
  public static object buildproxy(final object target,final adviceinter advice) { 
    object proxyobject = proxy.newproxyinstance( 
        target.getclass().getclassloader(),// 指定类加载器 
        target.getclass().getinterfaces(), // 指定目标对象实现的接口 
        // handler 
        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; 
  } 
   
} 
 
/** 
 * 代理中执行目标方法之前之后的操作的一个实例 
 * 
 * @author 张明学 
 * 
 */ 
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); 
    } 
  } 
} 
/** 
 * 代理中执行目标方法之前之后的操作 
 * 
 * @author 张明学 
 * 
 */ 
public interface adviceinter { 
  /** 
   * 目标方法执行之前 
   * 
   */ 
  public void beforemethod(object target, method method, object[] args); 
 
  /** 
   * 目标方法执行之后 
   * 
   * @param target 
   *      目标对象 
   * @param method 
   *      方法 
   * @param args 
   *      参数 
   */ 
  public void aftermethod(object target, method method, object[] args); 
} 


 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网