http://blog.csdn.net/jiankunking/article/details/52143504
版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
一、什么是***?
***是一种常用的设计模式,其目的就是为其他对象提供一个***以控制对某个对象的访问。***类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理。
***模式UML图:
简单结构示意图:
为了保持行为的一致性,***类和委托类通常会实现相同的接口,所以在访问者看来两者没有丝毫的区别。通过***类这中间一层,能有效控制对委托类对象的直接访问,也可以很好地隐藏和保护委托类对象,同时也为实施不同控制策略预留了空间,从而在设计上获得了更大的灵活性。Java 动态***机制以巧妙的方式近乎完美地实践了***模式的设计理念。
二、JDK的动态***怎么使用?
1、需要动态***的接口:
- package jiankunking;
- /**
- * 需要动态***的接口
- */
- public interface Subject
- {
- /**
- * 你好
- *
- * @param name
- * @return
- */
- public String SayHello(String name);
- /**
- * 再见
- *
- * @return
- */
- public String SayGoodBye();
- }
2、需要***的实际对象
- package jiankunking;
- /**
- * 实际对象
- */
- public class RealSubject implements Subject
- {
- /**
- * 你好
- *
- * @param name
- * @return
- */
- public String SayHello(String name)
- {
- return "hello " + name;
- }
- /**
- * 再见
- *
- * @return
- */
- public String SayGoodBye()
- {
- return " good bye ";
- }
- }
3、调用处理器实现类(有木有感觉这里就是传说中的AOP啊)
- package jiankunking;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- /**
- * 调用处理器实现类
- * 每次生成动态***类对象时都需要指定一个实现了该接口的调用处理器对象
- */
- public class InvocationHandlerImpl implements InvocationHandler
- {
- /**
- * 这个就是我们要***的真实对象
- */
- private Object subject;
- /**
- * 构造方法,给我们要***的真实对象赋初值
- *
- * @param subject
- */
- public InvocationHandlerImpl(Object subject)
- {
- this.subject = subject;
- }
- /**
- * 该方法负责集中处理动态***类上的所有方法调用。
- * 调用处理器根据这三个参数进行预处理或分派到委托类实例上反射执行
- *
- * @param proxy ***类实例
- * @param method 被调用的方法对象
- * @param args 调用参数
- * @return
- * @throws Throwable
- */
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
- {
- //在***真实对象前我们可以添加一些自己的操作
- System.out.println("在调用之前,我要干点啥呢?");
- System.out.println("Method:" + method);
- //当***对象调用真实对象的方法时,其会自动的跳转到***对象关联的handler对象的invoke方法来进行调用
- Object returnValue = method.invoke(subject, args);
- //在***真实对象后我们也可以添加一些自己的操作
- System.out.println("在调用之后,我要干点啥呢?");
- return returnValue;
- }
- }
- package jiankunking;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Proxy;
- /**
- * 动态***演示
- */
- public class DynamicProxyDemonstration
- {
- public static void main(String[] args)
- {
- //***的真实对象
- Subject realSubject = new RealSubject();
- /**
- * InvocationHandlerImpl 实现了 InvocationHandler 接口,并能实现方法调用从***类到委托类的分派转发
- * 其内部通常包含指向委托类实例的引用,用于真正执行分派转发过来的方法调用.
- * 即:要***哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法
- */
- InvocationHandler handler = new InvocationHandlerImpl(realSubject);
- ClassLoader loader = handler.getClass().getClassLoader();
- Class[] interfaces = realSubject.getClass().getInterfaces();
- /**
- * 该方法用于为指定类装载器、一组接口及调用处理器生成动态***类实例
- */
- Subject subject = (Subject) Proxy.newProxyInstance(loader, interfaces, handler);
- System.out.println("动态***对象的类型:"+subject.getClass().getName());
- String hello = subject.SayHello("jiankunking");
- System.out.println(hello);
- // String goodbye = subject.SayGoodBye();
- // System.out.println(goodbye);
- }
- }
5、输出结果如下:
演示demo下载地址:http://download.csdn.net/detail/xunzaosiyecao/9597388
三、动态***怎么实现的?
从使用代码中可以看出,关键点在:
- Subject subject = (Subject) Proxy.newProxyInstance(loader, interfaces, handler);
也就是说,当代码执行到:
subject.SayHello("jiankunking")这句话时,会自动调用InvocationHandlerImpl的invoke方法。这是为啥呢?
======================横线之间的是代码跟分析的过程,不想看的朋友可以直接看结论=====================================
以下代码来自:JDK1.8.0_92
既然生成***对象是用的Proxy类的静态方newProxyInstance,那么我们就去它的源码里看一下它到底都做了些什么?
- public static Object newProxyInstance(ClassLoader loader,
- Class<?>[] interfaces,
- InvocationHandler h)
- throws IllegalArgumentException
- {
- //检查h 不为空,否则抛异常
- Objects.requireNonNull(h);
- final Class<?>[] intfs = interfaces.clone();
- final SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
- }
- /*
- * 获得与指定类装载器和一组接口相关的***类类型对象
- */
- Class<?> cl = getProxyClass0(loader, intfs);
- /*
- * 通过反射获取构造函数对象并生成***类实例
- */
- try {
- if (sm != null) {
- checkNewProxyPermission(Reflection.getCallerClass(), cl);
- }
- //获取***对象的构造方法(也就是$Proxy0(InvocationHandler h))
- 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;
- }
- });
- }
- //生成***类的实例并把InvocationHandlerImpl的实例传给它的构造方法
- 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);
- }
- }
我们再进去getProxyClass0方法看一下:
- /**
- * Generate a proxy class. Must call the checkProxyAccess method
- * to perform permission checks before calling this.
- */
- private static Class<?> getProxyClass0(ClassLoader loader,
- Class<?>... interfaces) {
- if (interfaces.length > 65535) {
- throw new IllegalArgumentException("interface limit exceeded");
- }
- // If the proxy class defined by the given loader implementing
- // the given interfaces exists, this will simply return the cached copy;
- // otherwise, it will create the proxy class via the ProxyClassFactory
- return proxyClassCache.get(loader, interfaces);
- }
- /**
- * a cache of proxy classes
- */
- private static final WeakCache<ClassLoader, Class<?>[], Class<?>>
- proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
奥,原来用了一下缓存啊
那么它对应的get方法啥样呢? - /**
- * Look-up the value through the cache. This always evaluates the
- * {@code subKeyFactory} function and optionally evaluates
- * {@code valueFactory} function if there is no entry in the cache for given
- * pair of (key, subKey)&nbs***bsp;the entry has already been cleared.
- *
- * @param key possibly null key
- * @param parameter parameter used together with key to create sub-key and
- * value (should not be null)
- * @return the cached value (never null)
- * @throws NullPointerException if {@code parameter} passed in or
- * {@code sub-key} calculated by
- * {@code subKeyFactory}&nbs***bsp;{@code value}
- * calculated by {@code valueFactory} is null.
- */
- public V get(K key, P parameter) {
- Objects.requireNonNull(parameter);
- expungeStaleEntries();
- Object cacheKey = CacheKey.valueOf(key, refQueue);
- // lazily install the 2nd level valuesMap for the particular cacheKey
- ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);
- if (valuesMap == null) {
- //putIfAbsent这个方法在key不存在的时候加入一个值,如果key存在就不放入
- ConcurrentMap<Object, Supplier<V>> oldValuesMap
- = map.putIfAbsent(cacheKey,
- valuesMap = new ConcurrentHashMap<>());
- if (oldValuesMap != null) {
- valuesMap = oldValuesMap;
- }
- }
- // create subKey and retrieve the possible Supplier<V> stored by that
- // subKey from valuesMap
- Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
- Supplier<V> supplier = valuesMap.get(subKey);
- Factory factory = null;
- while (true) {
- if (supplier != null) {
- // supplier might be a Factory&nbs***bsp;a CacheValue<V> instance
- V value = supplier.get();
- if (value != null) {
- return value;
- }
- }
- // else no supplier in cache
- //&nbs***bsp;a supplier that returned null (could be a cleared CacheValue
- //&nbs***bsp;a Factory that wasn't successful in installing the CacheValue)
- // lazily construct a Factory
- if (factory == null) {
- factory = new Factory(key, parameter, subKey, valuesMap);
- }
- if (supplier == null) {
- supplier = valuesMap.putIfAbsent(subKey, factory);
- if (supplier == null) {
- // successfully installed Factory
- supplier = factory;
- }
- // else retry with winning supplier
- } else {
- if (valuesMap.replace(subKey, supplier, factory)) {
- // successfully replaced
- // cleared CacheEntry / unsuccessful Factory
- // with our Factory
- supplier = factory;
- } else {
- // retry with current supplier
- supplier = valuesMap.get(subKey);
- }
- }
- }
- }
我们可以看到它调用了 supplier.get(); 获取动态***类,其中supplier是Factory,这个类定义在WeakCach的内部。
来瞅瞅,get里面又做了什么?
- public synchronized V get() { // serialize access
- // re-check
- Supplier<V> supplier = valuesMap.get(subKey);
- if (supplier != this) {
- // something changed while we were waiting:
- // might be that we were replaced by a CacheValue
- //&nbs***bsp;were removed because of failure ->
- // return null to signal WeakCache.get() to retry
- // the loop
- return null;
- }
- // else still us (supplier == this)
- // create new value
- V value = null;
- try {
- value = Objects.requireNonNull(valueFactory.apply(key, parameter));
- } finally {
- if (value == null) { // remove us on failure
- valuesMap.remove(subKey, this);
- }
- }
- // the only path to reach here is with non-null value
- assert value != null;
- // wrap value with CacheValue (WeakReference)
- CacheValue<V> cacheValue = new CacheValue<>(value);
- // try replacing us with CacheValue (this should always succeed)
- if (valuesMap.replace(subKey, this, cacheValue)) {
- // put also in reverseMap
- reverseMap.put(cacheValue, Boolean.TRUE);
- } else {
- throw new AssertionError("Should not reach here");
- }
- // successfully replaced us with new CacheValue -> return the value
- // wrapped by it
- return value;
- }
- }
发现重点还是木有出现,但我们可 以看到它调用了valueFactory.apply(key, parameter)方法:
- /**
- * A factory function that generates, defines and returns the proxy class given
- * the ClassLoader and array of interfaces.
- */
- private static final class ProxyClassFactory
- implements BiFunction<ClassLoader, Class<?>[], Class<?>>
- {
- // prefix for all proxy class names
- private static final String proxyClassNamePrefix = "$Proxy";
- // next number to use for generation of unique proxy class names
- private static final AtomicLong nextUniqueNumber = new AtomicLong();
- @Override
- public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
- Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
- for (Class<?> intf : interfaces) {
- /*
- * Verify that the class loader resolves the name of this
- * interface to the same Class object.
- */
- Class<?> interfaceClass = null;
- try {
- interfaceClass = Class.forName(intf.getName(), false, loader);
- } catch (ClassNotFoundException e) {
- }
- if (interfaceClass != intf) {
- throw new IllegalArgumentException(
- intf + " is not visible from class loader");
- }
- /*
- * Verify that the Class object actually represents an
- * interface.
- */
- if (!interfaceClass.isInterface()) {
- throw new IllegalArgumentException(
- interfaceClass.getName() + " is not an interface");
- }
- /*
- * Verify that this interface is not a duplicate.
- */
- if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
- throw new IllegalArgumentException(
- "repeated interface: " + interfaceClass.getName());
- }
- }
- String proxyPkg = null; // package to define proxy class in
- int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
- /*
- * Record the package of a non-public proxy interface so that the
- * proxy class will be defined in the same package. Verify that
- * all non-public proxy interfaces are in the same package.
- */
- for (Class<?> intf : interfaces) {
- int flags = intf.getModifiers();
- if (!Modifier.isPublic(flags)) {
- accessFlags = Modifier.FINAL;
- String name = intf.getName();
- int n = name.lastIndexOf('.');
- String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
- if (proxyPkg == null) {
- proxyPkg = pkg;
- } else if (!pkg.equals(proxyPkg)) {
- throw new IllegalArgumentException(
- "non-public interfaces from different packages");
- }
- }
- }
- if (proxyPkg == null) {
- // if no non-public proxy interfaces, use com.sun.proxy package
- proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
- }
- /*
- * Choose a name for the proxy class to generate.
- */
- long num = nextUniqueNumber.getAndIncrement();
- String proxyName = proxyPkg + proxyClassNamePrefix + num;
- /*
- * Generate the specified proxy class.
- */
- byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
- proxyName, interfaces, accessFlags);
- try {
- return defineClass0(loader, proxyName,
- proxyClassFile, 0, proxyClassFile.length);
- } catch (ClassFormatError e) {
- /*
- * A ClassFormatError here means that (barring bugs in the
- * proxy class generation code) there was some other
- * invalid aspect of the arguments supplied to the proxy
- * class creation (such as virtual machine limitations
- * exceeded).
- */
- throw new IllegalArgumentException(e.toString());
- }
- }
- }
- //生成字节码
- byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces, accessFlags);
- package jiankunking;
- import sun.misc.ProxyGenerator;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Proxy;
- /**
- * 动态***演示
- */
- public class DynamicProxyDemonstration
- {
- public static void main(String[] args)
- {
- //***的真实对象
- Subject realSubject = new RealSubject();
- /**
- * InvocationHandlerImpl 实现了 InvocationHandler 接口,并能实现方法调用从***类到委托类的分派转发
- * 其内部通常包含指向委托类实例的引用,用于真正执行分派转发过来的方法调用.
- * 即:要***哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法
- */
- InvocationHandler handler = new InvocationHandlerImpl(realSubject);
- ClassLoader loader = handler.getClass().getClassLoader();
- Class[] interfaces = realSubject.getClass().getInterfaces();
- /**
- * 该方法用于为指定类装载器、一组接口及调用处理器生成动态***类实例
- */
- Subject subject = (Subject) Proxy.newProxyInstance(loader, interfaces, handler);
- System.out.println("动态***对象的类型:"+subject.getClass().getName());
- String hello = subject.SayHello("jiankunking");
- System.out.println(hello);
- // 将生成的字节码保存到本地,
- createProxyClassFile();
- }
- private static void createProxyClassFile(){
- String name = "ProxySubject";
- byte[] data = ProxyGenerator.generateProxyClass(name,new Class[]{Subject.class});
- FileOutputStream out =null;
- try {
- out = new FileOutputStream(name+".class");
- System.out.println((new File("hello")).getAbsolutePath());
- out.write(data);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(null!=out) try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.lang.reflect.UndeclaredThrowableException;
- import jiankunking.Subject;
- public final class ProxySubject
- extends Proxy
- implements Subject
- {
- private static Method m1;
- private static Method m3;
- private static Method m4;
- private static Method m2;
- private static Method m0;
- public ProxySubject(InvocationHandler paramInvocationHandler)
- {
- super(paramInvocationHandler);
- }
- public final boolean equals(Object paramObject)
- {
- try
- {
- return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
- }
- catch (Error|RuntimeException localError)
- {
- throw localError;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final String SayGoodBye()
- {
- try
- {
- return (String)this.h.invoke(this, m3, null);
- }
- catch (Error|RuntimeException localError)
- {
- throw localError;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final String SayHello(String paramString)
- {
- try
- {
- return (String)this.h.invoke(this, m4, new Object[] { paramString });
- }
- catch (Error|RuntimeException localError)
- {
- throw localError;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final String toString()
- {
- try
- {
- return (String)this.h.invoke(this, m2, null);
- }
- catch (Error|RuntimeException localError)
- {
- throw localError;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final int hashCode()
- {
- try
- {
- return ((Integer)this.h.invoke(this, m0, null)).intValue();
- }
- catch (Error|RuntimeException localError)
- {
- throw localError;
- }
- catch (Throwable localThrowable)
- {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- static
- {
- try
- {
- m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
- m3 = Class.forName("jiankunking.Subject").getMethod("SayGoodBye", new Class[0]);
- m4 = Class.forName("jiankunking.Subject").getMethod("SayHello", new Class[] { Class.forName("java.lang.String") });
- m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
- m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
- return;
- }
- catch (NoSuchMethodException localNoSuchMethodException)
- {
- throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
- }
- catch (ClassNotFoundException localClassNotFoundException)
- {
- throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
- }
- }
- }
这就是最终真正的***类,它继承自Proxy并实现了我们定义的Subject接口
也就是说 :
- Subject subject = (Subject) Proxy.newProxyInstance(loader, interfaces, handler);
这里的 subject 实际是这个类的一个实例,那么我们调用它的:
- public final String SayHello(String paramString)
就是调用我们定义的InvocationHandlerImpl的 invoke方法:
======================横线之间的是代码跟分析的过程,不想看的朋友可以直接看结论=====================================
四、结论
到了这里,终于解答了:
subject.SayHello("jiankunking")这句话时,为什么会自动调用InvocationHandlerImpl的invoke方法?
因为JDK生成的最终真正的***类,它继承自Proxy并实现了我们定义的Subject接口,在实现Subject接口方法的内部,通过反射调用了InvocationHandlerImpl的invoke方法。
包含生成本地class文件的demo:http://download.csdn.net/detail/xunzaosiyecao/9597474
通过分析代码可以看出Java 动态***,具体有如下四步骤:
- 通过实现 InvocationHandler 接口创建自己的调用处理器;
- 通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建动态***类;
- 通过反射机制获得动态***类的构造函数,其唯一参数类型是调用处理器接口类型;
- 通过构造函数创建动态***类实例,构造时调用处理器对象作为参数被传入。
作者:jiankunking 出处:http://blog.csdn.net/jiankunking