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.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.apache.oro.text.regex.MalformedPatternException;
  21. import org.apache.oro.text.regex.Pattern;
  22. import org.apache.oro.text.regex.PatternMatcher;
  23. import org.apache.oro.text.regex.Perl5Compiler;
  24. import org.apache.oro.text.regex.Perl5Matcher;
  25. import org.springframework.aop.ClassFilter;
  26. /**
  27. * Perl5 regular expression pointcut bean. JavaBean properties are:
  28. * <li>pattern: Perl5 regular expression for the fully-qualified method names to match
  29. * <li>patterns: alternative property taking a String array of patterns. The result will
  30. * be the union of these patterns.
  31. * <li>interceptor: AOP Alliance interceptor to invoke if the pointcut matches
  32. * Matching is based purely on method name.
  33. *
  34. * <p>Note: the regular expressions must be a match. For example,
  35. * <code>.*get.*</code> will match com.mycom.Foo.getBar().
  36. * <code>get.*</code> will not.
  37. *
  38. * <p>Currently uses Jakarta ORO regular expression library.
  39. * Does not require J2SE 1.4, although it runs under 1.4.
  40. *
  41. * @author Rod Johnson
  42. * @since July 22, 2003
  43. * @version $Id: RegexpMethodPointcut.java,v 1.6 2004/03/18 02:46:11 trisberg Exp $
  44. */
  45. public class RegexpMethodPointcut extends StaticMethodMatcherPointcut implements ClassFilter {
  46. private final Log logger = LogFactory.getLog(getClass());
  47. /** Regular expressions to match */
  48. private String[] patterns = new String[0];
  49. /** ORO's compiled form of this pattern */
  50. private Pattern[] compiledPatterns = new Pattern[0];
  51. /** ORO pattern matcher to use */
  52. private PatternMatcher matcher;
  53. /**
  54. * Convenience method when we have only a single pattern.
  55. * Use either this method or setPatterns(), not both.
  56. * @see #setPatterns
  57. */
  58. public void setPattern(String pattern) throws MalformedPatternException {
  59. setPatterns(new String[] { pattern });
  60. }
  61. /**
  62. * Set the regular expressions defining methods to match.
  63. * Matching will be the union of all these; if any match,
  64. * the pointcut matches.
  65. * @param patterns Perl5 regular expressions describing methods
  66. * to match
  67. */
  68. public void setPatterns(String[] patterns) throws MalformedPatternException {
  69. this.patterns = patterns;
  70. this.compiledPatterns = new Pattern[patterns.length];
  71. Perl5Compiler compiler = new Perl5Compiler();
  72. for (int i = 0; i < patterns.length; i++) {
  73. // Compile the pattern to be threadsafe
  74. this.compiledPatterns[i] = compiler.compile(patterns[i], Perl5Compiler.READ_ONLY_MASK);
  75. }
  76. this.matcher = new Perl5Matcher();
  77. }
  78. /**
  79. * Return the regular expressions for method matching.
  80. */
  81. public String[] getPatterns() {
  82. return patterns;
  83. }
  84. /**
  85. * Try to match the regular expression against the fully qualified name
  86. * of the method's declaring class, plus the name of the method.
  87. * Note that the declaring class is that class that originally declared
  88. * the method, not necessarily the class that's currently exposing it.
  89. * <p>For example, <code>java.lang.Object.hashCode</code> matches
  90. * any subclass of object's hashCode() method.
  91. */
  92. public boolean matches(Method m, Class targetClass) {
  93. // TODO use target class here?
  94. String patt = m.getDeclaringClass().getName() + "." + m.getName();
  95. for (int i = 0; i < this.compiledPatterns.length; i++) {
  96. boolean matched = this.matcher.matches(patt, this.compiledPatterns[i]);
  97. if (logger.isDebugEnabled()) {
  98. logger.debug("Candidate is: '" + patt + "'; pattern is " + this.compiledPatterns[i].getPattern() +
  99. "; matched=" + matched);
  100. }
  101. if (matched) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public boolean matches(Class clazz) {
  108. // TODO do with regexp
  109. return true;
  110. }
  111. public ClassFilter getClassFilter() {
  112. return this;
  113. }
  114. }