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.support;
  17. import java.lang.reflect.Method;
  18. import org.springframework.aop.MethodMatcher;
  19. /**
  20. * Static methods useful for composing MethodMatchers. A MethodMatcher may be
  21. * evaluated statically (based on method and target class) or need further
  22. * evaluation dynamically (based on arguments at the time of method invocation).
  23. * @author Rod Johnson
  24. * @since 11-Nov-2003
  25. * @version $Id: MethodMatchers.java,v 1.5 2004/05/23 20:50:29 jhoeller Exp $
  26. */
  27. public abstract class MethodMatchers {
  28. public static MethodMatcher union(MethodMatcher a, MethodMatcher b) {
  29. return new UnionMethodMatcher(a, b);
  30. }
  31. public static MethodMatcher intersection(MethodMatcher a, MethodMatcher b) {
  32. return new IntersectionMethodMatcher(a, b);
  33. }
  34. private static class UnionMethodMatcher implements MethodMatcher {
  35. private MethodMatcher a;
  36. private MethodMatcher b;
  37. private UnionMethodMatcher(MethodMatcher a, MethodMatcher b) {
  38. this.a = a;
  39. this.b = b;
  40. }
  41. public boolean matches(Method m, Class targetClass) {
  42. return a.matches(m, targetClass) || b.matches(m, targetClass);
  43. }
  44. public boolean isRuntime() {
  45. return a.isRuntime() || b.isRuntime();
  46. }
  47. public boolean matches(Method m, Class targetClass, Object[] args) {
  48. return a.matches(m, targetClass, args) || b.matches(m, targetClass, args);
  49. }
  50. }
  51. private static class IntersectionMethodMatcher implements MethodMatcher {
  52. private MethodMatcher a;
  53. private MethodMatcher b;
  54. private IntersectionMethodMatcher(MethodMatcher a, MethodMatcher b) {
  55. this.a = a;
  56. this.b = b;
  57. }
  58. public boolean matches(Method m, Class targetClass) {
  59. return a.matches(m, targetClass) && b.matches(m, targetClass);
  60. }
  61. public boolean isRuntime() {
  62. return a.isRuntime() || b.isRuntime();
  63. }
  64. public boolean matches(Method m, Class targetClass, Object[] args) {
  65. // Because a dynamic intersection may be composed of a static and dynamic part,
  66. // we must avoid calling the 3-arg matches method on a dynamic matcher, as
  67. // it will probably be an unsupported operation.
  68. boolean aMatches = a.isRuntime() ? a.matches(m, targetClass, args) : a.matches(m, targetClass);
  69. boolean bMatches = b.isRuntime() ? b.matches(m, targetClass, args) : b.matches(m, targetClass);
  70. return aMatches && bMatches;
  71. }
  72. }
  73. }