KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > metadata > SimpleClassMetaDataLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 import java.lang.reflect.Constructor JavaDoc;
37 import java.lang.reflect.Field JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * <description>
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 45997 $
47  * @see <related>
48  */

49 public class SimpleClassMetaDataLoader implements ClassMetaDataLoader
50 {
51    public final static SimpleClassMetaDataLoader singleton = new SimpleClassMetaDataLoader();
52
53    // Public --------------------------------------------------------
54
public ClassMetaDataBinding importMetaData(Element JavaDoc element, String JavaDoc name, String JavaDoc tag, String JavaDoc classExpr) throws Exception JavaDoc
55    {
56       SimpleClassMetaDataBinding data = new SimpleClassMetaDataBinding(this, name, tag, classExpr);
57       Element JavaDoc defaultElement = XmlHelper.getOptionalChild(element, "default");
58       if (defaultElement != null)
59       {
60          NodeList JavaDoc 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 JavaDoc child = (Element JavaDoc) children.item(i);
67                String JavaDoc attr = child.getTagName();
68                String JavaDoc 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 JavaDoc());
74       }
75
76       Element JavaDoc classElement = XmlHelper.getOptionalChild(element, "class");
77       if (classElement != null)
78       {
79          NodeList JavaDoc 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 JavaDoc child = (Element JavaDoc) children.item(i);
86                String JavaDoc attr = child.getTagName();
87                String JavaDoc 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 JavaDoc());
95          }
96       }
97
98       Iterator JavaDoc it = XmlHelper.getChildrenByTagName(element, "method");
99       while (it.hasNext())
100       {
101          Element JavaDoc method = (Element JavaDoc) it.next();
102          String JavaDoc methodName = method.getAttribute("name");
103          String JavaDoc 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 JavaDoc("neither a name or expr attribute has been defined");
113          if (expr != null && methodName != null) throw new RuntimeException JavaDoc("cannot specify a name and expr in same place");
114
115          if (expr == null && methodName != null)
116          {
117             expr = "* " + methodName + "(..)";
118          }
119
120          NodeList JavaDoc 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 JavaDoc child = (Element JavaDoc) children.item(i);
127                String JavaDoc attr = child.getTagName();
128                String JavaDoc val = XmlHelper.getElementContent(child, "");
129                data.queueMethodMetaData(expr, tag, attr, val);
130                hadTag = true;
131             }
132          }
133          if (!hadTag) // put in an empty tag
134
{
135             // put in an empty tag for annotations with no attributes
136
data.queueMethodMetaData(expr, tag, MetaDataResolver.EMPTY_TAG, new Object JavaDoc());
137          }
138
139       }
140       it = XmlHelper.getChildrenByTagName(element, "field");
141       while (it.hasNext())
142       {
143          Element JavaDoc field = (Element JavaDoc) it.next();
144          String JavaDoc fieldName = field.getAttribute("name");
145          NodeList JavaDoc 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 JavaDoc child = (Element JavaDoc) children.item(i);
152                String JavaDoc attr = child.getTagName();
153                String JavaDoc val = XmlHelper.getElementContent(child, "");
154                data.queueFieldMetaData(fieldName, tag, attr, val);
155                hadTag = true;
156             }
157          }
158          if (!hadTag) // put in an empty tag
159
{
160             // put in an empty tag for annotations with no attributes
161
data.queueFieldMetaData(fieldName, tag, MetaDataResolver.EMPTY_TAG, new Object JavaDoc());
162          }
163       }
164       it = XmlHelper.getChildrenByTagName(element, "constructor");
165       while (it.hasNext())
166       {
167          Element JavaDoc field = (Element JavaDoc) it.next();
168          String JavaDoc expr = field.getAttribute("expr");
169          NodeList JavaDoc 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 JavaDoc child = (Element JavaDoc) children.item(i);
176                String JavaDoc attr = child.getTagName();
177                String JavaDoc val = XmlHelper.getElementContent(child, "");
178                data.queueConstructorMetaData(expr, tag, attr, val);
179                hadTag = true;
180             }
181          }
182          if (!hadTag) // put in an empty tag
183
{
184             // put in an empty tag for annotations with no attributes
185
data.queueConstructorMetaData(expr, tag, MetaDataResolver.EMPTY_TAG, new Object JavaDoc());
186          }
187       }
188       return data;
189    }
190
191    public void bind(Advisor advisor, ClassMetaDataBinding data, Method[] methods, Field[] fields, Constructor[] constructors) throws Exception JavaDoc
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          //Class has not yet been attached, so method tables etc. will be empty, no point in going on
223
//TODO: move into bind()?
224
return;
225       }
226       
227       boolean exactMatch = data.matches(advisor, advisor.getClazz());
228       ArrayList JavaDoc 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                   //old skool weaving doesn't support metadata overriding, so only do this extra work for generated advisors
240
//exactMatch = data.exactlyMatchesAdvisor(advisor);
241
if (!exactMatch)
242                   {
243                      //Only add if the method is inherited and we had no exact match on the advisor already
244
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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