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.ClassFilter;
  19. import org.springframework.aop.MethodMatcher;
  20. import org.springframework.aop.Pointcut;
  21. /**
  22. * Pointcut unions are tricky, because we can't just
  23. * OR the MethodMatchers: we need to check that each MethodMatcher's
  24. * ClassFilter was happy as well.
  25. * @author Rod Johnson
  26. * @version $Id: UnionPointcut.java,v 1.2 2004/03/18 02:46:11 trisberg Exp $
  27. */
  28. class UnionPointcut implements Pointcut {
  29. private final Pointcut a;
  30. private final Pointcut b;
  31. private MethodMatcher mm;
  32. public UnionPointcut(Pointcut a, Pointcut b) {
  33. this.a = a;
  34. this.b = b;
  35. this.mm = new PointcutUnionMethodMatcher();
  36. }
  37. /**
  38. * @see org.springframework.aop.Pointcut#getClassFilter()
  39. */
  40. public ClassFilter getClassFilter() {
  41. return ClassFilters.union(a.getClassFilter(), b.getClassFilter());
  42. }
  43. /**
  44. * @see org.springframework.aop.Pointcut#getMethodMatcher()
  45. */
  46. public MethodMatcher getMethodMatcher() {
  47. // Complicated: we need to consider both class filter and method matcher
  48. return mm;
  49. }
  50. private class PointcutUnionMethodMatcher implements MethodMatcher {
  51. public boolean matches(Method m, Class targetClass) {
  52. return (a.getClassFilter().matches(targetClass) && a.getMethodMatcher().matches(m, targetClass)) ||
  53. (b.getClassFilter().matches(targetClass) && b.getMethodMatcher().matches(m, targetClass));
  54. }
  55. public boolean isRuntime() {
  56. return a.getMethodMatcher().isRuntime() || b.getMethodMatcher().isRuntime();
  57. }
  58. public boolean matches(Method m, Class targetClass, Object[] args) {
  59. // 2-arg matcher will already have run, so we don't need to do class filtering again
  60. return a.getMethodMatcher().matches(m, targetClass, args) || b.getMethodMatcher().matches(m, targetClass, args);
  61. }
  62. }
  63. }