1 22 package org.jboss.aop.metadata; 23 24 import javassist.CtConstructor; 25 import javassist.CtField; 26 import javassist.CtMethod; 27 import javassist.NotFoundException; 28 import org.jboss.aop.Advisor; 29 import org.jboss.aop.ClassAdvisor; 30 import org.jboss.aop.expressions.ConstructorExpression; 31 import org.jboss.aop.util.XmlHelper; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 36 import java.lang.reflect.Constructor ; 37 import java.lang.reflect.Field ; 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 42 49 public class SimpleClassMetaDataLoader implements ClassMetaDataLoader 50 { 51 public final static SimpleClassMetaDataLoader singleton = new SimpleClassMetaDataLoader(); 52 53 public ClassMetaDataBinding importMetaData(Element element, String name, String tag, String classExpr) throws Exception 55 { 56 SimpleClassMetaDataBinding data = new SimpleClassMetaDataBinding(this, name, tag, classExpr); 57 Element defaultElement = XmlHelper.getOptionalChild(element, "default"); 58 if (defaultElement != null) 59 { 60 NodeList children = defaultElement.getChildNodes(); 61 boolean hasTag = false; 62 for (int i = 0; i < children.getLength(); i++) 63 { 64 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 65 { 66 Element child = (Element ) children.item(i); 67 String attr = child.getTagName(); 68 String val = XmlHelper.getElementContent(child, ""); 69 data.addDefaultMetaData(tag, attr, val); 70 hasTag = true; 71 } 72 } 73 if (!hasTag) data.addDefaultMetaData(tag, MetaDataResolver.EMPTY_TAG, new Object ()); 74 } 75 76 Element classElement = XmlHelper.getOptionalChild(element, "class"); 77 if (classElement != null) 78 { 79 NodeList children = classElement.getChildNodes(); 80 boolean hasTag = false; 81 for (int i = 0; i < children.getLength(); i++) 82 { 83 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 84 { 85 Element child = (Element ) children.item(i); 86 String attr = child.getTagName(); 87 String val = XmlHelper.getElementContent(child, ""); 88 data.addClassMetaData(tag, attr, val); 89 hasTag = true; 90 } 91 } 92 if (!hasTag) 93 { 94 data.addClassMetaData(tag, MetaDataResolver.EMPTY_TAG, new Object ()); 95 } 96 } 97 98 Iterator it = XmlHelper.getChildrenByTagName(element, "method"); 99 while (it.hasNext()) 100 { 101 Element method = (Element ) it.next(); 102 String methodName = method.getAttribute("name"); 103 String expr = method.getAttribute("expr"); 104 if (methodName != null && methodName.equals("")) 105 { 106 methodName = null; 107 } 108 if (expr != null && expr.equals("")) 109 { 110 expr = null; 111 } 112 if (expr == null && methodName == null) throw new RuntimeException ("neither a name or expr attribute has been defined"); 113 if (expr != null && methodName != null) throw new RuntimeException ("cannot specify a name and expr in same place"); 114 115 if (expr == null && methodName != null) 116 { 117 expr = "* " + methodName + "(..)"; 118 } 119 120 NodeList children = method.getChildNodes(); 121 boolean hadTag = false; 122 for (int i = 0; i < children.getLength(); i++) 123 { 124 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 125 { 126 Element child = (Element ) children.item(i); 127 String attr = child.getTagName(); 128 String val = XmlHelper.getElementContent(child, ""); 129 data.queueMethodMetaData(expr, tag, attr, val); 130 hadTag = true; 131 } 132 } 133 if (!hadTag) { 135 data.queueMethodMetaData(expr, tag, MetaDataResolver.EMPTY_TAG, new Object ()); 137 } 138 139 } 140 it = XmlHelper.getChildrenByTagName(element, "field"); 141 while (it.hasNext()) 142 { 143 Element field = (Element ) it.next(); 144 String fieldName = field.getAttribute("name"); 145 NodeList children = field.getChildNodes(); 146 boolean hadTag = false; 147 for (int i = 0; i < children.getLength(); i++) 148 { 149 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 150 { 151 Element child = (Element ) children.item(i); 152 String attr = child.getTagName(); 153 String val = XmlHelper.getElementContent(child, ""); 154 data.queueFieldMetaData(fieldName, tag, attr, val); 155 hadTag = true; 156 } 157 } 158 if (!hadTag) { 160 data.queueFieldMetaData(fieldName, tag, MetaDataResolver.EMPTY_TAG, new Object ()); 162 } 163 } 164 it = XmlHelper.getChildrenByTagName(element, "constructor"); 165 while (it.hasNext()) 166 { 167 Element field = (Element ) it.next(); 168 String expr = field.getAttribute("expr"); 169 NodeList children = field.getChildNodes(); 170 boolean hadTag = false; 171 for (int i = 0; i < children.getLength(); i++) 172 { 173 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 174 { 175 Element child = (Element ) children.item(i); 176 String attr = child.getTagName(); 177 String val = XmlHelper.getElementContent(child, ""); 178 data.queueConstructorMetaData(expr, tag, attr, val); 179 hadTag = true; 180 } 181 } 182 if (!hadTag) { 184 data.queueConstructorMetaData(expr, tag, MetaDataResolver.EMPTY_TAG, new Object ()); 186 } 187 } 188 return data; 189 } 190 191 public void bind(Advisor advisor, ClassMetaDataBinding data, Method[] methods, Field[] fields, Constructor[] constructors) throws Exception 192 { 193 SimpleClassMetaDataBinding meta = (SimpleClassMetaDataBinding) data; 194 SimpleMetaData defaultData = advisor.getDefaultMetaData(); 195 defaultData.mergeIn(meta.getDefaultMetaData()); 196 SimpleMetaData classData = advisor.getClassMetaData(); 197 classData.mergeIn(meta.getClassMetaData()); 198 199 200 if (methods != null) 201 { 202 MethodMetaData methodData = advisor.getMethodMetaData(); 203 bindMethodMetaData(advisor, meta, methodData, methods); 204 } 205 206 if (fields != null) 207 { 208 FieldMetaData fieldData = advisor.getFieldMetaData(); 209 bindFieldMetaData(meta, fieldData, fields); 210 } 211 if (constructors != null) 212 { 213 ConstructorMetaData condata = advisor.getConstructorMetaData(); 214 bindConstructorMetaData(meta, condata, constructors); 215 } 216 } 217 218 protected void bindMethodMetaData(Advisor advisor, SimpleClassMetaDataBinding data, MethodMetaData methodMetaData, Method[] advisedMethods) 219 { 220 if (advisor.getClazz() == null) 221 { 222 return; 225 } 226 227 boolean exactMatch = data.matches(advisor, advisor.getClazz()); 228 ArrayList queuedData = data.getQueuedMethodMetaData(); 229 for (int i = 0; i < queuedData.size(); i++) 230 { 231 SimpleClassMetaDataBinding.QueuedMethodMetaData queued = (SimpleClassMetaDataBinding.QueuedMethodMetaData) queuedData.get(i); 232 for (int j = 0; j < advisedMethods.length; j++) 233 { 234 if (queued.expr.matches(advisedMethods[j])) 235 { 236 boolean add = true; 237 if (advisor.chainOverridingForInheritedMethods()) 238 { 239 if (!exactMatch) 242 { 243 add = (advisor.getClazz() != advisedMethods[j].getDeclaringClass()); 245 246 if (add) 247 { 248 if (methodMetaData.getMethodMetaData(advisedMethods[j], queued.tag, queued.attr) != null) 249 { 250 add = methodMetaData.tagWasMatchedInexactly(advisedMethods[j], queued.tag, queued.attr); 251 } 252 } 253 } 254 } 255 256 if (add) 257 { 258 methodMetaData.addMethodMetaData(advisedMethods[j], queued.tag, queued.attr, queued.value, exactMatch); 259 } 260 } 261 } 262 } 263 } 264 265 protected void bindFieldMetaData(SimpleClassMetaDataBinding data, FieldMetaData fieldMetaData, Field[] advisedFields) 266 { 267 ArrayList queuedData = data.getQueuedFieldMetaData(); 268 for (int i = 0; i < queuedData.size(); i++) 269 { 270 SimpleClassMetaDataBinding.QueuedMetaData queued = (SimpleClassMetaDataBinding.QueuedMetaData) queuedData.get(i); 271 for (int j = 0; j < advisedFields.length; j++) 272 { 273 String fieldName = advisedFields[j].getName(); 274 if (queued.matches(fieldName)) 275 { 276 fieldMetaData.addFieldMetaData(advisedFields[j], queued.tag, queued.attr, queued.value); 277 } 278 } 279 } 280 } 281 282 protected void bindConstructorMetaData(SimpleClassMetaDataBinding data, ConstructorMetaData conMetaData, Constructor[] advisedCons) 283 { 284 ArrayList queuedData = data.getQueuedConstructorMetaData(); 285 for (int i = 0; i < queuedData.size(); i++) 286 { 287 SimpleClassMetaDataBinding.QueuedConstructorMetaData queued = (SimpleClassMetaDataBinding.QueuedConstructorMetaData) queuedData.get(i); 288 ConstructorExpression expression = queued.expr; 289 for (int j = 0; j < advisedCons.length; j++) 290 { 291 if (expression.matchParamsOnly(advisedCons[j])) 292 { 293 conMetaData.addConstructorMetaData(advisedCons[j], queued.tag, queued.attr, queued.value); 294 } 295 } 296 } 297 } 298 299 public void bind(Advisor advisor, ClassMetaDataBinding data, CtMethod[] methods, CtField[] fields, CtConstructor[] constructors) throws Exception 300 { 301 SimpleClassMetaDataBinding meta = (SimpleClassMetaDataBinding) data; 302 SimpleMetaData defaultData = advisor.getDefaultMetaData(); 303 defaultData.mergeIn(meta.getDefaultMetaData()); 304 SimpleMetaData classData = advisor.getClassMetaData(); 305 classData.mergeIn(meta.getClassMetaData()); 306 if (methods != null) 307 { 308 MethodMetaData methodData = advisor.getMethodMetaData(); 309 bindMethodMetaData(meta, methodData, methods); 310 } 311 312 if (fields != null) 313 { 314 FieldMetaData fieldData = advisor.getFieldMetaData(); 315 bindFieldMetaData(meta, fieldData, fields); 316 } 317 if (constructors != null) 318 { 319 ConstructorMetaData condata = advisor.getConstructorMetaData(); 320 bindConstructorMetaData(meta, condata, constructors); 321 } 322 } 323 324 325 protected void bindMethodMetaData(SimpleClassMetaDataBinding data, MethodMetaData methodMetaData, CtMethod[] advisedMethods) throws NotFoundException 326 { 327 ArrayList queuedData = data.getQueuedMethodMetaData(); 328 for (int i = 0; i < queuedData.size(); i++) 329 { 330 SimpleClassMetaDataBinding.QueuedMethodMetaData queued = (SimpleClassMetaDataBinding.QueuedMethodMetaData) queuedData.get(i); 331 for (int j = 0; j < advisedMethods.length; j++) 332 { 333 if (queued.expr.matches(advisedMethods[j])) 334 { 335 methodMetaData.addMethodMetaData(advisedMethods[j], queued.tag, queued.attr, queued.value); 336 } 337 } 338 } 339 } 340 341 protected void bindFieldMetaData(SimpleClassMetaDataBinding data, FieldMetaData fieldMetaData, CtField[] advisedFields) 342 { 343 ArrayList queuedData = data.getQueuedFieldMetaData(); 344 for (int i = 0; i < queuedData.size(); i++) 345 { 346 SimpleClassMetaDataBinding.QueuedMetaData queued = (SimpleClassMetaDataBinding.QueuedMetaData) queuedData.get(i); 347 for (int j = 0; j < advisedFields.length; j++) 348 { 349 String fieldName = advisedFields[j].getName(); 350 if (queued.matches(fieldName)) 351 { 352 fieldMetaData.addFieldMetaData(advisedFields[j].getName(), queued.tag, queued.attr, queued.value); 353 } 354 } 355 } 356 } 357 358 protected void bindConstructorMetaData(SimpleClassMetaDataBinding data, ConstructorMetaData conMetaData, CtConstructor[] advisedCons) throws NotFoundException 359 { 360 ArrayList queuedData = data.getQueuedConstructorMetaData(); 361 for (int i = 0; i < queuedData.size(); i++) 362 { 363 SimpleClassMetaDataBinding.QueuedConstructorMetaData queued = (SimpleClassMetaDataBinding.QueuedConstructorMetaData) queuedData.get(i); 364 ConstructorExpression expression = queued.expr; 365 for (int j = 0; j < advisedCons.length; j++) 366 { 367 if (expression.matchParamsOnly(advisedCons[j])) 368 { 369 conMetaData.addConstructorMetaData(advisedCons[j], queued.tag, queued.attr, queued.value); 370 } 371 } 372 } 373 } 374 375 } 376 | Popular Tags |