您的位置:首页 > 编程语言 > Java开发

Java 动态代理深入解析

2016-01-28 18:02 501 查看
package cn.quantgroup.tms.proxy;

/**
* Created by tums on 2016/1/28.
*/
public interface BusinessProcessor {//1 业务接口
String doSomething(String name);
}

package cn.quantgroup.tms.proxy;

/**
* Created by tums on 2016/1/28.
*/
public class BusinessProcessorImpl implements BusinessProcessor {//2 业务接口实现
@Override
public String doSomething(String name) {
return "hello," + name;
}
}
package cn.quantgroup.tms.proxy;

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

/**
* Created by tums on 2016/1/28.
*/
public class BusinessProcessorHandler implements InvocationHandler {//3 代理处理器

private Object target = null;

//构造注入实际业务处理实例
public BusinessProcessorHandler(Object target) {
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//method.getName() 可以看出当前代理调用的方法
System.out.println("You can do something here before process your business:" + method.getName());
Object result = method.invoke(target, args);//target 必须是实际处理业务的实例
System.out.println("You can do something here after process your business:" + method.getName());
return result;
}
}
package cn.quantgroup.tms.proxy;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;

/**
* Created by tums on 2016/1/27.
*/
public class DynamicProxy {//4 测试类
public static void main(String args[]) {
BusinessProcessor businessProcessorImpl = new BusinessProcessorImpl();//实际业务处理实例
System.out.println("businessProcessorImpl:" + businessProcessorImpl);
BusinessProcessor proxyInstance = (BusinessProcessor) Proxy.newProxyInstance(BusinessProcessor.class.getClassLoader(), new Class[]{BusinessProcessor.class}, new BusinessProcessorHandler(businessProcessorImpl));//创建代理对象
String result = proxyInstance.doSomething("zhangsan");//观察方法执行过程
System.out.println("result:" + result);
System.out.println("proxyInstance:" + proxyInstance);//发现和上面创建的业务实例是同一个

//5 打印代理对象是如何实现的
System.out.println(proxyInstance.getClass().getName());
Class clz = proxyInstance.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("{");

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("}");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: