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. import org.springframework.core.ControlFlow;
  22. import org.springframework.core.ControlFlowFactory;
  23. /**
  24. * Pointcut and method matcher for use in simple <b>cflow</b>-style pointcut.
  25. * Note that evaluating such pointcuts is 10-15 times slower than evaluating
  26. * normal pointcuts, but they are useful in some cases.
  27. * @see org.springframework.core.ControlFlow
  28. * @author Rod Johnson
  29. * @version $Id: ControlFlowPointcut.java,v 1.6 2004/03/18 02:46:11 trisberg Exp $
  30. */
  31. public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher {
  32. private Class clazz;
  33. private String methodName;
  34. private int evaluations;
  35. public ControlFlowPointcut(Class clazz) {
  36. this(clazz, null);
  37. }
  38. /**
  39. * Construct a new pointcut that matches all calls below the
  40. * given method in the given class. If the method name is null,
  41. * matches all control flows below that class.
  42. * @param clazz
  43. * @param methodName
  44. */
  45. public ControlFlowPointcut(Class clazz, String methodName) {
  46. this.clazz = clazz;
  47. this.methodName = methodName;
  48. }
  49. /**
  50. * Subclasses can override this for greater filtering (and performance).
  51. */
  52. public boolean matches(Class clazz) {
  53. return true;
  54. }
  55. /**
  56. * Subclasses can override this if it's possible to filter out
  57. * some candidate classes.
  58. */
  59. public boolean matches(Method m, Class targetClass) {
  60. return true;
  61. }
  62. public boolean isRuntime() {
  63. return true;
  64. }
  65. public boolean matches(Method m, Class targetClass, Object[] args) {
  66. ++evaluations;
  67. ControlFlow cflow = ControlFlowFactory.createControlFlow();
  68. return (methodName != null) ? cflow.under(clazz, methodName) : cflow.under(clazz);
  69. }
  70. /**
  71. * It's useful to know how many times we've fired, for optimization.
  72. */
  73. public int getEvaluations() {
  74. return evaluations;
  75. }
  76. public ClassFilter getClassFilter() {
  77. return this;
  78. }
  79. public MethodMatcher getMethodMatcher() {
  80. return this;
  81. }
  82. }