1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.aop.framework;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.util.Arrays;
  20. import org.aopalliance.aop.AspectException;
  21. /**
  22. * Miscellaneous utilities for AOP proxies
  23. * @author Rod Johnson
  24. * @version $Id: AopProxyUtils.java,v 1.6 2004/03/19 21:35:54 johnsonr Exp $
  25. */
  26. public abstract class AopProxyUtils {
  27. /**
  28. * Get complete set of interfaces to proxy. This will always add the ProxyConfig interface.
  29. * @return the complete set of interfaces to proxy
  30. */
  31. public static Class[] completeProxiedInterfaces(AdvisedSupport advised) {
  32. Class[] proxiedInterfaces = advised.getProxiedInterfaces();
  33. if (proxiedInterfaces == null ||proxiedInterfaces.length == 0) {
  34. proxiedInterfaces = new Class[1];
  35. proxiedInterfaces[0] = Advised.class;
  36. }
  37. else {
  38. // Don't add the interface twice if it's already there
  39. if (!advised.isInterfaceProxied(Advised.class)) {
  40. proxiedInterfaces = new Class[advised.getProxiedInterfaces().length + 1];
  41. proxiedInterfaces[0] = Advised.class;
  42. System.arraycopy(advised.getProxiedInterfaces(), 0, proxiedInterfaces, 1, advised.getProxiedInterfaces().length);
  43. }
  44. }
  45. return proxiedInterfaces;
  46. }
  47. /**
  48. * Invoke the target directly via reflection.
  49. */
  50. public static Object invokeJoinpointUsingReflection(Object target, Method m, Object[] args) throws Throwable {
  51. // Use reflection to invoke the method
  52. try {
  53. Object rval = m.invoke(target, args);
  54. return rval;
  55. }
  56. catch (InvocationTargetException ex) {
  57. // Invoked method threw a checked exception.
  58. // We must rethrow it. The client won't see the interceptor.
  59. throw ex.getTargetException();
  60. }
  61. catch (IllegalArgumentException ex) {
  62. throw new AspectException("AOP configuration seems to be invalid: tried calling " + m + " on [" + target + "]: " + ex);
  63. }
  64. catch (IllegalAccessException ex) {
  65. throw new AspectException("Couldn't access method " + m, ex);
  66. }
  67. }
  68. /**
  69. * Note the same as equality of the AdvisedSupport objects.
  70. */
  71. public static boolean equalsInProxy(AdvisedSupport a, AdvisedSupport b) {
  72. if (a == b)
  73. return true;
  74. if (!Arrays.equals(a.getProxiedInterfaces(), b.getProxiedInterfaces()))
  75. return false;
  76. if (!Arrays.equals(a.getAdvisors(), b.getAdvisors()))
  77. return false;
  78. if (a.getTargetSource() == null)
  79. return b.getTargetSource() == null;
  80. return a.getTargetSource().equals(b.getTargetSource());
  81. }
  82. }