1 java反射機制:運行時的類信息
Class類與java.lang.reflect類庫一起對反射概念進行了支持,該類庫包含了Field,Method以及Constructor類。這些類型的對象是由JVM在運行時創建的,用以表示未知類裏對應的成員。這樣就可以使用Constructor創建新的對象,用get()與set()方法讀取與修改與Field對象相關的字段,用invoke()方法調用與Method對象相關的方法等等。
2 動態代理:
代理模式:爲了提供額外的或不同的操作,而插入的用來代替“實際”對象。這些操作通常涉及與“實際”對象的通訊,因此代理通常充當着中間人的角色。
動態代理:所有的調用都會被重定向到單一的調用處理器上,他的工作是揭示調用的類型並確定相應的對策。
java反射機制實現動態代理的源碼:
- interface Interface {
- void doSomething();
- void somethingElse(String arg);
- }
- class RealObject implements Interface {
- public void doSomething() { print("doSomething"); }
- public void somethingElse(String arg) {
- print("somethingElse " + arg);
- }
- }
- import java.lang.reflect.*;
- class DynamicProxyHandler implements InvocationHandler {
- private Object proxied;
- public DynamicProxyHandler(Object proxied) {
- this.proxied = proxied;
- }
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- System.out.println("**** proxy: " + proxy.getClass() + ", method: "
- + method + ", args: " + args);
- if (args != null)
- for (Object arg : args)
- System.out.println(" " + arg);
- return method.invoke(proxied, args);
- }
- }
- public class SimpleDynamicProxy {
- public static void consumer(Interface iface) {
- iface.doSomething();
- iface.somethingElse("bonobo");
- }
- public static void main(String[] args) {
- RealObject real = new RealObject();
- consumer(real);
- // Insert a proxy and call again:
- Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class
- .getClassLoader(), new Class[] { Interface.class },
- new DynamicProxyHandler(real));
- consumer(proxy);
- }
- }
當我們查看java.lang.reflect.Proxy源碼,我們發現起核心作用的是ProxyGenerator.generateProxyClass(String paramString, Class[] paramArrayOfClass),最令人疑惑的問題是,代理對象和如何調用DynamicProxyHandler的invoke方法的,從源碼裏面我們很難發現如何處理的,對於ProxyGenerator寫了一個測試類ProxyClassFile
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import sun.misc.ProxyGenerator;
- public class ProxyClassFile {
- public static void main(String[] args) {
- String proxyName = "SimpleDynamicProxy";
- RealObject t = new RealObject();
- Class[] interfaces = t.getClass().getInterfaces();
- byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
- proxyName, interfaces);
- File f = new File("E:/workspace/TIJ4/bin/typeinfo/SimpleDynamicProxy.class");
- try {
- FileOutputStream fos = new FileOutputStream(f);
- fos.write(proxyClassFile);
- fos.flush();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace(); // To change body of catch statement use File |
- // Settings | File Templates.
- } catch (IOException e) {
- e.printStackTrace(); // To change body of catch statement use File |
- // Settings | File Templates.
- }
- }
- }
反編譯SimpleDynamicProxy.class,代碼初看起來有點複雜,仔細觀察還是很有規律的,將SimpleDynamicProxy5個方法都重定向到invoke()方法,equals(),hashCode()和toString()都是父類Object方法,doSomething()和somethingElse()爲接口方法。
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.lang.reflect.UndeclaredThrowableException;
- import typeinfo.Interface;
- public final class SimpleDynamicProxy extends Proxy
- implements Interface
- {
- private static Method m1;
- private static Method m0;
- private static Method m3;
- private static Method m4;
- private static Method m2;
- public SimpleDynamicProxy(InvocationHandler paramInvocationHandler)
- throws
- {
- super(paramInvocationHandler);
- }
- public final boolean equals(Object paramObject)
- throws
- {
- try
- {
- return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
- }
- catch (RuntimeException localRuntimeException)
- {
- throw localRuntimeException;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final int hashCode()
- throws
- {
- try
- {
- return ((Integer)this.h.invoke(this, m0, null)).intValue();
- }
- catch (RuntimeException localRuntimeException)
- {
- throw localRuntimeException;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final void doSomething()
- throws
- {
- try
- {
- this.h.invoke(this, m3, null);
- return;
- }
- catch (RuntimeException localRuntimeException)
- {
- throw localRuntimeException;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final void somethingElse(String paramString)
- throws
- {
- try
- {
- this.h.invoke(this, m4, new Object[] { paramString });
- return;
- }
- catch (RuntimeException localRuntimeException)
- {
- throw localRuntimeException;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final String toString()
- throws
- {
- try
- {
- return (String)this.h.invoke(this, m2, null);
- }
- catch (RuntimeException localRuntimeException)
- {
- throw localRuntimeException;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- static
- {
- try
- {
- m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
- m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
- m3 = Class.forName("typeinfo.Interface").getMethod("doSomething", new Class[0]);
- m4 = Class.forName("typeinfo.Interface").getMethod("somethingElse", new Class[] { Class.forName("java.lang.String") });
- m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
- return;
- }
- catch (NoSuchMethodException localNoSuchMethodException)
- {
- throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
- }
- catch (ClassNotFoundException localClassNotFoundException)
- {
- throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
- }
- }
- }
通過源代碼我們不難看出,代理類是如何調用invoke方法的了。