KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > instrument > ConstructionTransformer


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.instrument;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.jboss.aop.ClassAdvisor;
27 import org.jboss.aop.pointcut.Pointcut;
28
29 import javassist.CannotCompileException;
30 import javassist.CtClass;
31 import javassist.CtConstructor;
32 import javassist.CtField;
33 import javassist.Modifier;
34 import javassist.NotFoundException;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 44253 $
41  */

42 public abstract class ConstructionTransformer
43 {
44    final static String JavaDoc CONSTRUCTION_INFO_CLASS_NAME = "org.jboss.aop.ConstructionInfo";
45    protected Instrumentor instrumentor;
46
47    protected ConstructionTransformer(Instrumentor instrumentor)
48    {
49       this.instrumentor = instrumentor;
50    }
51
52    /**
53     * Adds a ConstructionInfo field to the passed in class
54     */

55    protected String JavaDoc addConstructionInfoField(int modifiers, CtClass addTo, CtConstructor ctor, int index) throws NotFoundException, CannotCompileException
56    {
57       return addConstructionInfoField(modifiers, addTo, ctor, index, null);
58    }
59    
60    /**
61     * Adds a ConstructionInfo field to the passed in class
62     */

63    protected String JavaDoc addConstructionInfoField(int modifiers, CtClass addTo, CtConstructor ctor, int index, CtField.Initializer init) throws NotFoundException, CannotCompileException
64    {
65       String JavaDoc name = getConstructionInfoFieldName(ctor.getDeclaringClass().getSimpleName(), index);
66       
67       //Instrumentor claspool will be null during hotswap, in which case we
68
//already will have created the field
69
if (instrumentor.getClassPool() != null)
70       {
71          try
72          {
73             addTo.getDeclaredField(name);
74             return name;
75          }
76          catch(NotFoundException e)
77          {
78          }
79          TransformerCommon.addInfoField(instrumentor, CONSTRUCTION_INFO_CLASS_NAME, name, modifiers, addTo, addInfoAsWeakReference(), init);
80          
81       }
82       return name;
83    }
84
85    protected boolean addInfoAsWeakReference()
86    {
87       return true;
88    }
89
90    public static String JavaDoc getConstructionInfoFieldName(String JavaDoc classname, int index)
91    {
92       if (classname.indexOf(".") >= 0)
93       {
94          throw new RuntimeException JavaDoc("Use simple class name for construction info field name: " + classname);
95       }
96       return "aop$constructionInfo_" + classname.replace('.','$') + "_" + index;
97    }
98       
99    protected static String JavaDoc constructionInfoFromWeakReference(String JavaDoc localName, String JavaDoc ctorInfoName)
100    {
101       return TransformerCommon.infoFromWeakReference(CONSTRUCTION_INFO_CLASS_NAME, localName, ctorInfoName);
102    }
103
104    // generateWrapper + prepareForWrapping
105
public boolean insertConstructionInterception(CtClass clazz, ClassAdvisor advisor)
106    throws Exception JavaDoc
107    {
108       if (!advisor.getManager().isConstruction()) return false;
109
110       boolean oneMatch = false;
111       List JavaDoc constructors = instrumentor.getConstructors(clazz);
112       
113       Iterator JavaDoc it = constructors.iterator();
114       for (int index = 0; it.hasNext(); index++)
115       {
116          // generate wrapper
117
CtConstructor constructor = (CtConstructor) it.next();
118          if (constructor.isClassInitializer() || !isAdvisableConstructor(constructor, advisor))
119          {
120             if (oneMatch)
121             {
122                //Should this be done for class initialisers?
123
generateNotMatchedConstructionInfoField(constructor, index);
124             }
125             continue;
126          }
127          
128          if (!oneMatch)
129          {
130             oneMatch = true;
131             instrumentor.setupBasics(clazz);
132             
133             for (int j = 0 ; j < index ; j++)
134             {
135                generateNotMatchedConstructionInfoField((CtConstructor)constructors.get(j), j);
136             }
137          }
138
139          generateConstructionInfoField(constructor, index);
140          insertInterception(constructor, index);
141       }
142       return oneMatch;
143    }
144
145    protected abstract void insertInterception(CtConstructor constructor, int index) throws Exception JavaDoc;
146
147    // currently used by CallerTransformer
148
public static boolean isAdvisableConstructor(CtConstructor con, ClassAdvisor advisor) throws NotFoundException
149    {
150       Iterator JavaDoc pointcuts = advisor.getManager().getPointcuts().values().iterator();
151       while (pointcuts.hasNext())
152       {
153          Pointcut pointcut = (Pointcut) pointcuts.next();
154          if (pointcut.matchesConstruction(advisor, con))
155          {
156             return true;
157          }
158       }
159       return false;
160    }
161
162    protected void generateConstructionInfoField(CtConstructor constructor, int index) throws NotFoundException, CannotCompileException
163    {
164       addConstructionInfoField(Modifier.STATIC | Modifier.PRIVATE,
165             constructor.getDeclaringClass(),
166             constructor,
167             index);
168    }
169    
170    protected void generateNotMatchedConstructionInfoField(CtConstructor constructor, int index) throws NotFoundException, CannotCompileException
171    {
172       
173    }
174    
175 }
Popular Tags