KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class OptimizedCallerInvocations extends OptimizedBehaviourInvocations
43 {
44
45    protected static String JavaDoc createOptimizedMethodCalledByConInvocationClass(
46          Instrumentor instrumentor,
47          String JavaDoc className,
48          CtClass callingClass,
49          CtMethod method,
50          int callingIndex,
51          long calledHash) throws NotFoundException, CannotCompileException
52    {
53       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
54       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation");
55    
56       ////////////////
57
//Create the class
58
boolean makeInnerClass = Modifier.isPrivate(method.getModifiers());
59       CtClass invocation = makeInvocationClass(pool, makeInnerClass, callingClass, className, methodInvocation);
60    
61       CtClass[] params = method.getParameterTypes();
62       addArgumentFieldsToInvocation(invocation, params);
63    
64       /////////
65
//Create invokeTarget() body
66
boolean isStatic = javassist.Modifier.isStatic(method.getModifiers());
67       if (!isStatic)
68       {
69          CtField target = new CtField(method.getDeclaringClass(), "typedTargetObject", invocation);
70          target.setModifiers(Modifier.PUBLIC);
71          invocation.addField(target);
72       }
73    
74    
75       CtMethod in = methodInvocation.getDeclaredMethod("invokeTarget");
76    
77       String JavaDoc code = "{";
78    
79       String JavaDoc returnStr = (method.getReturnType().equals(CtClass.voidType)) ? "" : "return ($w)";
80       if (isStatic)
81       {
82          code +=
83          " " + returnStr + " " + method.getDeclaringClass().getName() + ".";
84       }
85       else
86       {
87          code +=
88          " " + returnStr + " typedTargetObject.";
89       }
90       code += method.getName() + "(";
91       for (int i = 0; i < params.length; i++)
92       {
93          if (i > 0) code += ", ";
94          code += "arg" + i;
95       }
96       code += "); ";
97       if (method.getReturnType().equals(CtClass.voidType))
98       {
99          code += " return null; ";
100       }
101       code += "}";
102       CtMethod invokeTarget = null;
103       try
104       {
105          invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code, invocation);
106       }
107       catch (CannotCompileException e)
108       {
109          System.out.println(code);
110          throw e;
111       }
112       invokeTarget.setModifiers(in.getModifiers());
113       invocation.addMethod(invokeTarget);
114       addGetArguments(pool, invocation, method.getParameterTypes());
115       addSetArguments(pool, invocation, method.getParameterTypes());
116       
117    
118       ////////////
119
//Create copy() method
120
CtMethod copyTemplate = methodInvocation.getDeclaredMethod("copy");
121    
122       String JavaDoc copyCode = "{ "
123       + " "
124       + invocation.getName()
125       + " wrapper = new "
126       + invocation.getName()
127       + "(this.advisor, this.calling, this.method, this.targetObject, this.arguments, this.interceptors);"
128       + " wrapper.metadata = this.metadata; "
129       + " wrapper.currentInterceptor = this.currentInterceptor; "
130       + " wrapper.instanceResolver = this.instanceResolver; "
131       + " wrapper.targetObject = this.targetObject; "
132       + " wrapper.responseContextInfo = this.responseContextInfo; ";
133    
134       if (!isStatic)
135       {
136          copyCode += "wrapper.typedTargetObject = typedTargetObject;";
137       }
138    
139       for (int i = 0; i < params.length; i++)
140       {
141          copyCode += " wrapper.arg" + i + " = this.arg" + i + "; ";
142       }
143       copyCode += " return wrapper; }";
144    
145       CtMethod copy = null;
146       try
147       {
148          copy = CtNewMethod.make(
149                copyTemplate.getReturnType(),
150                "copy",
151                copyTemplate.getParameterTypes(),
152                copyTemplate.getExceptionTypes(),
153                copyCode,
154                invocation);
155       }
156       catch (CannotCompileException e)
157       {
158          System.out.println(copyCode);
159          throw e;
160       }
161       copy.setModifiers(copyTemplate.getModifiers());
162       invocation.addMethod(copy);
163    
164       TransformerCommon.compileOrLoadClass(callingClass, invocation);
165    
166       //Return fully qualified name of class (may be an inner class)
167
return invocation.getName();
168    }
169
170    protected static String JavaDoc createOptimizedConCalledByConInvocationClass(
171          Instrumentor instrumentor,
172          String JavaDoc className,
173          CtClass callingClass,
174          CtConstructor con,
175          int callingIndex,
176          long calledHash) throws NotFoundException, CannotCompileException
177    {
178       //TODO: Merge this method with createOptimizedConCalledByMethodInvocationClass()
179
AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
180       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorCalledByConstructorInvocation");
181    
182       ////////////////
183
//Create the class
184
//String className = getOptimizedConCalledByConInvocationClassName(callingIndex, callingClass.getName(), calledHash);
185
boolean makeInnerClass = Modifier.isPrivate(con.getModifiers());
186       CtClass invocation = makeInvocationClass(pool, makeInnerClass, callingClass, className, conInvocation);
187    
188       CtClass[] params = con.getParameterTypes();
189       addArgumentFieldsToInvocation(invocation, params);
190    
191       /////////
192
//Create invokeTarget() body
193
CtMethod in = conInvocation.getDeclaredMethod("invokeTarget");
194
195       String JavaDoc code = "{";
196       
197       code += "setTargetObject(new " + con.getDeclaringClass().getName() + "(";
198       for (int i = 0; i < params.length; i++)
199       {
200          if (i > 0) code += ", ";
201          code += "arg" + i;
202       }
203       code += ")); ";
204       code += "return getTargetObject();";
205       code += "}";
206
207       CtMethod invokeTarget = null;
208       try
209       {
210          invokeTarget = CtNewMethod.make(
211                in.getReturnType(),
212                "invokeTarget",
213                in.getParameterTypes(),
214                in.getExceptionTypes(),
215                code,
216                invocation);
217       }
218       catch (Exception JavaDoc e)
219       {
220          throw new RuntimeException JavaDoc("code: " + code, e);
221       }
222       invokeTarget.setModifiers(in.getModifiers());
223       invocation.addMethod(invokeTarget);
224       addGetArguments(pool, invocation, con.getParameterTypes());
225       addSetArguments(pool, invocation, con.getParameterTypes());
226    
227       ////////////
228
//Create copy() method
229
CtMethod copyTemplate = conInvocation.getDeclaredMethod("copy");
230    
231       String JavaDoc copyCode = "{ "
232          + " "
233          + invocation.getName()
234          + " wrapper = new "
235          + invocation.getName()
236          + "(this.advisor, this.calling, this.constructor, this.wrappingMethod, this.arguments, this.interceptors);"
237          + " wrapper.metadata = super.metadata; "
238          + " wrapper.currentInterceptor = super.currentInterceptor; "
239          + " wrapper.instanceResolver = super.instanceResolver; "
240          + " wrapper.interceptors = super.interceptors; "
241          + " wrapper.responseContextInfo = super.responseContextInfo; "
242          + " wrapper.targetObject = super.targetObject; ";
243       
244       for (int i = 0; i < params.length; i++)
245       {
246          copyCode += " wrapper.arg" + i + " = this.arg" + i + "; ";
247       }
248       copyCode += " return wrapper; }";
249
250       CtMethod copy = null;
251       try
252       {
253          copy = CtNewMethod.make(
254                copyTemplate.getReturnType(),
255                "copy",
256                copyTemplate.getParameterTypes(),
257                copyTemplate.getExceptionTypes(),
258                copyCode,
259                invocation);
260       }
261       catch (Exception JavaDoc e)
262       {
263          throw new RuntimeException JavaDoc("code: " + code, e);
264       }
265       copy.setModifiers(copyTemplate.getModifiers());
266       invocation.addMethod(copy);
267    
268       TransformerCommon.compileOrLoadClass(callingClass, invocation);
269    
270       //Return fully qualified name of class (may be an inner class)
271
return invocation.getName();
272    }
273
274    protected static String JavaDoc createOptimizedConCalledByMethodInvocationClass(
275          Instrumentor instrumentor,
276          String JavaDoc className,
277          CtClass callingClass,
278          CtConstructor con,
279          long callingHash,
280          long calledHash) throws NotFoundException, CannotCompileException
281    {
282       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
283       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation");
284    
285       ////////////////
286
//Create the class
287
boolean makeInnerClass = Modifier.isPrivate(con.getModifiers());
288       CtClass invocation = makeInvocationClass(pool, makeInnerClass, callingClass, className, conInvocation);
289    
290       CtClass[] params = con.getParameterTypes();
291       addArgumentFieldsToInvocation(invocation, params);
292    
293       /////////
294
//Create invokeTarget() body
295
CtMethod in = conInvocation.getDeclaredMethod("invokeTarget");
296
297       String JavaDoc code = "{";
298       code += "setTargetObject(new " + con.getDeclaringClass().getName() + "(";
299       for (int i = 0; i < params.length; i++)
300       {
301          if (i > 0) code += ", ";
302          code += "arg" + i;
303       }
304       code += ")); ";
305       code += "return getTargetObject();";
306       code += "}";
307       
308       CtMethod invokeTarget = null;
309       try
310       {
311          invokeTarget = CtNewMethod.make(
312                in.getReturnType(),
313                "invokeTarget",
314                in.getParameterTypes(),
315                in.getExceptionTypes(),
316                code,
317                invocation);
318       }
319       catch (CannotCompileException e)
320       {
321          System.out.println(code);
322          throw e;
323       }
324
325       invokeTarget.setModifiers(in.getModifiers());
326    
327       invocation.addMethod(invokeTarget);
328       addGetArguments(pool, invocation, con.getParameterTypes());
329       addSetArguments(pool, invocation, con.getParameterTypes());
330    
331       ////////////
332
//Create copy() method
333
CtMethod copyTemplate = conInvocation.getDeclaredMethod("copy");
334
335       String JavaDoc copyCode = "{ "
336          + " "
337          + invocation.getName()
338          + " wrapper = new "
339          + invocation.getName()
340          + "(this.advisor, this.callingClass, this.callingMethod, this.constructor, this.wrappingMethod, this.callingObject, this.arguments, this.interceptors);"
341          + " wrapper.metadata = this.metadata; "
342          + " wrapper.currentInterceptor = this.currentInterceptor; "
343          + " wrapper.instanceResolver = this.instanceResolver; "
344          + " wrapper.targetObject = this.targetObject; "
345          + " wrapper.responseContextInfo = this.responseContextInfo; ";
346       
347       for (int i = 0; i < params.length; i++)
348       {
349          copyCode += " wrapper.arg" + i + " = this.arg" + i + "; ";
350       }
351       copyCode += " return wrapper; }";
352
353       CtMethod copy = null;
354       try
355       {
356          copy = CtNewMethod.make(
357                copyTemplate.getReturnType(),
358                "copy",
359                copyTemplate.getParameterTypes(),
360                copyTemplate.getExceptionTypes(),
361                copyCode,
362                invocation);
363       }
364       catch (CannotCompileException e)
365       {
366          System.out.println(copyCode);
367          throw e;
368       }
369       copy.setModifiers(copyTemplate.getModifiers());
370    
371       invocation.addMethod(copy);
372    
373       TransformerCommon.compileOrLoadClass(callingClass, invocation);
374       
375       //Return fully qualified name of class (may be an inner class)
376
return invocation.getName();
377    }
378
379    protected static String JavaDoc createOptimizedMethodCalledByMethodInvocationClass(
380          Instrumentor instrumentor,
381          String JavaDoc className,
382          CtClass callingClass,
383          CtMethod method,
384          long callingHash,
385          long calledHash) throws NotFoundException, CannotCompileException
386    {
387       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
388       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodCalledByMethodInvocation");
389    
390       ////////////////
391
//Create the class
392
boolean makeInnerClass = Modifier.isPrivate(method.getModifiers());
393       CtClass invocation = makeInvocationClass(pool, makeInnerClass, callingClass, className, methodInvocation);
394    
395       CtClass[] params = method.getParameterTypes();
396       addArgumentFieldsToInvocation(invocation, params);
397    
398       /////////
399
//Create invokeTarget() body
400
boolean isStatic = javassist.Modifier.isStatic(method.getModifiers());
401       if (!isStatic)
402       {
403          CtField target = new CtField(method.getDeclaringClass(), "typedTargetObject", invocation);
404          target.setModifiers(Modifier.PUBLIC);
405          invocation.addField(target);
406       }
407    
408    
409       CtMethod in = methodInvocation.getDeclaredMethod("invokeTarget");
410    
411       String JavaDoc code = "{";
412    
413       String JavaDoc returnStr = (method.getReturnType().equals(CtClass.voidType)) ? "" : "return ($w)";
414       if (isStatic)
415       {
416          code +=
417          " " + returnStr + " " + method.getDeclaringClass().getName() + ".";
418       }
419       else
420       {
421          code +=
422          " " + returnStr + " typedTargetObject.";
423       }
424       code += method.getName() + "(";
425       for (int i = 0; i < params.length; i++)
426       {
427          if (i > 0) code += ", ";
428          code += "arg" + i;
429       }
430       code += "); ";
431       if (method.getReturnType().equals(CtClass.voidType))
432       {
433          code += " return null; ";
434       }
435       code += "}";
436       
437       CtMethod invokeTarget = null;
438       try
439       {
440          invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code, invocation);
441       }
442       catch (CannotCompileException e)
443       {
444          System.out.println(code);
445          throw e;
446       }
447       invokeTarget.setModifiers(in.getModifiers());
448       invocation.addMethod(invokeTarget);
449       addGetArguments(pool, invocation, method.getParameterTypes());
450       addSetArguments(pool, invocation, method.getParameterTypes());
451    
452       ////////////
453
//Create copy() method
454
CtMethod copyTemplate = methodInvocation.getDeclaredMethod("copy");
455    
456       String JavaDoc copyCode = "{ "
457       + " "
458       + invocation.getName()
459       + " wrapper = new "
460       + invocation.getName()
461       + "(this.advisor, this.callingClass, this.callingMethod, this.method, this.callingObject, this.targetObject, this.arguments, this.interceptors);"
462       + " wrapper.metadata = this.metadata; "
463       + " wrapper.currentInterceptor = this.currentInterceptor; "
464       + " wrapper.instanceResolver = this.instanceResolver; "
465       + " wrapper.targetObject = this.targetObject; "
466       + " wrapper.responseContextInfo = this.responseContextInfo; ";
467    
468       if (!isStatic)
469       {
470          copyCode += "wrapper.typedTargetObject = typedTargetObject;";
471       }
472    
473       for (int i = 0; i < params.length; i++)
474       {
475          copyCode += " wrapper.arg" + i + " = this.arg" + i + "; ";
476       }
477       copyCode += " return wrapper; }";
478    
479       CtMethod copy = null;
480       try
481       {
482          copy = CtNewMethod.make(
483                copyTemplate.getReturnType(),
484                "copy",
485                copyTemplate.getParameterTypes(),
486                copyTemplate.getExceptionTypes(),
487                copyCode,
488                invocation);
489       }
490       catch (CannotCompileException e)
491       {
492          System.out.println(copyCode);
493          throw e;
494       }
495       copy.setModifiers(copyTemplate.getModifiers());
496       invocation.addMethod(copy);
497    
498       TransformerCommon.compileOrLoadClass(callingClass, invocation);
499    
500       //Return fully qualified name of class (may be an inner class)
501
return invocation.getName();
502    }
503    
504 }
505
Popular Tags