JDK动态代理

  • 一个动态代理的实例
package com;

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

/** * @author doggy * Created on 16-7-3. */
public class Test {
    public static void main(String[] args){
        AI ai = new AI();
        AP ap = new AP();
        A a = (A)ap.bind(ai);
        System.out.println(a.getClass());
        a.p();

    }
}
interface A{
    void p();
}
class AI implements A{
    @Override
    public void p() {
        System.out.println("I am AI.");
    }
}
class AP implements InvocationHandler{
    private Object target;
    public Object bind(Object o){
        this.target = o;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("proxyed");
        return method.invoke(target,args);
    }
}

JDK的动态代理做了什么?

  1. Proxy.newProxyInstance()会先根据传进去的接口和类加载器,创建一个代理类,通过native方法,无法具体查看.
  2. 根据InvocationHandler实例创建一个代理类的实例.
  3. 代理方法调用时,内部通过反射生成Method对象,并通过调用InvocationHandler实例的invoke方法,在invoke内部进行具体调用.

源码:

public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {
        Objects.requireNonNull(h);

        final Class<?>[] intfs = interfaces.clone();
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
        }

        /* * Look up or generate the designated proxy class. */
        Class<?> cl = getProxyClass0(loader, intfs);

        /* * Invoke its constructor with the designated invocation handler. */
        try {
            if (sm != null) {
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
            }

            final Constructor<?> cons = cl.getConstructor(constructorParams);
            final InvocationHandler ih = h;
            if (!Modifier.isPublic(cl.getModifiers())) {
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    public Void run() {
                        cons.setAccessible(true);
                        return null;
                    }
                });
            }
            return cons.newInstance(new Object[]{h});
        } catch (IllegalAccessException|InstantiationException e) {
            throw new InternalError(e.toString(), e);
        } catch (InvocationTargetException e) {
            Throwable t = e.getCause();
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else {
                throw new InternalError(t.toString(), t);
            }
        } catch (NoSuchMethodException e) {
            throw new InternalError(e.toString(), e);
        }
    }

你可能感兴趣的:(动态代理,jdk)