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;
  17. import java.lang.reflect.Method;
  18. /**
  19. * Part of a Pointcut. Checks whether the target method is eligible for advice.
  20. *
  21. * <p>A MethodMatcher may be evaluated <b>statically</b> or at <b>runtime</b> (dynamically).
  22. * Static matching involves method and (possibly) method attributes. Dynamic matching
  23. * also makes arguments for a particular call available, and any effects of running
  24. * previous advice applying to the joinpoint.
  25. *
  26. * <p>If an implementation returns false in its isRuntime() method, evaluation can be performed
  27. * statically, and the result will be the same for all invocations of this method,
  28. * whatever their arguments. If the isRuntime() method returns false, the 3-arg matches()
  29. * method will never be invoked.
  30. *
  31. * <p>If an implementation returns true in its 2-arg matches() method, and its isRuntime()
  32. * method returns true, the 3-argument matches() method will be invoked <i>immediately before
  33. * each potential execution of the related advice</i>, to decide whether the advice should run.
  34. * All previous advice, such as earlier interceptors in an interceptor chain, will have run,
  35. * so any state changes they have produced in parameters or ThreadLocal state, will be
  36. * available at the time of evaluation.
  37. *
  38. * @author Rod Johnson
  39. * @since 11-Nov-2003
  40. * @version $Id: MethodMatcher.java,v 1.5 2004/03/18 02:46:07 trisberg Exp $
  41. */
  42. public interface MethodMatcher {
  43. /**
  44. * Perform static checking. If this returns false, or if the isRuntime() method
  45. * returns false, no runtime check will be made.
  46. * @param m candidate method
  47. * @param targetClass target class (may be null, in which case the candidate
  48. * class must be taken to be the method's declaring class)
  49. * @return whether or not this method matches statically
  50. */
  51. boolean matches(Method m, Class targetClass);
  52. /**
  53. * Is this MethodMatcher dynamic?
  54. * Must a final call be made on the matches(Method, Class, Object[]) method at runtime
  55. * even if the 2-arg matches method returns true?
  56. * Can be invoked when an AOP proxy is created, and need not be invoked
  57. * again before each method invocation,
  58. * <p>Note: Could be pulled up into a Matcher super-interface to apply to
  59. * fields also.
  60. * @return whether or not a runtime matche via the 3-arg matches() method is
  61. * required if static matching passed.
  62. */
  63. boolean isRuntime();
  64. /**
  65. * Is there a runtime (dynamic) match for this method, which must have matched
  66. * statically. This method is
  67. * invoked only if the 2-arg matches method returns true for the given method
  68. * and target class, and if the isRuntime() method returns true.
  69. * Invoked immediately before potentially running of the advice, after any
  70. * advice earlier in the advice chain has run.
  71. * @param m candidate method
  72. * @param targetClass target class
  73. * @param args arguments to the method
  74. * @return whether there's a runtime match
  75. * @see MethodMatcher#matches(Method, Class)
  76. */
  77. boolean matches(Method m, Class targetClass, Object[] args);
  78. /**
  79. * Canonical instance that matches all methods.
  80. */
  81. MethodMatcher TRUE = new MethodMatcher() {
  82. public boolean isRuntime() {
  83. return false;
  84. }
  85. public boolean matches(Method m, Class targetClass) {
  86. return true;
  87. }
  88. public boolean matches(Method m, Class targetClass, Object[] args) {
  89. // should never be invoked as isRuntime returns false
  90. throw new UnsupportedOperationException();
  91. }
  92. };
  93. }