Chinaunix首页 | 论坛 | 博客
  • 博客访问: 147676
  • 博文数量: 27
  • 博客积分: 531
  • 博客等级: 一等列兵
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-25 18:31
文章分类

全部博文(27)

文章存档

2015年(4)

2014年(3)

2013年(6)

2012年(14)

我的朋友

分类: Java

2013-06-01 15:00:29

    一直听说java反射,也使用过一些基本的反射机制。其实就是基本的根据类名,从系统的classloader中加载出类来使用。
    这几天看到一段代码,使用了proxy,当时没有看太懂,所以借今天又周末,来找一篇blog看看,到底这proxy是怎么回事儿。

    首先这里涉及到三个类
    A (接口)
    B (A的实现)
    C (InvocationHandler类的实现)
    
    使用java的 Proxy 类,调用它的方法:
    public static Object newProxyInstance(ClassLoader loader,
                      Class[] interfaces,
                      InvocationHandler h)

    classloader是 B 的classloader
    interface 是 B 实现的 interface
    hander 是 C

    现在得说说 newProxyInstance 干了点什么
    其实,它是构造了一个 代理类,这个代理类实现了 interface A 并继承自 InvocationHander
    当调用 newProxyInstance 返回的实例的对象的方法的时候,被这个 代理类实现的interface方法转到 hander 的 invoke 方法
    这样就可以在 invoke 方法中做一下准备和扫尾的工作,并可以相应的参数调用真正的方法

    例子如下,这个例子是抄别人的 :)

1. interface A

点击(此处)折叠或打开

  1. public interface BusinessProcessor {
  2.     public void processBusiness();
  3. }
2. 实现类 B

点击(此处)折叠或打开

  1. public class BusinessProcessorImpl implements BusinessProcessor {
  2.     public void processBusiness() {
  3.         System.out.println("processing business.....");
  4.     }
  5. }
3. hander类 C

点击(此处)折叠或打开

  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;

  3. public class BusinessProcessorHandler implements InvocationHandler {

  4.     private Object target = null;

  5.     BusinessProcessorHandler(Object target) {
  6.         this.target = target;
  7.     }

  8.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  9.         System.out.println("You can do something here before process your business");
  10.         Object result = method.invoke(target, args);
  11.         System.out.println("You can do something here after process your business");
  12.         return result;
  13.     }
  14. }
4. test,该实现打印出了Proxy动态实现的类 $Proxy0

点击(此处)折叠或打开

  1. public class Test {
  2.     public static void main(String[] args) {
  3.         BusinessProcessorImpl bpimpl = new BusinessProcessorImpl();
  4.         BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl);
  5.         BusinessProcessor bp = (BusinessProcessor) Proxy.newProxyInstance(bpimpl.getClass().getClassLoader(), bpimpl
  6.                 .getClass().getInterfaces(), handler);

  7.         System.out.println("-------------- start -------------");
  8.         bp.processBusiness();

  9.         System.out.println(bp.getClass().getName());
  10.         Class<?> clz = bp.getClass();
  11.         printClassDefinition(clz);
  12.     }

  13.     public static String getModifier(int modifier) {
  14.         String result = "";
  15.         switch (modifier) {
  16.         case Modifier.PRIVATE:
  17.             result = "private";
  18.         case Modifier.PUBLIC:
  19.             result = "public";
  20.         case Modifier.PROTECTED:
  21.             result = "protected";
  22.         case Modifier.ABSTRACT:
  23.             result = "abstract";
  24.         case Modifier.FINAL:
  25.             result = "final";
  26.         case Modifier.NATIVE:
  27.             result = "native";
  28.         case Modifier.STATIC:
  29.             result = "static";
  30.         case Modifier.SYNCHRONIZED:
  31.             result = "synchronized";
  32.         case Modifier.STRICT:
  33.             result = "strict";
  34.         case Modifier.TRANSIENT:
  35.             result = "transient";
  36.         case Modifier.VOLATILE:
  37.             result = "volatile";
  38.         case Modifier.INTERFACE:
  39.             result = "interface";
  40.         }
  41.         return result;
  42.     }

  43.     public static void printClassDefinition(Class<?> clz) {

  44.         String clzModifier = getModifier(clz.getModifiers());
  45.         if (clzModifier != null && !clzModifier.equals("")) {
  46.             clzModifier = clzModifier + " ";
  47.         }
  48.         String superClz = clz.getSuperclass().getName();
  49.         if (superClz != null && !superClz.equals("")) {
  50.             superClz = "extends " + superClz;
  51.         }

  52.         Class<?>[] interfaces = clz.getInterfaces();

  53.         String inters = "";
  54.         for (int i = 0; i < interfaces.length; i++) {
  55.             if (i == 0) {
  56.                 inters += "implements ";
  57.             }
  58.             inters += interfaces[i].getName();
  59.         }

  60.         System.out.println(clzModifier + clz.getName() + " " + superClz + " " + inters);
  61.         System.out.println("{");

  62.         Field[] fields = clz.getDeclaredFields();
  63.         for (int i = 0; i < fields.length; i++) {
  64.             String modifier = getModifier(fields[i].getModifiers());
  65.             if (modifier != null && !modifier.equals("")) {
  66.                 modifier = modifier + " ";
  67.             }
  68.             String fieldName = fields[i].getName();
  69.             String fieldType = fields[i].getType().getName();
  70.             System.out.println(" " + modifier + fieldType + " " + fieldName + ";");
  71.         }

  72.         System.out.println();

  73.         Method[] methods = clz.getDeclaredMethods();
  74.         for (int i = 0; i < methods.length; i++) {
  75.             Method method = methods[i];

  76.             String modifier = getModifier(method.getModifiers());
  77.             if (modifier != null && !modifier.equals("")) {
  78.                 modifier = modifier + " ";
  79.             }

  80.             String methodName = method.getName();

  81.             Class<?> returnClz = method.getReturnType();
  82.             String retrunType = returnClz.getName();

  83.             Class<?>[] clzs = method.getParameterTypes();
  84.             String paraList = "(";
  85.             for (int j = 0; j < clzs.length; j++) {
  86.                 paraList += clzs[j].getName();
  87.                 if (j != clzs.length - 1) {
  88.                     paraList += ", ";
  89.                 }
  90.             }
  91.             paraList += ")";

  92.             clzs = method.getExceptionTypes();
  93.             String exceptions = "";
  94.             for (int j = 0; j < clzs.length; j++) {
  95.                 if (j == 0) {
  96.                     exceptions += "throws ";
  97.                 }

  98.                 exceptions += clzs[j].getName();

  99.                 if (j != clzs.length - 1) {
  100.                     exceptions += ", ";
  101.                 }
  102.             }

  103.             exceptions += ";";

  104.             String methodPrototype = modifier + retrunType + " " + methodName + paraList + exceptions;

  105.             System.out.println(" " + methodPrototype);

  106.         }
  107.         System.out.println("}");
  108.     }
  109. }
输出如下:
-------------- start -------------
You can do something here before process your business
processing business.....
You can do something here after process your business
$Proxy0
$Proxy0 extends java.lang.reflect.Proxy implements proxy.BusinessProcessor
{
    java.lang.reflect.Method m2;
    java.lang.reflect.Method m0;
    java.lang.reflect.Method m3;
    java.lang.reflect.Method m1;

    int hashCode();
    boolean equals(java.lang.Object);
    java.lang.String toString();
    void processBusiness();
}

阅读(2024) | 评论(0) | 转发(0) |
0

上一篇:java技术的区别

下一篇:可怕的C++

给主人留下些什么吧!~~