1 4 package com.tc.aspectwerkz.expression.regexp; 5 6 7 import com.tc.aspectwerkz.proxy.ProxyDelegationStrategy; 8 import com.tc.aspectwerkz.proxy.ProxySubclassingStrategy; 9 import com.tc.aspectwerkz.util.Strings; 10 import com.tc.aspectwerkz.expression.SubtypePatternType; 11 import com.tc.aspectwerkz.expression.ExpressionException; 12 import com.tc.aspectwerkz.reflect.ClassInfo; 13 14 import java.io.ObjectInputStream ; 15 16 21 public class TypePattern extends Pattern { 22 23 26 protected transient com.tc.jrexx.regex.Pattern m_typeNamePattern; 27 28 31 protected String m_pattern; 32 33 36 private SubtypePatternType m_subtypePatternType; 37 38 44 TypePattern(final String pattern, final SubtypePatternType subtypePatternType) { 45 m_pattern = pattern; 46 m_subtypePatternType = subtypePatternType; 47 escape(m_pattern); 48 } 49 50 56 public boolean matches(String typeName) { 57 if (m_typeNamePattern.contains(typeName)) { 59 return true; 60 } 61 62 int awProxySuffixStart1 = typeName.indexOf(ProxySubclassingStrategy.PROXY_SUFFIX); 64 int awProxySuffixStart2 = typeName.indexOf(ProxyDelegationStrategy.PROXY_SUFFIX); 65 if (awProxySuffixStart1 > 0) { 66 typeName = typeName.substring(0, awProxySuffixStart1); 67 } else if (awProxySuffixStart2 > 0) { 68 typeName = typeName.substring(0, awProxySuffixStart2); 69 } else { 70 int cglibFastClassSuffixStarg = typeName.indexOf("$$FastClassByCGLIB$$"); 71 if (cglibFastClassSuffixStarg > 0) { 72 return false; 74 } 75 int cglibEnhancerSuffixStart = typeName.indexOf("$$EnhancerByCGLIB$$"); 76 if (cglibEnhancerSuffixStart > 0) { 77 typeName = typeName.substring(0, cglibEnhancerSuffixStart); 78 } 79 } 80 if (typeName == null) { 81 return false; 82 } 83 if (typeName.equals("")) { 84 return false; 85 } 86 return m_typeNamePattern.contains(typeName); 87 } 88 89 95 public boolean matchType(final ClassInfo classInfo) { 96 SubtypePatternType type = getSubtypePatternType(); 97 if (type.equals(SubtypePatternType.MATCH_ON_ALL_METHODS)) { 98 return matchSuperClasses(classInfo); 99 } else if (type.equals(SubtypePatternType.MATCH_ON_BASE_TYPE_METHODS_ONLY)) { 100 return matchSuperClasses(classInfo); 103 } else { 104 return matches(classInfo.getName()); 105 } 106 } 107 108 115 public boolean matchSuperClasses(final ClassInfo classInfo) { 116 if ((classInfo == null)) { 117 return false; 118 } 119 120 if (matches(classInfo.getName())) { 122 return true; 123 } else { 124 if (matchInterfaces(classInfo.getInterfaces(), classInfo)) { 126 return true; 127 } 128 129 return matchSuperClasses(classInfo.getSuperclass()); 131 } 132 } 133 134 142 public boolean matchInterfaces(final ClassInfo[] interfaces, final ClassInfo classInfo) { 143 if ((interfaces.length == 0) || (classInfo == null)) { 144 return false; 145 } 146 for (int i = 0; i < interfaces.length; i++) { 147 ClassInfo anInterface = interfaces[i]; 148 if (matches(anInterface.getName())) { 149 return true; 150 } else { 151 if (matchInterfaces(anInterface.getInterfaces(), classInfo)) { 152 return true; 153 } else { 154 continue; 155 } 156 } 157 } 158 return false; 159 } 160 161 166 public SubtypePatternType getSubtypePatternType() { 167 return m_subtypePatternType; 168 } 169 170 175 public boolean isEagerWildCard() { 176 return m_pattern.equals(EAGER_WILDCARD); 177 } 178 179 184 public String getPattern() { 185 return m_pattern; 186 } 187 188 193 protected void escape(final String pattern) { 194 String typeName = pattern; 195 if (ABBREVIATIONS.containsKey(pattern)) { 196 typeName = (String ) ABBREVIATIONS.get(pattern); 197 } 198 try { 199 if (typeName.equals(REGULAR_WILDCARD) || typeName.equals(EAGER_WILDCARD)) { 200 typeName = "[a-zA-Z0-9_$.\\[\\]]+"; 201 } else { 202 typeName = Strings.replaceSubString(typeName, "[", "\\["); 204 typeName = Strings.replaceSubString(typeName, "]", "\\]"); 205 typeName = Strings.replaceSubString(typeName, "..", "[a-zA-Z0-9_$.]+"); 206 typeName = Strings.replaceSubString(typeName, ".", "\\."); 207 typeName = Strings.replaceSubString(typeName, "*", "[a-zA-Z0-9_$\\[\\]]*"); 208 } 209 m_typeNamePattern = new com.tc.jrexx.regex.Pattern(typeName); 210 } catch (Throwable e) { 211 e.printStackTrace(); 212 throw new ExpressionException("type pattern is not well formed: " + pattern, e); 213 } 214 } 215 216 222 private void readObject(final ObjectInputStream stream) throws Exception { 223 ObjectInputStream.GetField fields = stream.readFields(); 224 m_pattern = (String ) fields.get("m_pattern", null); 225 escape(m_pattern); 226 } 227 228 public int hashCode() { 229 int result = 17; 230 result = (37 * result) + hashCodeOrZeroIfNull(m_pattern); 231 result = (37 * result) + hashCodeOrZeroIfNull(m_typeNamePattern); 232 return result; 233 } 234 235 protected static int hashCodeOrZeroIfNull(final Object o) { 236 if (null == o) { 237 return 19; 238 } 239 return o.hashCode(); 240 } 241 242 public boolean equals(final Object o) { 243 if (this == o) { 244 return true; 245 } 246 if (!(o instanceof TypePattern)) { 247 return false; 248 } 249 final TypePattern obj = (TypePattern) o; 250 return areEqualsOrBothNull(obj.m_pattern, this.m_pattern) 251 && areEqualsOrBothNull(obj.m_typeNamePattern, this.m_typeNamePattern); 252 } 253 254 protected static boolean areEqualsOrBothNull(final Object o1, final Object o2) { 255 if (null == o1) { 256 return (null == o2); 257 } 258 return o1.equals(o2); 259 } 260 } | Popular Tags |