1 16 17 package org.springframework.aop.support; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.Serializable ; 22 import java.lang.reflect.Method ; 23 import java.util.Arrays ; 24 25 import org.springframework.util.Assert; 26 import org.springframework.util.ObjectUtils; 27 import org.springframework.util.StringUtils; 28 29 53 public abstract class AbstractRegexpMethodPointcut extends StaticMethodMatcherPointcut 54 implements Serializable { 55 56 57 private String [] patterns = new String [0]; 58 59 60 private String [] excludedPatterns = new String [0]; 61 62 63 68 public void setPattern(String pattern) { 69 setPatterns(new String [] {pattern}); 70 } 71 72 77 public void setPatterns(String [] patterns) { 78 Assert.notEmpty(patterns, "'patterns' must not be empty"); 79 this.patterns = new String [patterns.length]; 80 for (int i = 0; i < patterns.length; i++) { 81 this.patterns[i] = StringUtils.trimWhitespace(patterns[i]); 82 } 83 initPatternRepresentation(this.patterns); 84 } 85 86 89 public String [] getPatterns() { 90 return this.patterns; 91 } 92 93 98 public void setExcludedPattern(String excludedPattern) { 99 setExcludedPatterns(new String [] {excludedPattern}); 100 } 101 102 107 public void setExcludedPatterns(String [] excludedPatterns) { 108 Assert.notEmpty(excludedPatterns, "'excludedPatterns' must not be empty"); 109 this.excludedPatterns = new String [excludedPatterns.length]; 110 for (int i = 0; i < excludedPatterns.length; i++) { 111 this.excludedPatterns[i] = StringUtils.trimWhitespace(excludedPatterns[i]); 112 } 113 initExcludedPatternRepresentation(this.excludedPatterns); 114 } 115 116 119 public String [] getExcludedPatterns() { 120 return this.excludedPatterns; 121 } 122 123 124 132 public final boolean matches(Method method, Class targetClass) { 133 String patt = method.getDeclaringClass().getName() + "." + method.getName(); 135 for (int i = 0; i < this.patterns.length; i++) { 136 boolean matched = matches(patt, i); 137 if (matched) { 138 for (int j = 0; j < this.excludedPatterns.length; j++) { 139 boolean excluded = matchesExclusion(patt, j); 140 if(excluded) { 141 return false; 142 } 143 } 144 return true; 145 } 146 } 147 return false; 148 } 149 150 151 159 protected abstract void initPatternRepresentation(String [] patterns) throws IllegalArgumentException ; 160 161 protected abstract void initExcludedPatternRepresentation(String [] excludedPatterns) throws IllegalArgumentException ; 162 163 169 protected abstract boolean matches(String pattern, int patternIndex); 170 171 177 protected abstract boolean matchesExclusion(String pattern, int patternIndex); 178 179 180 public boolean equals(Object other) { 181 if (this == other) { 182 return true; 183 } 184 if (!(other instanceof AbstractRegexpMethodPointcut)) { 185 return false; 186 } 187 AbstractRegexpMethodPointcut otherPointcut = (AbstractRegexpMethodPointcut) other; 188 return (Arrays.equals(this.patterns, otherPointcut.patterns) && 189 Arrays.equals(this.excludedPatterns, otherPointcut.excludedPatterns)); 190 } 191 192 public int hashCode() { 193 int result = 27; 194 for (int i = 0; i < this.patterns.length; i++) { 195 String pattern = this.patterns[i]; 196 result = 13 * result + pattern.hashCode(); 197 } 198 for (int i = 0; i < this.excludedPatterns.length; i++) { 199 String excludedPattern = this.excludedPatterns[i]; 200 result = 13 * result + excludedPattern.hashCode(); 201 } 202 return result; 203 } 204 205 public String toString() { 206 return getClass().getName() + ": patterns " + ObjectUtils.nullSafeToString(this.patterns) + 207 ", excluded patterns " + ObjectUtils.nullSafeToString(this.excludedPatterns); 208 } 209 210 211 215 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 216 ois.defaultReadObject(); 218 219 initPatternRepresentation(this.patterns); 221 initExcludedPatternRepresentation(this.excludedPatterns); 222 } 223 224 } 225 | Popular Tags |