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. /**
  19. * Pointcut bean for simple method name matches,
  20. * as alternative to regexp patterns.
  21. * @author Juergen Hoeller
  22. * @since 11.02.2004
  23. * @see #isMatch
  24. */
  25. public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut {
  26. private String[] mappedNames = new String[0];
  27. /**
  28. * Convenience method when we have only a single method name
  29. * to match. Use either this method or setMappedNames(), not both.
  30. * @see #setMappedNames
  31. */
  32. public void setMappedName(String mappedName) {
  33. this.mappedNames = new String[] {mappedName};
  34. }
  35. /**
  36. * Set the method names defining methods to match.
  37. * Matching will be the union of all these; if any match,
  38. * the pointcut matches.
  39. */
  40. public void setMappedNames(String[] mappedNames) {
  41. this.mappedNames = mappedNames;
  42. }
  43. public boolean matches(Method m, Class targetClass) {
  44. for (int i = 0; i<this.mappedNames.length; i++) {
  45. String mappedName = this.mappedNames[i];
  46. if (mappedName.equals(m.getName()) || isMatch(m.getName(), mappedName)) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. /**
  53. * Return if the given method name matches the mapped name.
  54. * The default implementation checks for "xxx*" and "*xxx" matches.
  55. * Can be overridden in subclasses.
  56. * @param methodName the method name of the class
  57. * @param mappedName the name in the descriptor
  58. * @return if the names match
  59. */
  60. protected boolean isMatch(String methodName, String mappedName) {
  61. return (mappedName.endsWith("*") && methodName.startsWith(mappedName.substring(0, mappedName.length() - 1))) ||
  62. (mappedName.startsWith("*") && methodName.endsWith(mappedName.substring(1, mappedName.length())));
  63. }
  64. }