1 22 package org.jboss.aop.expressions; 23 24 import javassist.CtMethod; 25 import javassist.NotFoundException; 26 27 import java.lang.reflect.Method ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 38 public class MethodExpression 39 { 40 protected static final Pattern PATTERN = Pattern.compile("(.*)\\s+(.*)\\((.*)\\)"); 41 public String originalExpression; 42 public ParameterExpression params; 43 public Pattern methodNameExpr; 44 public String methodName; 45 public String returnString; 46 public Pattern returnExpr; 47 48 public MethodExpression(String ex) 49 { 50 originalExpression = ex; 51 Matcher m = PATTERN.matcher(ex); 52 if (!m.matches()) throw new IllegalStateException ("MethodExpression is invalid: " + ex); 53 returnString = m.group(1); 54 returnString = returnString.replaceAll("\\.", "\\\\."); 55 returnString = returnString.replaceAll("\\*", ".*"); 56 returnString = returnString.replaceAll("\\[", "\\\\["); 57 returnString = returnString.replaceAll("\\]", "\\\\]"); 58 returnExpr = Pattern.compile(returnString); 59 methodName = m.group(2); 60 methodName = methodName.replaceAll("\\.", "\\\\."); 61 methodName = methodName.replaceAll("\\*", ".*"); 62 methodName = methodName.replaceAll("\\[", "\\\\["); 63 methodName = methodName.replaceAll("\\]", "\\\\]"); 64 methodNameExpr = Pattern.compile(methodName); 65 params = new ParameterExpression(m.group(3)); 66 } 67 68 69 public boolean matches(Method method) 70 { 71 String returnType = ParameterExpression.simpleType(method.getReturnType()); 72 Matcher ret = returnExpr.matcher(returnType); 73 if (!ret.matches()) return false; 74 75 Matcher cm = methodNameExpr.matcher(method.getName()); 76 if (cm.matches() == false) return false; 77 78 return params.matches(method.getParameterTypes()); 79 } 80 81 public boolean matches(CtMethod method) throws NotFoundException 82 { 83 String returnType = method.getReturnType().getName(); 84 Matcher ret = returnExpr.matcher(returnType); 85 if (!ret.matches()) return false; 86 87 Matcher cm = methodNameExpr.matcher(method.getName()); 88 if (cm.matches() == false) return false; 89 90 return params.matches(method.getParameterTypes()); 91 } 92 93 94 } 95 | Popular Tags |