1 18 19 package org.objectweb.jac.core; 20 21 22 import java.io.Serializable ; 23 import java.util.*; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.core.rtti.*; 26 import org.objectweb.jac.util.*; 27 28 34 35 public abstract class Pointcut implements Serializable { 36 static Logger logger = Logger.getLogger("pointcut"); 37 static Logger loggerTags = Logger.getLogger("tags"); 38 static Logger loggerKeywords = Logger.getLogger("pointcut.keywords"); 39 40 45 public abstract void applyTo(Wrappee wrappee, ClassItem cl); 46 47 51 protected abstract String parseKeyword(Wrappee wrappee, 52 ClassItem cl, 53 String keywordExpr, 54 List parameters); 55 56 64 protected List replaceTags(List parameters,ClassItem cli) { 65 if (cli==null || parameters==null) 66 return null; 67 Vector result = new Vector(); 68 loggerTags.debug("check "+parameters+" on "+cli.getName()); 69 Iterator it = parameters.iterator(); 70 while (it.hasNext()) { 71 String param = (String )it.next(); 72 if (param.startsWith("<") && param.endsWith(">")) { 73 Collection taggedMembers; 74 if (param.charAt(1)=='!') 75 taggedMembers = cli.getTaggedFields( 76 param.substring(2,param.length()-1),true); 77 else 78 taggedMembers = cli.getTaggedFields( 79 param.substring(1,param.length()-1),false); 80 result.addAll(taggedMembers); 81 88 } else if (param.startsWith("{") && param.endsWith("}")) { 89 result.addAll(cli.filterFields(param.substring(1,param.length()-1))); 90 } else { 91 result.add(cli.getMember(param)); 92 } 93 } 94 loggerTags.debug(" result="+result); 95 return result; 96 } 97 98 108 protected void parseExpr(String descr, Wrappee wrappee, ClassItem cl, 109 String expr, String [] keywords, 110 Vector result, Vector inv) { 111 112 result.clear(); 113 inv.clear(); 114 expr = Strings.replace(expr, " || ", "\\|"); 115 int pos = skipSpaces(expr,0); 116 boolean end = false; 117 118 if (expr.charAt(0) == '!') { 119 pos = skipSpaces(expr,pos+1); 120 inv.add(Boolean.FALSE); 121 } else { 122 inv.add(Boolean.TRUE); 123 } 124 if (pos==-1) 125 throw new RuntimeException ("Invalid expression: \""+expr+"\""); 126 while (!end) { 127 int newpos = expr.indexOf("&&", pos); 128 try { 129 if (newpos != -1) { 130 result.add(replaceKeywords( 131 wrappee,cl,expr.substring(pos,newpos).trim(),keywords)); 132 pos = skipSpaces(expr,newpos + 2); 133 if (pos==-1) 134 throw new RuntimeException ("Invalid expression: \""+expr+"\""); 135 if (expr.charAt(pos) == '!') { 136 pos = skipSpaces(expr,pos+1); 137 if (pos==-1) 138 throw new RuntimeException ("Invalid expression: \""+expr+"\""); 139 inv.add(Boolean.FALSE); 140 } else { 141 inv.add(Boolean.TRUE); 142 } 143 } else { 144 result.add( 145 replaceKeywords(wrappee,cl,expr.substring(pos).trim(),keywords)); 146 end = true; 147 } 148 } catch (Exception e) { 149 logger.error("Invalid pointcut definition, "+descr+ 150 " construction failed at position "+pos+": "+e); 151 } 152 } 153 } 154 155 162 static int skipSpaces(String str, int pos) { 163 while (pos<str.length()) { 164 if (!Character.isWhitespace(str.charAt(pos))) 165 return pos; 166 pos++; 167 } 168 return -1; 169 } 170 171 172 String replaceKeywords(Wrappee wrappee, ClassItem cl, 173 String expr, String [] keywords) { 174 String newExpr = expr; 175 for(int i=0; i<keywords.length; i++) { 176 newExpr = replaceKeyword(wrappee, cl, newExpr, keywords[i]); 177 } 178 return newExpr; 179 } 180 181 187 List parseParameters(String params, ClassItem cli) { 188 if (params.equals("") || params.charAt(0) != '(') 189 return null; 190 196 return replaceTags( 197 Strings.splitToList(params.substring(1, params.indexOf(')',0)),",") 198 ,cli); 199 } 200 201 int parametersLength(String params) { 202 if (params.equals( "" )) 203 return 0; 204 if (params.charAt(0) != '(') 205 return 0; 206 return params.indexOf(')',0)+1; 207 } 208 209 210 String replaceKeyword(Wrappee wrappee, ClassItem cl, 211 String expr, String keyword) { 212 213 int pos = 0; 214 boolean end = false; 215 StringBuffer newExpr = new StringBuffer (); 216 int keyLen = keyword.length(); 217 218 while (!end) { 219 int newpos = expr.indexOf(keyword, pos); 220 try { 221 if (newpos != -1) { 222 loggerKeywords.debug("replacing keyword '"+keyword+ 223 "' in expr :"+expr); 224 newExpr.append(expr.substring(pos, newpos - pos)); 225 newExpr.append( 226 parseKeyword( 227 wrappee, cl, keyword, 228 parseParameters( 229 expr.substring(newpos+keyword.length()),cl))); 230 pos = newpos + keyLen + 231 parametersLength(expr.substring(newpos+keyLen)); 232 } else { 233 newExpr.append(expr.substring(pos)); 234 end = true; 235 } 236 } catch (Exception e) { 237 e.printStackTrace(); 238 } 240 } 241 return newExpr.toString(); 242 } 243 244 } 245 | Popular Tags |