KickJava   Java API By Example, From Geeks To Geeks.

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


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

38 public class NonOptimizedMethodExecutionTransformer extends MethodExecutionTransformer
39 {
40    public NonOptimizedMethodExecutionTransformer(Instrumentor instrumentor)
41    {
42       super(instrumentor);
43    }
44
45    protected void transformMethod(MethodTransformation trans, boolean wrap) throws NotFoundException, CannotCompileException
46    {
47       String JavaDoc methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans);
48       // generate Wrapper
49
String JavaDoc wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(),
50                                                              trans.getMethod().getName());
51       CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null);
52
53       String JavaDoc originalName = trans.getOriginalName();
54       wmethod.setName(wrappedName);
55       trans.getClazz().addMethod(wmethod);
56       copyAnnotations(trans.getMethod(), wmethod);
57       trans.getMethod().setName(wrappedName);
58       wmethod.setName(originalName);
59       
60       trans.setWMethod(wmethod, wrappedName);
61
62       // prepareForWrapping
63
getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX);
64
65
66       if (wrap)
67       {
68          // wrap
69
getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX);
70
71          // executeWrapping
72
setWrapperBody(trans, methodInfoField);
73       }
74    }
75
76    protected void doWrap(MethodTransformation trans, String JavaDoc methodInfoFieldName)throws NotFoundException, Exception JavaDoc
77    {
78       setWrapperBody(trans, methodInfoFieldName);
79    }
80    
81 // protected void setWrapperBody(CtMethod method, String name, long hash, String methodInfoField, String wrappedName, CtMethod wmethod) throws NotFoundException
82
protected void setWrapperBody(MethodTransformation trans, String JavaDoc methodInfoField) throws NotFoundException
83    {
84       //make a copy of method and modify the existing method because we need to retain annotations
85
//of existing method
86
//wmethod = method;
87
boolean isStatic = Modifier.isStatic(trans.getMethod().getModifiers());
88       String JavaDoc code = null;
89       String JavaDoc args = "null";
90       if (trans.getMethod().getParameterTypes().length > 0) args = "$args";
91       if (!isStatic)
92       {
93          code =
94          "{ " +
95          " " + methodInfoFromWeakReference("info", methodInfoField) +
96          " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)_getInstanceAdvisor();" +
97          " if (info.getInterceptors() != (Object[])null || (instAdv != null && instAdv.hasInstanceAspects)) " +
98          " { " +
99          " Object[] ags = " + args + "; " +
100          " " + getAopReturnStr(trans.getMethod()) + Instrumentor.HELPER_FIELD_NAME + ".invokeMethod(instAdv, this, " + trans.getHash() + "L, ags, info); " +
101          " } " +
102          " else " +
103          " {" +
104          " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " +
105          " }" +
106          "}";
107       }
108       else
109       {
110          code =
111          "{ " +
112          " " + methodInfoFromWeakReference("info", methodInfoField) +
113          " if (info.getInterceptors() != (Object[])null) " +
114          " { " +
115          " org.jboss.aop.ClassInstanceAdvisor ia = null; " +
116          " Object[] ags = " + args + "; " +
117          " Object target = null; " +
118          " " + getAopReturnStr(trans.getMethod()) + Instrumentor.HELPER_FIELD_NAME + ".invokeMethod(ia, target, " + trans.getHash() + "L, ags, info); " +
119          " } " +
120          " else " +
121          " {" +
122          " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " +
123          " }" +
124          "}";
125       }
126       try
127       {
128          trans.setWMethodBody(code);
129       }
130       catch (CannotCompileException e)
131       {
132          e.printStackTrace();
133          throw new RuntimeException JavaDoc("code was: " + code + " for method " + trans.getOriginalName()); //To change body of catch statement use Options | File Templates.
134
}
135    }
136 }
137
Popular Tags