1 22 package org.jboss.aop.pointcut; 23 24 import java.lang.reflect.Field ; 25 import org.jboss.aop.Advisor; 26 import org.jboss.aop.pointcut.ast.ASTAll; 27 import org.jboss.aop.pointcut.ast.ASTAttribute; 28 import org.jboss.aop.pointcut.ast.ASTConstructor; 29 import org.jboss.aop.pointcut.ast.ASTField; 30 import org.jboss.aop.pointcut.ast.ASTFieldExecution; 31 import org.jboss.aop.pointcut.ast.ASTHas; 32 import org.jboss.aop.pointcut.ast.ASTHasField; 33 import org.jboss.aop.pointcut.ast.ASTMethod; 34 import org.jboss.aop.pointcut.ast.ASTStart; 35 import org.jboss.aop.pointcut.ast.ClassExpression; 36 import org.jboss.aop.pointcut.ast.Node; 37 import javassist.CtField; 38 import javassist.NotFoundException; 39 40 46 public class FieldMatcher extends MatcherHelper 47 { 48 protected Advisor advisor; 49 protected String classname; 50 protected String fieldName; 51 protected int fieldModifiers; 52 protected CtField ctField; 53 protected Field refField; 54 55 public FieldMatcher(Advisor advisor, CtField field, ASTStart start) throws NotFoundException 56 { 57 super(start, advisor.getManager()); 58 this.advisor = advisor; 59 this.classname = field.getDeclaringClass().getName(); 60 this.start = start; 61 this.fieldName = field.getName(); 62 this.fieldModifiers = field.getModifiers(); 63 this.ctField = field; 64 } 65 66 public FieldMatcher(Advisor advisor, Field field, ASTStart start) 67 { 68 super(start, advisor.getManager()); 69 this.advisor = advisor; 70 this.classname = field.getDeclaringClass().getName(); 71 this.start = start; 72 this.fieldName = field.getName(); 73 this.fieldModifiers = field.getModifiers(); 74 this.refField = field; 75 } 76 77 protected Boolean resolvePointcut(Pointcut p) 78 { 79 throw new RuntimeException ("SHOULD NOT BE CALLED"); 80 } 81 82 public Object visit(ASTField node, Object data) 83 { 84 85 if (node.getAttributes().size() > 0) 86 { 87 for (int i = 0; i < node.getAttributes().size(); i++) 88 { 89 ASTAttribute attr = (ASTAttribute) node.getAttributes().get(i); 90 if (!Util.matchModifiers(attr, fieldModifiers)) 91 { 92 return Boolean.FALSE; 93 } 94 } 95 } 96 97 try 98 { 99 ClassExpression type = node.getType(); 100 if (ctField != null) 101 { 102 if (!Util.matchesClassExpr(type, ctField.getType(), advisor)) return Boolean.FALSE; 103 if (!Util.matchesClassExpr(node.getClazz(), ctField.getDeclaringClass(), advisor)) return Boolean.FALSE; 104 } 105 else 106 { 107 if (!Util.matchesClassExpr(type, refField.getType(), advisor)) return Boolean.FALSE; 108 if (!Util.matchesClassExpr(node.getClazz(), refField.getDeclaringClass(), advisor)) return Boolean.FALSE; 109 } 110 } 111 catch (NotFoundException nfe) 112 { 113 throw new RuntimeException (nfe); 114 } 115 116 if (node.getFieldIdentifier().isAnnotation()) 117 { 118 String sub = node.getFieldIdentifier().getOriginal().substring(1); 119 if (advisor.getFieldMetaData().hasTag(fieldName, sub) || advisor.getDefaultMetaData().hasTag(sub)) 120 { 121 return Boolean.TRUE; 122 } 123 try 124 { 125 if (ctField != null) 126 { 127 if (advisor.hasAnnotation(ctField, sub)) 128 { 129 return Boolean.TRUE; 130 } 131 } 132 else 133 { 134 if (advisor.hasAnnotation(refField, sub)) 135 { 136 return Boolean.TRUE; 137 } 138 } 139 } 140 catch (Exception e) 141 { 142 throw new RuntimeException (e); } 144 } 145 else 146 { 147 if (node.getFieldIdentifier().matches(fieldName)) 148 { 149 return Boolean.TRUE; 150 } 151 } 152 return Boolean.FALSE; 153 } 154 155 public Object visit(ASTFieldExecution node, Object data) 156 { 157 return node.jjtGetChild(0).jjtAccept(this, null); 158 } 159 160 public Object visit(ASTAll node, Object data) 161 { 162 if (node.getClazz().isAnnotation()) 163 { 164 String sub = node.getClazz().getOriginal().substring(1); 165 if (!advisor.getFieldMetaData().hasTag(fieldName, sub)) 166 { 167 if (!advisor.getDefaultMetaData().hasTag(sub)) 168 { 169 if (ctField != null) 170 { 171 if (!advisor.hasAnnotation(ctField, sub)) return Boolean.FALSE; 172 } 173 else 174 { 175 try 176 { 177 if (!advisor.hasAnnotation(refField, sub)) return Boolean.FALSE; 178 } 179 catch (Exception e) 180 { 181 throw new RuntimeException (e); } 183 } 184 } 185 } 186 } 187 else if (node.getClazz().isInstanceOf()) 188 { 189 if (ctField != null) 190 { 191 if (!Util.subtypeOf(ctField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE; 192 } 193 else if (!Util.subtypeOf(refField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE; 194 195 } 196 else if (node.getClazz().isTypedef()) 197 { 198 if (ctField != null) 199 { 200 try 201 { 202 if (!Util.matchesTypedef(ctField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE; 203 } 204 catch (Exception e) 205 { 206 throw new RuntimeException (e); 207 } 208 } 209 else if (!Util.matchesTypedef(refField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE; 210 } 211 else if (!node.getClazz().matches(classname)) 212 { 213 return Boolean.FALSE; 214 } 215 216 return Boolean.TRUE; 217 } 218 219 public Object visit(ASTHasField node, Object data) 220 { 221 ASTField f = (ASTField) node.jjtGetChild(0); 222 if (ctField != null) 223 return new Boolean (Util.has(ctField.getDeclaringClass(), f, advisor)); 224 else 225 return new Boolean (Util.has(refField.getDeclaringClass(), f, advisor)); 226 } 227 228 public Object visit(ASTHas node, Object data) 229 { 230 Node n = node.jjtGetChild(0); 231 if (n instanceof ASTMethod) 232 { 233 if (ctField != null) 234 { 235 return new Boolean (Util.has(ctField.getDeclaringClass(), (ASTMethod) n, advisor)); 236 } 237 else 238 { 239 return new Boolean (Util.has(refField.getDeclaringClass(), (ASTMethod) n, advisor)); 240 241 } 242 } 243 else 244 { 245 if (refField != null) 246 { 247 return new Boolean (Util.has(ctField.getDeclaringClass(), (ASTConstructor) n, advisor)); 248 } 249 else 250 { 251 return new Boolean (Util.has(refField.getDeclaringClass(), (ASTConstructor) n, advisor)); 252 253 } 254 } 255 } 256 } 257 | Popular Tags |