Loading

深入理解Java Proxy

深入理解Java Proxy:

http://blog.csdn.net/rokii/article/details/4046098

整理之后的代码:

package com.stono.reftest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
interface BusinessProcessor {
    void processBusiness();
    void processBusiness2(); // 这样$Proxy0会有 Method m4属性;
}
class BusinessProcessorImpl implements BusinessProcessor {
    @Override
    public void processBusiness() {
        System.out.println("processing business...");
    }
    @Override
    public void processBusiness2() {
        System.out.println("processing business 2...");
    }
}
class BusinessProcessorHandler implements InvocationHandler {
    private Object target = null;
    public BusinessProcessorHandler(Object target) {
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("You can do something before your business");
        Object result = method.invoke(target, args);
        System.out.println("You can do something after your business");
        return result;
    }
}
public class DynProxy {
    public static void main(String[] args) {
        BusinessProcessorImpl bpimpl = new BusinessProcessorImpl();
        BusinessProcessorHandler handler = new BusinessProcessorHandler(bpimpl);
        BusinessProcessor bp = (BusinessProcessor) Proxy.newProxyInstance(bpimpl.getClass().getClassLoader(), bpimpl.getClass().getInterfaces(), handler);
        bp.processBusiness();
        bp.processBusiness2();
        System.out.println(bp.getClass().getName());
        Class clz = bp.getClass();
        printClassDefinition(clz);
    }
    public static String getModifier(int modifier) {
        String result = "";
        switch (modifier) {
        case Modifier.PRIVATE:
            result = "private";
        case Modifier.PUBLIC:
            result = "public";
        case Modifier.PROTECTED:
            result = "protected";
        case Modifier.ABSTRACT:
            result = "abstract";
        case Modifier.FINAL:
            result = "final";
        case Modifier.NATIVE:
            result = "native";
        case Modifier.STATIC:
            result = "static";
        case Modifier.SYNCHRONIZED:
            result = "synchronized";
        case Modifier.STRICT:
            result = "strict";
        case Modifier.TRANSIENT:
            result = "transient";
        case Modifier.VOLATILE:
            result = "volatile";
        case Modifier.INTERFACE:
            result = "interface";
        }
        return result;
    }
    public static void printClassDefinition(Class clz) {
        String clzModifier = getModifier(clz.getModifiers());
        if (clzModifier != null && !clzModifier.equals("")) {
            clzModifier = clzModifier + " ";
        }
        String superClz = clz.getSuperclass().getName();
        if (superClz != null && !superClz.equals("")) {
            superClz = "extends " + superClz;
        }
        Class[] interfaces = clz.getInterfaces();
        String inters = "";
        for (int i = 0; i < interfaces.length; i++) {
            if (i == 0) {
                inters += "implements ";
            }
            inters += interfaces[i].getName();
        }
        System.out.println(clzModifier + clz.getName() + " " + superClz + " " + inters);
        System.out.println("{");
        Constructor<?>[] constructors = clz.getConstructors();// 长度为1
        for (int i = 0; i < constructors.length; i++) {
            Constructor<?> constructor = constructors[i];
            String modifier = getModifier(constructor.getModifiers());
            if (modifier != null && !modifier.equals("")) {
                modifier = modifier + " ";
            }
            String name = constructor.getName();
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            String params = "";
            for (int j = 0; j < parameterTypes.length; j++) {
                params += parameterTypes[i].getName() + ", ";
            }
            String substring = params.substring(0, params.length() - 2);
            System.out.println("    " + modifier + name + "(" + substring + ");");
        }
        System.out.println();
        Field[] fields = clz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            String modifier = getModifier(fields[i].getModifiers());
            if (modifier != null && !modifier.equals("")) {
                modifier = modifier + " ";
            }
            String fieldName = fields[i].getName();
            String fieldType = fields[i].getType().getName();
            System.out.println("    " + modifier + fieldType + " " + fieldName + ";");
        }
        System.out.println();
        Method[] methods = clz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            String modifier = getModifier(method.getModifiers());
            if (modifier != null && !modifier.equals("")) {
                modifier = modifier + " ";
            }
            String methodName = method.getName();
            Class returnClz = method.getReturnType();
            String retrunType = returnClz.getName();
            Class[] clzs = method.getParameterTypes();
            String paraList = "(";
            for (int j = 0; j < clzs.length; j++) {
                paraList += clzs[j].getName();
                if (j != clzs.length - 1) {
                    paraList += ", ";
                }
            }
            paraList += ")";
            clzs = method.getExceptionTypes();
            String exceptions = "";
            for (int j = 0; j < clzs.length; j++) {
                if (j == 0) {
                    exceptions += "throws ";
                }
                exceptions += clzs[j].getName();
                if (j != clzs.length - 1) {
                    exceptions += ", ";
                }
            }
            exceptions += ";";
            String methodPrototype = modifier + retrunType + " " + methodName + paraList + exceptions;
            System.out.println("    " + methodPrototype);
        }
        System.out.println("}");
    }
}

 

posted @ 2015-10-10 09:20  stono  阅读(243)  评论(0编辑  收藏  举报