KickJava   Java API By Example, From Geeks To Geeks.

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


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 javassist.CannotCompileException;
25 import javassist.CtClass;
26 import javassist.Modifier;
27 import javassist.NotFoundException;
28
29 import org.jboss.aop.AspectManager;
30 import org.jboss.aop.ClassAdvisor;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
36  * @version $Revision$
37  */

38 public class OptimizedCallerTransformer extends CallerTransformer
39 {
40    public OptimizedCallerTransformer(Instrumentor instrumentor, AspectManager manager)
41    {
42       super(instrumentor, manager, true, new ClassicCallerInfoAdder(instrumentor));
43    }
44
45    protected CallerExprEditor callerExprEditorFactory(ClassAdvisor advisor, CtClass clazz)
46    {
47       return new OptimizedCallerExprEditor(advisor, clazz);
48    }
49
50    class OptimizedCallerExprEditor extends CallerExprEditor
51    {
52
53       public OptimizedCallerExprEditor(ClassAdvisor advisor, CtClass callingClass)
54       {
55          super(advisor, callingClass);
56       }
57       
58       protected void setupConstructor(ConstructorDetail cd)throws NotFoundException, CannotCompileException
59       {
60          if (callerInfos.get(cd.callerInfoField) == null)
61          {
62             String JavaDoc createName = getOptimizedMethodCalledByConstructorClassName(cd.callingIndex, callingClass.getName(), cd.calledHash);
63             String JavaDoc invocationClassName = OptimizedCallerInvocations.createOptimizedMethodCalledByConInvocationClass(instrumentor, createName, callingClass, cd.calledMethod, cd.callingIndex, cd.calledHash);
64
65             callerInfos.put(cd.callerInfoField, invocationClassName);
66             callerInfoAdder.addMethodByConInfoField(callingClass, cd.callerInfoField, cd.callingIndex, cd.classname, cd.calledHash);
67          }
68       }
69
70       protected void setupMethod(MethodDetail md) throws NotFoundException, CannotCompileException
71       {
72          if (callerInfos.get(md.callerInfoField) == null)
73          {
74             String JavaDoc createName = getOptimizedMethodCalledByMethodClassName(md.callingHash, callingClass.getName(), md.calledHash);
75             String JavaDoc invocationClassName = OptimizedCallerInvocations.createOptimizedMethodCalledByMethodInvocationClass(instrumentor, createName, callingClass, md.calledMethod, md.callingHash, md.calledHash);
76                   
77             callerInfos.put(md.callerInfoField, invocationClassName);
78             callerInfoAdder.addMethodByMethodInfoField(callingClass, md.callerInfoField, md.callingHash, md.classname, md.calledHash);
79          }
80       }
81
82       protected void setupMethod(ConByMethodDetail cd) throws NotFoundException, CannotCompileException
83       {
84          if (callerInfos.get(cd.callerInfoField) == null)
85          {
86             //If target constructor is advisable we do not want to create an optimized invocation class
87
//since the interceptor chain for its execution pointcut does not get invoked.
88
if (!cd.isTgtConAdvised)
89             {
90                String JavaDoc createName = getOptimizedConCalledByMethodInvocationClassName(cd.callingHash, callingClass.getName(), cd.calledHash);
91                String JavaDoc invocationClassName = OptimizedCallerInvocations.createOptimizedConCalledByMethodInvocationClass(instrumentor, createName, callingClass, cd.calledConstructor, cd.callingHash, cd.calledHash);
92                callerInfos.put(cd.callerInfoField, invocationClassName);
93             }
94             else
95             {
96                callerInfos.put(cd.callerInfoField, NonOptimizedCallerTransformer.PLACEHOLDER);
97             }
98
99             callerInfoAdder.addConByMethodInfoField(callingClass, cd.callerInfoField, cd.callingHash, cd.classname, cd.calledHash);
100          }
101       }
102       
103       protected void setupConstructor(ConByConDetail cd)throws NotFoundException, CannotCompileException
104       {
105          if (callerInfos.get(cd.callerInfoField) == null)
106          {
107             //If target constructor is advisable we do not want to create an optimized invocation class
108
//since the interceptor chain for its execution pointcut does not get invoked.
109
if (!cd.isTgtConAdvised)
110             {
111                String JavaDoc createName = getOptimizedConCalledByConInvocationClassName(cd.callingIndex, callingClass.getName(), cd.calledHash);
112                String JavaDoc invocationClassName = OptimizedCallerInvocations.createOptimizedConCalledByConInvocationClass(instrumentor, createName, callingClass, cd.calledConstructor, cd.callingIndex, cd.calledHash);
113                callerInfos.put(cd.callerInfoField, invocationClassName);
114             }
115             else
116             {
117                callerInfos.put(cd.callerInfoField, NonOptimizedCallerTransformer.PLACEHOLDER);
118             }
119             callerInfoAdder.addConByConInfoField(callingClass, cd.callerInfoField, cd.callingIndex, cd.classname, cd.calledHash);
120          }
121       }
122
123       protected void replaceMethodCallInCon(ConstructorDetail cd)throws CannotCompileException, NotFoundException
124       {
125          String JavaDoc invocationClassName = (String JavaDoc) callerInfos.get(cd.callerInfoField);
126          String JavaDoc typedTargetObject = javassist.Modifier.isStatic(cd.calledMethod.getModifiers()) ? "" : "invocation.typedTargetObject=$0;";
127    
128          String JavaDoc replaced =
129          methodByConInfoFromWeakReference("info", cd.callerInfoField) +
130          "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " +
131          invocationClassName + " invocation = new "
132          + invocationClassName + " (info, $0, $args, info.getInterceptors());" +
133          typedTargetObject +
134          OptimizedBehaviourInvocations.setArguments(cd.calledMethod.getParameterTypes().length) +
135          "invocation.setTargetObject($0);" +
136          "$_ = ($r)invocation.invokeNext();" +
137          "} else { " +
138          "$_ = $proceed($$); " +
139          "}";
140          
141          cd.call.replace(replaced);
142       }
143    
144       protected void replaceMethodCallInMethod(MethodDetail md)throws NotFoundException, CannotCompileException
145       {
146          String JavaDoc callingObject = ", null";
147          if (!Modifier.isStatic(md.where.getModifiers()))
148          {
149             callingObject = ", this";
150          }
151
152          String JavaDoc invocationClassName = (String JavaDoc) callerInfos.get(md.callerInfoField);
153          String JavaDoc typedTargetObject = javassist.Modifier.isStatic(md.calledMethod.getModifiers()) ? "" : "invocation.typedTargetObject=$0;";
154
155          String JavaDoc replaced =
156          methodByMethodInfoFromWeakReference("info", md.callerInfoField) +
157          "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " +
158          invocationClassName + " invocation = new "
159          + invocationClassName + " (info" + callingObject + ", $0, $args, info.getInterceptors());" +
160          OptimizedBehaviourInvocations.setArguments(md.calledMethod.getParameterTypes().length) +
161          typedTargetObject +
162          "invocation.setTargetObject($0);" +
163          "$_ = ($r)invocation.invokeNext();" +
164          "} else { " +
165          "$_ = $proceed($$); " +
166          "}";
167
168          md.call.replace(replaced);
169       }
170
171       protected void replaceConCallInMethod(ConByMethodDetail cd) throws NotFoundException, CannotCompileException
172       {
173          //If target constructor is advisable we do not want to create an optimized invocation class
174
//since the interceptor chain for its execution pointcut does not get invoked.
175
if (cd.isTgtConAdvised)
176          {
177             super.replaceConCallInMethod(cd);
178             return;
179          }
180          String JavaDoc callingObject = "null";
181          if (!Modifier.isStatic(cd.where.getModifiers()))
182          {
183             callingObject = "this";
184          }
185
186          String JavaDoc invocationClassName = (String JavaDoc) callerInfos.get(cd.callerInfoField);
187          String JavaDoc replaced =
188          conByMethodInfoFromWeakReference("info", cd.callerInfoField) +
189          "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " +
190          "java.lang.Object callingObject = " + callingObject + "; " +
191          invocationClassName + " invocation = new "
192          + invocationClassName + " (info," + callingObject + ", $args, info.getInterceptors());" +
193          OptimizedBehaviourInvocations.setArguments(cd.calledConstructor.getParameterTypes().length) +
194          "$_ = ($r)invocation.invokeNext();" +
195          "} else { " +
196          "$_ = $proceed($$); " +
197          "}";
198
199          cd.call.replace(replaced);
200       }
201
202       protected void replaceConCallInCon(ConByConDetail cd)throws CannotCompileException, NotFoundException
203       {
204          //If target constructor is advisable we do not want to create an optimized invocation class
205
//since the interceptor chain for its execution pointcut does not get invoked.
206
if (cd.isTgtConAdvised)
207          {
208             super.replaceConCallInCon(cd);
209             return;
210          }
211          String JavaDoc invocationClassName = (String JavaDoc) callerInfos.get(cd.callerInfoField);
212
213          String JavaDoc replaced =
214          conByConInfoFromWeakReference("info", cd.callerInfoField) +
215          "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " +
216          invocationClassName + " invocation = new "
217          + invocationClassName + "(info, $args, info.getInterceptors());" +
218          OptimizedBehaviourInvocations.setArguments(cd.calledConstructor.getParameterTypes().length) +
219          "$_ = ($r)invocation.invokeNext();" +
220          "} else { " +
221          "$_ = $proceed($$); " +
222          "}";
223
224          cd.call.replace(replaced);
225       }
226    }
227
228 }
229
Popular Tags