jdk的动态代理及为什么需要接口[转载] jdk的动态代理及为什么需要接口

jdk的动态代理及为什么需要接口

https://blog.csdn.net/zxysshgood/article/details/78684229

动态代理有关,无非是使用JDK动态代理,和cglib动态代理。一直不待明白的是为什么,jdk的动态代理需要接口才能实现,这也是其短板和令人诟病的地方。很多的博文说的很复杂,代码一大堆,没有太明白。手打了一下,参考了一些优秀的博文,在这里给自己做个总结。

首先,动态代理是个挺有用的东西,常见的就是javaAOP的有关,主要的作用是是在一个方法使用前后,能进行别的处理。比如吧,aop所说的,面向切面编程,日志有关,检测有关。都可以通过AOP或者说动态代理来实现。

先来看下最简单的代码下实现动态代理的情况,然后看下源码和我们的主题,为什么需要接口

为什么需要接口,先上结论

1.在需要继承proxy类获得有关方法和InvocationHandler构造方法传参的同时,java不能同时继承两个类,我们需要和想要代理的类建立联系,只能实现一个接口

2.需要反射获得代理类的有关参数,必须要通过某个类,反射获取有关方法,如本次测试用的 :printSomeThing

3.成功返回的是object类型,要获取原类,只能继承/实现,或者就是那个代理类

4.对具体实现的方法内部并不关心,这个交给InvocationHandler.invoke那个方法里去处理就好了,我只想根据你给我的接口反射出对我有用的东西。

5.考虑到设计模式,以及proxy编者编写代码的逻辑使然


过程:

1.实现简单的JDK动态代理

1.1为什么需要这个接口的---这个接口==

  1. package Activeproxy.jdk;
  2. public interface tagService {
  3. public void printSomeThing();
  4. }

1.2编写他的简单实现类

  1. package Activeproxy.jdk;
  2. public class tagServiceImpl implements tagService {
  3. public final void printSomeThing() {
  4. System.err.println( "this is printSomeThing Core ServiceImpl");
  5. }
  6. }

1.3编写调用处理程序InvocationHandler,实现该方法,其实在后面调用的时候,具体方法的调用,会进入这里面按下面invoke里面的顺序执行。 三个参数,分别代表,传入的代理实现类,此次调用的方法名称,有关参数。这是平时使用的时候的动态代理的核心方法,看起来也很简答

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. public class jdkInvocation implements InvocationHandler {
  5. private Object object;
  6. public void setTagServiceObject(Object object) {
  7. this.object = object;
  8. }
  9. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  10. System.out.println( "TagService代理前");
  11. Object returnObject = method.invoke( this.object, args);
  12. System.out.println( "TagService代理后");
  13. return returnObject;
  14. }
  15. }

1.4编写调用类

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.Proxy;
  3. public class start {
  4. public static void main(String[] args) {
  5. jdkInvocation invocation = new jdkInvocation();
  6. invocation.setTagServiceObject( new tagServiceImpl());
  7. tagService service = (tagService) Proxy
  8. .newProxyInstance(start.class.getClassLoader(), new Class[] { tagService.class }, invocation);
  9. service.printSomeThing();
  10. }
  11. }

启动后,控制台会输出



就简单的动态代理来说在这里就结束了,但是并没有解决的我的疑问,为什么jdk动态代理需要接口

主要的秘密在newProxyInstance这个方法里

2.1在jdk 1.8里这个方法的代码是这样的

  1. @CallerSensitive
  2. public static Object newProxyInstance(ClassLoader loader,
  3. Class<?>[] interfaces,
  4. InvocationHandler h)
  5. throws IllegalArgumentException
  6. {
  7. Objects.requireNonNull(h);
  8. final Class<?>[] intfs = interfaces.clone();
  9. final SecurityManager sm = System.getSecurityManager();
  10. if (sm != null) {
  11. checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
  12. }
  13. /*
  14. * Look up or generate the designated proxy class.
  15. */
  16. Class<?> cl = getProxyClass0(loader, intfs);
  17. /*
  18. * Invoke its constructor with the designated invocation handler.
  19. */
  20. try {
  21. if (sm != null) {
  22. checkNewProxyPermission(Reflection.getCallerClass(), cl);
  23. }
  24. final Constructor<?> cons = cl.getConstructor(constructorParams);
  25. final InvocationHandler ih = h;
  26. if (!Modifier.isPublic(cl.getModifiers())) {
  27. AccessController.doPrivileged( new PrivilegedAction<Void>() {
  28. public Void run() {
  29. cons.setAccessible( true);
  30. return null;
  31. }
  32. });
  33. }
  34. return cons.newInstance( new Object[]{h});
  35. } catch (IllegalAccessException|InstantiationException e) {
  36. throw new InternalError(e.toString(), e);
  37. } catch (InvocationTargetException e) {
  38. Throwable t = e.getCause();
  39. if (t instanceof RuntimeException) {
  40. throw (RuntimeException) t;
  41. } else {
  42. throw new InternalError(t.toString(), t);
  43. }
  44. } catch (NoSuchMethodException e) {
  45. throw new InternalError(e.toString(), e);
  46. }
  47. }


讲解一下:

checkProxyAccess是验证一些参数

Class<?> cl = getProxyClass0(loader, intfs);是查找或生成指定的代理类(重点)

final Constructor<?> cons = cl.getConstructor(constructorParams);这行代码是获取,生成代理类的构造函数是 InvocationHandler参数的方法

return cons.newInstance(new Object[]{h});这行代码的意思是将h,就是实现InvocationHandler的jdkInvocation注入到cons中。然后newInstance生成一个已经组装过参数的代理类。

现在这个参数名是cl的代理类,经过,获得cl,注入InvocationHandler的实现类。通过他newInstance的类,我们可以猜测一下,应该是有一个构造方法可以注入InvocationHandler的实现类。而且,还能被(tagService)这样强转,那么应该是继承了或者实现了tagService。

so,到这一步,理论上来说我们可以知道:一个代理类,继承或者实现了我们专门创的接口,且内部有构造方法接受InvocationHandler的实现类。两个类关联起来了。而且,如果调用实例化成功的话,我们已经可以通过接口访问内部的方法了。


那么重点来了,这个方法里的重点就是getProxyClass0(loader, intfs);这个方法里面是什么样的,做了哪些操作,返回的是什么代理类

3.1 getProxyClass0   通过该类源码,发现其实他是从  proxyClassCache里获取的。这个是已经获取完毕后放在所谓,有关代理的静态缓存中。怎么处理的秘密在ProxyClassFactory中

  1. private static Class<?> getProxyClass0(ClassLoader loader,
  2. Class<?>... interfaces) {
  3. if (interfaces.length > 65535) {
  4. throw new IllegalArgumentException( "interface limit exceeded");
  5. }
  6. // If the proxy class defined by the given loader implementing
  7. // the given interfaces exists, this will simply return the cached copy;
  8. // otherwise, it will create the proxy class via the ProxyClassFactory
  9. return proxyClassCache.get(loader, interfaces);
  10. }
3.2.ProxyClassFactory
  1. private static final class ProxyClassFactory
  2. implements BiFunction< ClassLoader, Class<?>[], Class<?>>
  3. {
  4. // prefix for all proxy class names
  5. private static final String proxyClassNamePrefix = "$Proxy";
  6. // next number to use for generation of unique proxy class names
  7. private static final AtomicLong nextUniqueNumber = new AtomicLong();
  8. @Override
  9. public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
  10. Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
  11. for (Class<?> intf : interfaces) {
  12. /*
  13. * Verify that the class loader resolves the name of this
  14. * interface to the same Class object.
  15. */
  16. Class<?> interfaceClass = null;
  17. try {
  18. interfaceClass = Class.forName(intf.getName(), false, loader);
  19. } catch (ClassNotFoundException e) {
  20. }
  21. if (interfaceClass != intf) {
  22. throw new IllegalArgumentException(
  23. intf + " is not visible from class loader");
  24. }
  25. /*
  26. * Verify that the Class object actually represents an
  27. * interface.
  28. */
  29. if (!interfaceClass.isInterface()) {
  30. throw new IllegalArgumentException(
  31. interfaceClass.getName() + " is not an interface");
  32. }
  33. /*
  34. * Verify that this interface is not a duplicate.
  35. */
  36. if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
  37. throw new IllegalArgumentException(
  38. "repeated interface: " + interfaceClass.getName());
  39. }
  40. }
  41. String proxyPkg = null; // package to define proxy class in
  42. int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
  43. /*
  44. * Record the package of a non-public proxy interface so that the
  45. * proxy class will be defined in the same package. Verify that
  46. * all non-public proxy interfaces are in the same package.
  47. */
  48. for (Class<?> intf : interfaces) {
  49. int flags = intf.getModifiers();
  50. if (!Modifier.isPublic(flags)) {
  51. accessFlags = Modifier.FINAL;
  52. String name = intf.getName();
  53. int n = name.lastIndexOf( '.');
  54. String pkg = ((n == - 1) ? "" : name.substring( 0, n + 1));
  55. if (proxyPkg == null) {
  56. proxyPkg = pkg;
  57. } else if (!pkg.equals(proxyPkg)) {
  58. throw new IllegalArgumentException(
  59. "non-public interfaces from different packages");
  60. }
  61. }
  62. }
  63. if (proxyPkg == null) {
  64. // if no non-public proxy interfaces, use com.sun.proxy package
  65. proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
  66. }
  67. /*
  68. * Choose a name for the proxy class to generate.
  69. */
  70. long num = nextUniqueNumber.getAndIncrement();
  71. String proxyName = proxyPkg + proxyClassNamePrefix + num;
  72. /*
  73. * Generate the specified proxy class.
  74. */
  75. byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
  76. proxyName, interfaces, accessFlags);
  77. try {
  78. return defineClass0(loader, proxyName,
  79. proxyClassFile, 0, proxyClassFile.length);
  80. } catch (ClassFormatError e) {
  81. /*
  82. * A ClassFormatError here means that (barring bugs in the
  83. * proxy class generation code) there was some other
  84. * invalid aspect of the arguments supplied to the proxy
  85. * class creation (such as virtual machine limitations
  86. * exceeded).
  87. */
  88. throw new IllegalArgumentException(e.toString());
  89. }
  90. }
  91. }
这么长的一大串代码其实也没干太多事,无非也就是验证参数,验证接口,反射获得接口。重点是

 byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);

这个方法就是要找到的原因。他隐藏起来了最关键的,怎么组装了一个byte类型代理类。在后面传了回去

通过反编译,我们得知,返回的结果是,

  1. package com.sun.proxy;
  2. import com.Activeproxy.jdk.tagService;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. import java.lang.reflect.UndeclaredThrowableException;
  7. public final class $Proxy0 extends Proxy implements tagService{
  8. private static Method m1;
  9. private static Method m3;
  10. private static Method m0;
  11. private static Method m2;
  12. public $Proxy0(InvocationHandler paramInvocationHandler) {
  13. super(paramInvocationHandler);
  14. }
  15. public final boolean equals(Object paramObject) {
  16. try {
  17. return ((Boolean) this.h.invoke( this, m1, new Object[] { paramObject })).booleanValue();
  18. }
  19. catch (Error|RuntimeException localError) {
  20. throw localError;
  21. }
  22. catch (Throwable localThrowable) {
  23. throw new UndeclaredThrowableException(localThrowable);
  24. }
  25. }
  26. public final void printSomeThing(String paramString) {
  27. try {
  28. this.h.invoke( this, m3, new Object[] { paramString });
  29. return;
  30. }
  31. catch (Error|RuntimeException localError) {
  32. throw localError;
  33. }
  34. catch (Throwable localThrowable) {
  35. throw new UndeclaredThrowableException(localThrowable);
  36. }
  37. }
  38. public final int hashCode() {
  39. try {
  40. return ((Integer) this.h.invoke( this, m0, null)).intValue();
  41. }
  42. catch (Error|RuntimeException localError) {
  43. throw localError;
  44. }
  45. catch (Throwable localThrowable) {
  46. throw new UndeclaredThrowableException(localThrowable);
  47. }
  48. }
  49. public final String toString() {
  50. try {
  51. return (String) this.h.invoke( this, m2, null);
  52. }
  53. catch (Error|RuntimeException localError) {
  54. throw localError;
  55. }
  56. catch (Throwable localThrowable) {
  57. throw new UndeclaredThrowableException(localThrowable);
  58. }
  59. }
  60. static {
  61. try {
  62. m1 = Class.forName( "java.lang.Object").getMethod( "equals", new Class[] { Class.forName( "java.lang.Object") });
  63. m3 = Class.forName( "com.Activeproxy.jdk.tagService").getMethod( "printSomeThing", new Class[ 0]);
  64. m0 = Class.forName( "java.lang.Object").getMethod( "hashCode", new Class[ 0]);
  65. m2 = Class.forName( "java.lang.Object").getMethod( "toString", new Class[ 0]);
  66. return;
  67. }
  68. catch (NoSuchMethodException localNoSuchMethodException) {
  69. throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
  70. }
  71. catch (ClassNotFoundException localClassNotFoundException) {
  72. throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
  73. }
  74. }
  75. }
看到这个方法,就算破案了。这个就是jdk动态代理和为什么需要接口。因为

1.在需要继承proxy类获得有关方法和InvocationHandler构造方法传参的同时,java不能同时继承两个类,我们需要和想要代理的类建立联系,只能实现一个接口

2.需要反射获得代理类的有关参数,必须要通过某个类,反射获取有关方法,如本次测试用的 :printSomeThing

3.成功返回的是object类型,要获取原类,只能继承/实现,或者就是那个代理类

4.对具体实现的方法内部并不关心,这个交给InvocationHandler.invoke那个方法里去处理就好了,我只想根据你给我的接口反射出对我有用的东西。

5.考虑到设计模式,以及proxy编者编写代码的逻辑使然


这就是jdk的动态代理及为什么需要接口

https://blog.csdn.net/zxysshgood/article/details/78684229

动态代理有关,无非是使用JDK动态代理,和cglib动态代理。一直不待明白的是为什么,jdk的动态代理需要接口才能实现,这也是其短板和令人诟病的地方。很多的博文说的很复杂,代码一大堆,没有太明白。手打了一下,参考了一些优秀的博文,在这里给自己做个总结。

首先,动态代理是个挺有用的东西,常见的就是javaAOP的有关,主要的作用是是在一个方法使用前后,能进行别的处理。比如吧,aop所说的,面向切面编程,日志有关,检测有关。都可以通过AOP或者说动态代理来实现。

先来看下最简单的代码下实现动态代理的情况,然后看下源码和我们的主题,为什么需要接口

为什么需要接口,先上结论

1.在需要继承proxy类获得有关方法和InvocationHandler构造方法传参的同时,java不能同时继承两个类,我们需要和想要代理的类建立联系,只能实现一个接口

2.需要反射获得代理类的有关参数,必须要通过某个类,反射获取有关方法,如本次测试用的 :printSomeThing

3.成功返回的是object类型,要获取原类,只能继承/实现,或者就是那个代理类

4.对具体实现的方法内部并不关心,这个交给InvocationHandler.invoke那个方法里去处理就好了,我只想根据你给我的接口反射出对我有用的东西。

5.考虑到设计模式,以及proxy编者编写代码的逻辑使然


过程:

1.实现简单的JDK动态代理

1.1为什么需要这个接口的---这个接口==

  1. package Activeproxy.jdk;
  2. public interface tagService {
  3. public void printSomeThing();
  4. }

1.2编写他的简单实现类

  1. package Activeproxy.jdk;
  2. public class tagServiceImpl implements tagService {
  3. public final void printSomeThing() {
  4. System.err.println( "this is printSomeThing Core ServiceImpl");
  5. }
  6. }

1.3编写调用处理程序InvocationHandler,实现该方法,其实在后面调用的时候,具体方法的调用,会进入这里面按下面invoke里面的顺序执行。 三个参数,分别代表,传入的代理实现类,此次调用的方法名称,有关参数。这是平时使用的时候的动态代理的核心方法,看起来也很简答

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. public class jdkInvocation implements InvocationHandler {
  5. private Object object;
  6. public void setTagServiceObject(Object object) {
  7. this.object = object;
  8. }
  9. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  10. System.out.println( "TagService代理前");
  11. Object returnObject = method.invoke( this.object, args);
  12. System.out.println( "TagService代理后");
  13. return returnObject;
  14. }
  15. }

1.4编写调用类

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.Proxy;
  3. public class start {
  4. public static void main(String[] args) {
  5. jdkInvocation invocation = new jdkInvocation();
  6. invocation.setTagServiceObject( new tagServiceImpl());
  7. tagService service = (tagService) Proxy
  8. .newProxyInstance(start.class.getClassLoader(), new Class[] { tagService.class }, invocation);
  9. service.printSomeThing();
  10. }
  11. }

启动后,控制台会输出



就简单的动态代理来说在这里就结束了,但是并没有解决的我的疑问,为什么jdk动态代理需要接口

主要的秘密在newProxyInstance这个方法里

2.1在jdk 1.8里这个方法的代码是这样的

  1. @CallerSensitive
  2. public static Object newProxyInstance(ClassLoader loader,
  3. Class<?>[] interfaces,
  4. InvocationHandler h)
  5. throws IllegalArgumentException
  6. {
  7. Objects.requireNonNull(h);
  8. final Class<?>[] intfs = interfaces.clone();
  9. final SecurityManager sm = System.getSecurityManager();
  10. if (sm != null) {
  11. checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
  12. }
  13. /*
  14. * Look up or generate the designated proxy class.
  15. */
  16. Class<?> cl = getProxyClass0(loader, intfs);
  17. /*
  18. * Invoke its constructor with the designated invocation handler.
  19. */
  20. try {
  21. if (sm != null) {
  22. checkNewProxyPermission(Reflection.getCallerClass(), cl);
  23. }
  24. final Constructor<?> cons = cl.getConstructor(constructorParams);
  25. final InvocationHandler ih = h;
  26. if (!Modifier.isPublic(cl.getModifiers())) {
  27. AccessController.doPrivileged( new PrivilegedAction<Void>() {
  28. public Void run() {
  29. cons.setAccessible( true);
  30. return null;
  31. }
  32. });
  33. }
  34. return cons.newInstance( new Object[]{h});
  35. } catch (IllegalAccessException|InstantiationException e) {
  36. throw new InternalError(e.toString(), e);
  37. } catch (InvocationTargetException e) {
  38. Throwable t = e.getCause();
  39. if (t instanceof RuntimeException) {
  40. throw (RuntimeException) t;
  41. } else {
  42. throw new InternalError(t.toString(), t);
  43. }
  44. } catch (NoSuchMethodException e) {
  45. throw new InternalError(e.toString(), e);
  46. }
  47. }


讲解一下:

checkProxyAccess是验证一些参数

Class<?> cl = getProxyClass0(loader, intfs);是查找或生成指定的代理类(重点)

final Constructor<?> cons = cl.getConstructor(constructorParams);这行代码是获取,生成代理类的构造函数是 InvocationHandler参数的方法

return cons.newInstance(new Object[]{h});这行代码的意思是将h,就是实现InvocationHandler的jdkInvocation注入到cons中。然后newInstance生成一个已经组装过参数的代理类。

现在这个参数名是cl的代理类,经过,获得cl,注入InvocationHandler的实现类。通过他newInstance的类,我们可以猜测一下,应该是有一个构造方法可以注入InvocationHandler的实现类。而且,还能被(tagService)这样强转,那么应该是继承了或者实现了tagService。

so,到这一步,理论上来说我们可以知道:一个代理类,继承或者实现了我们专门创的接口,且内部有构造方法接受InvocationHandler的实现类。两个类关联起来了。而且,如果调用实例化成功的话,我们已经可以通过接口访问内部的方法了。


那么重点来了,这个方法里的重点就是getProxyClass0(loader, intfs);这个方法里面是什么样的,做了哪些操作,返回的是什么代理类

3.1 getProxyClass0   通过该类源码,发现其实他是从  proxyClassCache里获取的。这个是已经获取完毕后放在所谓,有关代理的静态缓存中。怎么处理的秘密在ProxyClassFactory中

  1. private static Class<?> getProxyClass0(ClassLoader loader,
  2. Class<?>... interfaces) {
  3. if (interfaces.length > 65535) {
  4. throw new IllegalArgumentException( "interface limit exceeded");
  5. }
  6. // If the proxy class defined by the given loader implementing
  7. // the given interfaces exists, this will simply return the cached copy;
  8. // otherwise, it will create the proxy class via the ProxyClassFactory
  9. return proxyClassCache.get(loader, interfaces);
  10. }
3.2.ProxyClassFactory
  1. private static final class ProxyClassFactory
  2. implements BiFunction< ClassLoader, Class<?>[], Class<?>>
  3. {
  4. // prefix for all proxy class names
  5. private static final String proxyClassNamePrefix = "$Proxy";
  6. // next number to use for generation of unique proxy class names
  7. private static final AtomicLong nextUniqueNumber = new AtomicLong();
  8. @Override
  9. public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
  10. Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
  11. for (Class<?> intf : interfaces) {
  12. /*
  13. * Verify that the class loader resolves the name of this
  14. * interface to the same Class object.
  15. */
  16. Class<?> interfaceClass = null;
  17. try {
  18. interfaceClass = Class.forName(intf.getName(), false, loader);
  19. } catch (ClassNotFoundException e) {
  20. }
  21. if (interfaceClass != intf) {
  22. throw new IllegalArgumentException(
  23. intf + " is not visible from class loader");
  24. }
  25. /*
  26. * Verify that the Class object actually represents an
  27. * interface.
  28. */
  29. if (!interfaceClass.isInterface()) {
  30. throw new IllegalArgumentException(
  31. interfaceClass.getName() + " is not an interface");
  32. }
  33. /*
  34. * Verify that this interface is not a duplicate.
  35. */
  36. if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
  37. throw new IllegalArgumentException(
  38. "repeated interface: " + interfaceClass.getName());
  39. }
  40. }
  41. String proxyPkg = null; // package to define proxy class in
  42. int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
  43. /*
  44. * Record the package of a non-public proxy interface so that the
  45. * proxy class will be defined in the same package. Verify that
  46. * all non-public proxy interfaces are in the same package.
  47. */
  48. for (Class<?> intf : interfaces) {
  49. int flags = intf.getModifiers();
  50. if (!Modifier.isPublic(flags)) {
  51. accessFlags = Modifier.FINAL;
  52. String name = intf.getName();
  53. int n = name.lastIndexOf( '.');
  54. String pkg = ((n == - 1) ? "" : name.substring( 0, n + 1));
  55. if (proxyPkg == null) {
  56. proxyPkg = pkg;
  57. } else if (!pkg.equals(proxyPkg)) {
  58. throw new IllegalArgumentException(
  59. "non-public interfaces from different packages");
  60. }
  61. }
  62. }
  63. if (proxyPkg == null) {
  64. // if no non-public proxy interfaces, use com.sun.proxy package
  65. proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
  66. }
  67. /*
  68. * Choose a name for the proxy class to generate.
  69. */
  70. long num = nextUniqueNumber.getAndIncrement();
  71. String proxyName = proxyPkg + proxyClassNamePrefix + num;
  72. /*
  73. * Generate the specified proxy class.
  74. */
  75. byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
  76. proxyName, interfaces, accessFlags);
  77. try {
  78. return defineClass0(loader, proxyName,
  79. proxyClassFile, 0, proxyClassFile.length);
  80. } catch (ClassFormatError e) {
  81. /*
  82. * A ClassFormatError here means that (barring bugs in the
  83. * proxy class generation code) there was some other
  84. * invalid aspect of the arguments supplied to the proxy
  85. * class creation (such as virtual machine limitations
  86. * exceeded).
  87. */
  88. throw new IllegalArgumentException(e.toString());
  89. }
  90. }
  91. }
这么长的一大串代码其实也没干太多事,无非也就是验证参数,验证接口,反射获得接口。重点是

 byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);

这个方法就是要找到的原因。他隐藏起来了最关键的,怎么组装了一个byte类型代理类。在后面传了回去

通过反编译,我们得知,返回的结果是,

  1. package com.sun.proxy;
  2. import com.Activeproxy.jdk.tagService;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. import java.lang.reflect.UndeclaredThrowableException;
  7. public final class $Proxy0 extends Proxy implements tagService{
  8. private static Method m1;
  9. private static Method m3;
  10. private static Method m0;
  11. private static Method m2;
  12. public $Proxy0(InvocationHandler paramInvocationHandler) {
  13. super(paramInvocationHandler);
  14. }
  15. public final boolean equals(Object paramObject) {
  16. try {
  17. return ((Boolean) this.h.invoke( this, m1, new Object[] { paramObject })).booleanValue();
  18. }
  19. catch (Error|RuntimeException localError) {
  20. throw localError;
  21. }
  22. catch (Throwable localThrowable) {
  23. throw new UndeclaredThrowableException(localThrowable);
  24. }
  25. }
  26. public final void printSomeThing(String paramString) {
  27. try {
  28. this.h.invoke( this, m3, new Object[] { paramString });
  29. return;
  30. }
  31. catch (Error|RuntimeException localError) {
  32. throw localError;
  33. }
  34. catch (Throwable localThrowable) {
  35. throw new UndeclaredThrowableException(localThrowable);
  36. }
  37. }
  38. public final int hashCode() {
  39. try {
  40. return ((Integer) this.h.invoke( this, m0, null)).intValue();
  41. }
  42. catch (Error|RuntimeException localError) {
  43. throw localError;
  44. }
  45. catch (Throwable localThrowable) {
  46. throw new UndeclaredThrowableException(localThrowable);
  47. }
  48. }
  49. public final String toString() {
  50. try {
  51. return (String) this.h.invoke( this, m2, null);
  52. }
  53. catch (Error|RuntimeException localError) {
  54. throw localError;
  55. }
  56. catch (Throwable localThrowable) {
  57. throw new UndeclaredThrowableException(localThrowable);
  58. }
  59. }
  60. static {
  61. try {
  62. m1 = Class.forName( "java.lang.Object").getMethod( "equals", new Class[] { Class.forName( "java.lang.Object") });
  63. m3 = Class.forName( "com.Activeproxy.jdk.tagService").getMethod( "printSomeThing", new Class[ 0]);
  64. m0 = Class.forName( "java.lang.Object").getMethod( "hashCode", new Class[ 0]);
  65. m2 = Class.forName( "java.lang.Object").getMethod( "toString", new Class[ 0]);
  66. return;
  67. }
  68. catch (NoSuchMethodException localNoSuchMethodException) {
  69. throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
  70. }
  71. catch (ClassNotFoundException localClassNotFoundException) {
  72. throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
  73. }
  74. }
  75. }
看到这个方法,就算破案了。这个就是jdk动态代理和为什么需要接口。因为

1.在需要继承proxy类获得有关方法和InvocationHandler构造方法传参的同时,java不能同时继承两个类,我们需要和想要代理的类建立联系,只能实现一个接口

2.需要反射获得代理类的有关参数,必须要通过某个类,反射获取有关方法,如本次测试用的 :printSomeThing

3.成功返回的是object类型,要获取原类,只能继承/实现,或者就是那个代理类

4.对具体实现的方法内部并不关心,这个交给InvocationHandler.invoke那个方法里去处理就好了,我只想根据你给我的接口反射出对我有用的东西。

5.考虑到设计模式,以及proxy编者编写代码的逻辑使然


这就是jdk的动态代理及为什么需要接口

猜你喜欢

转载自blog.csdn.net/qq_41641296/article/details/80998939