Java靜態署理的運用詳解。本站提示廣大學習愛好者:(Java靜態署理的運用詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java靜態署理的運用詳解正文
靜態署理其實就是java.lang.reflect.Proxy類靜態的依據您指定的一切接口生成一個class byte,該class會繼續Proxy類,並完成一切你指定的接口(您在參數中傳入的接口數組);然後再應用您指定的classloader將 class byte加載進體系,最初生成如許一個類的對象,並初始化該對象的一些值,如invocationHandler,以即一切的接口對應的Method成員。 初始化以後將對象前往給挪用的客戶端。如許客戶端拿到的就是一個完成你一切的接口的Proxy對象。請看實例剖析:
package com.fans.common.proxy;
public interface BusinessProcessor {
public void processBusiness();
}
package com.fans.common.proxy;
/**
* 營業處置類
* @author fanshadoop
*
*/
public class BusinessProcessorImpl implements BusinessProcessor {
@Override
public void processBusiness() {
System.out.println("processing business.....");
}
}
package com.fans.common.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* 營業署理類
* @author fanshadoop
*
*/
public class BusinessProcessorHandler implements InvocationHandler {
private Object target = null;
BusinessProcessorHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out
.println("You can do something here before process your business");
Object result = method.invoke(target, args);
System.out
.println("You can do something here after process your business");
return result;
}
}
package com.fans.common.proxy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
public class Test {
/**
* @param args
*/
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();
System.out.println(bp.getClass().getName());
printClassDefinition(bp.getClass());
}
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("}");
}
}
運轉成果:
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 com.fans.common.proxy.BusinessProcessor
{
java.lang.reflect.Method m1;
java.lang.reflect.Method m3;
java.lang.reflect.Method m0;
java.lang.reflect.Method m2;
boolean equals(java.lang.Object);
java.lang.String toString();
int hashCode();
void processBusiness();
}
類BusinessProcessorHandler完成了InvocationHandler接口的invoke辦法,這個類就是Proxy終究挪用固定接口辦法。
很顯著,Proxy.newProxyInstance辦法會做以下幾件事:
1,依據傳入的第二個參數interfaces靜態生成一個類,完成interfaces中的接口,該例中即BusinessProcessor接口的processBusiness辦法。而且繼續了Proxy類,重寫了hashcode,toString,equals等三個辦法。詳細完成可參看 ProxyGenerator.generateProxyClass(...); 該例中生成了$Proxy0類
2,經由過程傳入的第一個參數classloder將剛生成的類加載到jvm中。行將$Proxy0類load
3,應用第三個參數,挪用$Proxy0的$Proxy0(InvocationHandler)結構函數 創立$Proxy0的對象,而且用interfaces參數遍歷其一切接口的辦法,並生成Method對象初始化對象的幾個Method成員變量
4,將$Proxy0的實例前往給客戶端。
如今好了。我們再看客戶端怎樣調就清晰了。
1,客戶端拿到的是$Proxy0的實例對象,因為$Proxy0繼續了BusinessProcessor,是以轉化為BusinessProcessor沒任何成績。
BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....);
2,bp.processBusiness();
現實上挪用的是$Proxy0.processBusiness();那末$Proxy0.processBusiness()的完成就是經由過程InvocationHandler去挪用invoke辦法啦!