KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import javassist.CannotCompileException;
28 import javassist.ClassPool;
29 import javassist.CtClass;
30 import javassist.CtField;
31 import javassist.CtMethod;
32 import javassist.NotFoundException;
33 import javassist.expr.ExprEditor;
34 import javassist.expr.FieldAccess;
35
36 import org.jboss.aop.ClassAdvisor;
37 import org.jboss.aop.instrument.OptimizedFieldAccessTransformer.OptimizedFieldAccessExprEditor;
38
39 /**
40  * Comment
41  *
42  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
43  * @version $Revision$
44  */

45 public class NonOptimizedFieldAccessTransformer extends FieldAccessTransformer
46 {
47
48    public NonOptimizedFieldAccessTransformer(Instrumentor instrumentor)
49    {
50       super(instrumentor);
51    }
52    
53    protected void doBuildFieldWrappers(CtClass clazz, CtField field, int fieldIndex, JoinpointClassification classificationGet, JoinpointClassification classificationSet)
54    throws NotFoundException, CannotCompileException
55    {
56       instrumentor.setupBasics(clazz);
57       
58       boolean wrappedGet = classificationGet.equals(JoinpointClassification.WRAPPED);
59       boolean wrappedSet = classificationSet.equals(JoinpointClassification.WRAPPED);
60       int mod = getStaticModifiers(field);
61        
62       // executeWrapping
63
replaceFieldAccessInternally(clazz, field, wrappedGet, wrappedSet, fieldIndex);
64
65       // don't need wrappers if the field is private as we inline
66
// the conditional and interception directly within code.
67
if (!javassist.Modifier.isPrivate(field.getModifiers()))
68       {
69          // prepareForWrapping
70
if (isPrepared(classificationGet))
71          {
72             wrapper.prepareForWrapping(field, GET_INDEX);
73          }
74          if (isPrepared(classificationSet))
75          {
76             wrapper.prepareForWrapping(field, SET_INDEX);
77          }
78           
79          // wrap
80
if (wrappedGet)
81          {
82             wrapper.wrap(field, GET_INDEX);
83             if (classificationGet.equals(JoinpointClassification.DYNAMICALY_WRAPPED))
84             {
85                instrumentor.dynamicTransformationObserver.fieldReadDynamicalyWrapped(field);
86             }
87          }
88          if (wrappedSet)
89          {
90             wrapper.wrap(field, SET_INDEX);
91             if (classificationSet.equals(JoinpointClassification.DYNAMICALY_WRAPPED))
92             {
93                instrumentor.dynamicTransformationObserver.fieldWriteDynamicalyWrapped(field);
94             }
95          }
96          // generateWrapper
97
buildWrapperPlaceHolders(clazz, field, isPrepared(classificationGet), isPrepared(classificationSet), mod);
98          buildWrappers(clazz, field, isPrepared(classificationGet), isPrepared(classificationSet), fieldIndex);//mod);
99
}
100    }
101    
102    private void buildWrappers(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index)
103    throws NotFoundException, CannotCompileException
104    {
105       if (doGet)
106       {
107          String JavaDoc code = getWrapperBody(clazz, field, true, index);
108          CtMethod method = clazz.getDeclaredMethod(fieldRead(field.getName()));
109          method.setBody(code);
110       }
111       if (doSet)
112       {
113          String JavaDoc code = getWrapperBody(clazz, field, false, index);
114          CtMethod method = clazz.getDeclaredMethod(fieldWrite(field.getName()));
115          method.setBody(code);
116       }
117    }
118    
119    protected String JavaDoc getWrapperBody(CtClass clazz, CtField field, boolean get, int fieldIndex) throws NotFoundException, CannotCompileException
120    {
121       String JavaDoc name = field.getName();
122       boolean isStatic = (javassist.Modifier.isStatic(field.getModifiers()));
123       String JavaDoc access = "";
124       String JavaDoc instanceCheck = "";
125       if (!isStatic)
126       {
127          access = "((" + clazz.getName() + ")$1).";
128          instanceCheck = " || ((org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$1)._getInstanceAdvisor()).hasInstanceAspects";
129       }
130       
131       // read wrapper
132
if (get)
133       {
134          return
135             "{ " +
136             " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() " + instanceCheck + " ) " +
137             " { " +
138             " return ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead($1, (int)" + (fieldIndex) + "); " +
139             " } " +
140             " return " + access + name + "; " +
141             "}";
142       }
143       // write wrapper
144
return
145              "{ " +
146              " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() " + instanceCheck + " ) " +
147              " { " +
148              " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite($1, (int)" + (fieldIndex) + ", ($w)$2); " +
149              " } " +
150              " else " +
151              " { " +
152              " " + access + name + " = $2; " +
153              " } " +
154              "}";
155    }
156
157    protected void replaceFieldAccessInternally(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) throws CannotCompileException
158    {
159       NonOptimizedFieldAccessExprEditor expr = new NonOptimizedFieldAccessExprEditor(clazz, field, doGet, doSet, index);
160       clazz.instrument(expr);
161    }
162
163
164    protected class NonOptimizedFieldAccessExprEditor extends FieldAccessExprEditor
165    {
166       public NonOptimizedFieldAccessExprEditor(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index)
167       {
168          super(clazz, field, doGet, doSet, index);
169       }
170
171       protected void replaceRead(FieldAccess fieldAccess) throws CannotCompileException
172       {
173          String JavaDoc code = null;
174          try
175          {
176             if (fieldAccess.isStatic())
177             {
178                code =
179                " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects()) " +
180                " { " +
181                " Object obj = null;" +
182                " $_ = ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead(obj, (int)" + (fieldIndex) + "); " +
183                " } " +
184                " else " +
185                " { " +
186                " $_ = " + clazz.getName() + "." + field.getName() + "; " +
187                " } " +
188                "";
189                fieldAccess.replace(code);
190             }
191             else
192             {
193                code =
194                " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$0)._getInstanceAdvisor();" +
195                " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() || (instAdv != null && instAdv.hasInstanceAspects)) " +
196                " { " +
197                " $_ = ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead($0, (int)" + (fieldIndex) + "); " +
198                " } " +
199                " else " +
200                " { " +
201                " $_ = $0." + fieldAccess.getFieldName() + "; " +
202                " } ";
203                fieldAccess.replace(code);
204             }
205          }
206          catch (CannotCompileException e)
207          {
208             throw new RuntimeException JavaDoc("failed with: " + code, e);
209          }
210       }
211
212       protected void replaceWrite(FieldAccess fieldAccess) throws CannotCompileException
213       {
214          if (fieldAccess.isStatic())
215          {
216             String JavaDoc code =
217                     " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects()) " +
218                     " { " +
219                     " Object obj = null;" +
220                     " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite(obj, (int)" + (fieldIndex) + ", ($w)$1); " +
221                     " } " +
222                     " else " +
223                     " { " +
224                     " " + clazz.getName() + "." + fieldAccess.getFieldName() + " = $1; " +
225                     " } ";
226             fieldAccess.replace(code);
227          }
228          else
229          {
230             String JavaDoc code =
231                     " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$0)._getInstanceAdvisor();" +
232                     " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() || (instAdv != null && instAdv.hasInstanceAspects)) " +
233                     " { " +
234                     " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite($0, (int)" + (fieldIndex) + ", ($w)$1); " +
235                     " } " +
236                     " else " +
237                     " { " +
238                     " $0." + fieldAccess.getFieldName() + " = $1; " +
239                     " } ";
240             fieldAccess.replace(code);
241          }
242       }
243
244
245    }//End Inner class FieldAccessExprEditor
246

247 }
248
Popular Tags