NameMatchMethodPointcut.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  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.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. /**
  22.  * Pointcut bean for simple method name matches,
  23.  * as alternative to regexp patterns.
  24.  * Does not handle overloaded methods--that is, all methods
  25.  * with a given name will be eligible.
  26.  * @author Juergen Hoeller
  27.  * @author Rod Johnson
  28.  * @since 11.02.2004
  29.  * @see #isMatch
  30.  */
  31. public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut implements Serializable {
  32. private List mappedNames = new LinkedList();
  33. /**
  34.  * Convenience method when we have only a single method name
  35.  * to match. Use either this method or setMappedNames(), not both.
  36.  * @see #setMappedNames
  37.  */
  38. public void setMappedName(String mappedName) {
  39. setMappedNames(new String[] { mappedName });
  40. }
  41. /**
  42.  * Set the method names defining methods to match.
  43.  * Matching will be the union of all these; if any match,
  44.  * the pointcut matches.
  45.  */
  46. public void setMappedNames(String[] mappedNames) {
  47. this.mappedNames = new LinkedList();
  48. if (mappedNames != null) {
  49. for (int i = 0; i < mappedNames.length; i++) {
  50. this.mappedNames.add(mappedNames[i]);
  51. }
  52. }
  53. }
  54. /**
  55.  * Add another eligible method name, in addition
  56.  * to those already named. Like the set methods, this method is for use
  57.  * when configuring proxies, before a proxy is used.
  58.  * <br>
  59.  * <b>NB:</b> This method does not work after the proxy is in
  60.  * use, as advice chains will be cached.
  61.  * @param name name of the additional method that will match
  62.  * @return this pointcut to allow for multiple additions in one line
  63.  */
  64. public NameMatchMethodPointcut addMethodName(String name) {
  65. // TODO in a future release, consider a way of letting proxies
  66. // cause advice changed events
  67. this.mappedNames.add(name);
  68. return this;
  69. }
  70. public boolean matches(Method m, Class targetClass) {
  71. for (int i = 0; i < this.mappedNames.size(); i++) {
  72. String mappedName = (String) this.mappedNames.get(i);
  73. if (mappedName.equals(m.getName()) || isMatch(m.getName(), mappedName)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80.  * Return if the given method name matches the mapped name.
  81.  * The default implementation checks for "xxx*" and "*xxx" matches.
  82.  * Can be overridden in subclasses.
  83.  * @param methodName the method name of the class
  84.  * @param mappedName the name in the descriptor
  85.  * @return if the names match
  86.  */
  87. protected boolean isMatch(String methodName, String mappedName) {
  88. return (mappedName.endsWith("*") && methodName.startsWith(mappedName.substring(0, mappedName.length() - 1))) ||
  89. (mappedName.startsWith("*") && methodName.endsWith(mappedName.substring(1, mappedName.length())));
  90. }
  91. }