KickJava   Java API By Example, From Geeks To Geeks.

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


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

39 public class OptimizedMethodExecutionTransformer extends MethodExecutionTransformer
40 {
41
42    public OptimizedMethodExecutionTransformer(Instrumentor instrumentor)
43    {
44       super(instrumentor);
45    }
46    
47    protected void transformMethod(MethodTransformation trans, boolean wrap)throws CannotCompileException, NotFoundException
48    {
49       String JavaDoc methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans);
50       String JavaDoc wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(),
51                                                              trans.getOriginalName());
52       CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null);
53
54       // generate Wrapper
55
String JavaDoc originalName = trans.getOriginalName();
56       wmethod.setName(wrappedName);
57       trans.getClazz().addMethod(wmethod);
58       copyAnnotations(trans.getMethod(), wmethod);
59       String JavaDoc optimizedInvocation = OptimizedMethodInvocations.createOptimizedInvocationClass(trans.getInstrumentor(), trans.getClazz(), trans.getMethod());
60       trans.getMethod().setName(wrappedName);
61       wmethod.setName(originalName);
62
63       trans.setWMethod(wmethod, wrappedName);
64       // prepareForWrapping
65
getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX);
66
67       if (wrap)
68       {
69          // wrap
70
getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX);
71          // executeWrapping
72
setWrapperBody(trans, methodInfoField, optimizedInvocation);
73       }
74    }
75
76    protected void doWrap(MethodTransformation trans, String JavaDoc methodInfoFieldName)throws NotFoundException, Exception JavaDoc
77    {
78       String JavaDoc invocationClassName = OptimizedMethodInvocations.getOptimizedInvocationClassName(trans.getClazz(), trans.getWMethod());
79       invocationClassName = invocationClassName.substring(invocationClassName.lastIndexOf('.') + 1);
80       invocationClassName = trans.getMethod().getDeclaringClass().getName() + "$" + invocationClassName;
81       setWrapperBody(trans, methodInfoFieldName, invocationClassName);
82    }
83    
84    protected void setWrapperBody(MethodTransformation trans, String JavaDoc methodInfoField, String JavaDoc optimizedInvocation) throws NotFoundException
85    {
86       boolean isStatic = Modifier.isStatic(trans.getMethod().getModifiers());
87       String JavaDoc code = null;
88       if (!isStatic)
89       {
90          code =
91          "{ " +
92          " " + methodInfoFromWeakReference("info", methodInfoField) +
93          " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)_getInstanceAdvisor();" +
94          //" System.out.println(\"" + trans.getMethod() + " \" + instAdv);" +
95
" org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" +
96          " if (interceptors != (Object[])null || (instAdv != null && instAdv.hasInstanceAspects)) " +
97          " { " +
98          " if (instAdv != null) " +
99          " { " +
100          " interceptors = instAdv.getInterceptors(interceptors); " +
101          " } " +
102          " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(info, interceptors); " +
103          " " + OptimizedBehaviourInvocations.setArguments(trans.getMethod().getParameterTypes().length) +
104          " invocation.setTargetObject(this); " +
105          " invocation.typedTargetObject = this; " +
106          " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " +
107          " " + getAopReturnStr(trans.getMethod()) + "invocation.invokeNext(); " +
108          " } " +
109          " else " +
110          " {" +
111          " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " +
112          " }" +
113          "}";
114       }
115       else
116       {
117          code =
118          "{ " +
119          " " + methodInfoFromWeakReference("info", methodInfoField) +
120          " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" +
121          " if (interceptors != (Object[])null) " +
122          " { " +
123          " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(info, interceptors); " +
124          " " + OptimizedBehaviourInvocations.setArguments(trans.getMethod().getParameterTypes().length) +
125          " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " +
126          " " + getAopReturnStr(trans.getMethod()) + "invocation.invokeNext(); " +
127          " } " +
128          " else " +
129          " {" +
130          " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " +
131          " }" +
132          "}";
133       }
134       try
135       {
136          trans.setWMethodBody(code);
137       }
138       catch (CannotCompileException e)
139       {
140          e.printStackTrace();
141          throw new RuntimeException JavaDoc("code was: " + code + " for method " + trans.getOriginalName()); //To change body of catch statement use Options | File Templates.
142
}
143    }
144    
145 }
146
Popular Tags