Proxy的public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h),
首先通过getProxyClass 创建一个新的Class 对象,//(Class cl = getProxyClass(loader, interfaces);)
getProxyClass实现所有的包含所有的interfaces接口,并且名字为String proxyName = proxyPkg + proxyClassNamePrefix + num;
其中proxyClassNamePrefix为"$Proxy",proxyPkg 为包名,num并发控制数字
然后 Constructor cons = cl.getConstructor(constructorParams);,其中Class [] constructorParams ={ InvocationHandler.class }
return (Object) cons.newInstance(new Object[] { h });
可知:proxyName 的Class对象包含有InvocationHandler.class数组的构造器,
最后 return (Object) cons.newInstance(new Object[] { h }),即用实现InvocationHandler接口的对象作为参数实例化proxyName Class。
测试:建立一个方法打印出Class的内容:
//此类修改于http://blog.csdn.net/rokii/archive/2009/04/03/4046098.aspx,原来的方法不能正确的打印出类的内容
public static void printClassDefinition(Class clz) {
String clzModifier = Modifier.toString(clz.getModifiers())+" ";
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("{");
System.out.println("//Fields:");
Field[] fields = clz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String modifier = Modifier.toString(fields[i].getModifiers())+" ";
String fieldName = fields[i].getName();
String fieldType = fields[i].getType().getName();
System.out.println(" " + modifier + fieldType + " " + fieldName
+ ";");
}
System.out.println("//Constructs:");
Constructor[] constructs=clz.getConstructors();
for(int i=0;i<constructs.length;i++){
Constructor construct=constructs[i];
System.out.println(" "+construct.toString()+";");
}
System.out.println("//Methods:");
Method[] methods = clz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
String modifier = Modifier.toString(method.getModifiers())+" ";
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("}");
}
某个代理类返回对象用printClassDefinition打印如下:
public final $Proxy0 extends java.lang.reflect.Proxy implements BusinessProcessor
{
//Fields:
private static java.lang.reflect.Method m1;
private static java.lang.reflect.Method m0;
private static java.lang.reflect.Method m3;
private static java.lang.reflect.Method m4;
private static java.lang.reflect.Method m2;
//Constructs:
public $Proxy0(java.lang.reflect.InvocationHandler);
//Methods:
public final void processBusiness(int);
public final void testMethod();
public final int hashCode();
public final boolean equals(java.lang.Object);
public final java.lang.String toString();
}
可知,$Proxy0的运作原理如下:
用实现java.lang.reflect.InvocationHandler接口的对象初始化。
Fields指向的是实现java.lang.reflect.InvocationHandler接口的对象的调用方法,
Fields和Methods是一一对应的
当$Proxy0对象调用方法时,比如调用public final void processBusiness(int);
在processBusiness的内部,会把int参数传递给某个对应的Field,
然后通过Field,调用实现java.lang.reflect.InvocationHandler接口对象的方法
java动态代理机制原理
最新推荐文章于 2021-05-19 14:05:25 发布