1 22 package org.jboss.aop.expressions; 23 24 import javassist.CtClass; 25 26 import java.util.ArrayList ; 27 import java.util.StringTokenizer ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 32 39 public class ParameterExpression 40 { 41 protected static final Pattern ALL; 42 protected static final Pattern PARAMS; 43 44 static 45 { 46 ALL = Pattern.compile("\\s*\\.\\.\\s*"); 47 PARAMS = Pattern.compile("\\s*(.*)\\s*"); 48 } 49 50 51 public String originalExpression; 52 public Pattern [] parameterPatterns; 53 public boolean isAll = false; 54 55 protected static String simpleType(Class type) 56 { 57 Class ret = type; 58 if (ret.isArray()) 59 { 60 Class arr = ret; 61 String array = ""; 62 while (arr.isArray()) 63 { 64 array += "[]"; 65 arr = arr.getComponentType(); 66 } 67 return arr.getName() + array; 68 } 69 return ret.getName(); 70 } 71 75 public ParameterExpression(String expression) 76 { 77 originalExpression = expression; 78 Matcher all = ALL.matcher(expression); 79 isAll = all.matches(); 80 if (!isAll) 81 { 82 Matcher params = PARAMS.matcher(expression); 83 if (!params.matches()) 84 { 85 throw new RuntimeException ("Illegal parameter list expression: " + expression); 86 } 87 StringTokenizer tokenizer = new StringTokenizer (expression, ","); 88 ArrayList paramList = new ArrayList (); 89 while (tokenizer.hasMoreTokens()) 90 { 91 String token = tokenizer.nextToken().trim(); 92 token = token.replaceAll("\\.", "\\\\."); 93 token = token.replaceAll("\\*", ".*"); 94 token = token.replaceAll("\\[", "\\\\["); 95 token = token.replaceAll("\\]", "\\\\]"); 96 paramList.add(Pattern.compile(token)); 97 } 98 parameterPatterns = (Pattern [])paramList.toArray(new Pattern [paramList.size()]); 99 } 100 } 101 102 public boolean matches(Class [] params) 103 { 104 if (isAll) 105 { 106 return true; 107 } 108 109 if (params.length != parameterPatterns.length) return false; 110 111 for (int i = 0; i < params.length; i++) 112 { 113 String asString = simpleType(params[i]); 114 Matcher m = parameterPatterns[i].matcher(asString); 115 if (!m.matches()) return false; 116 } 117 return true; 118 } 119 120 public boolean matches(CtClass[] params) 121 { 122 if (isAll) return true; 123 124 if (params.length != parameterPatterns.length) return false; 125 126 for (int i = 0; i < params.length; i++) 127 { 128 String asString = params[i].getName(); 129 Matcher m = parameterPatterns[i].matcher(asString); 130 if (!m.matches()) return false; 131 } 132 return true; 133 } 134 } 135 | Popular Tags |