KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class OptimizedFieldInvocations extends OptimizedInvocations
43 {
44
45    protected static String JavaDoc getOptimizedInvocationClassName(CtClass clazz, CtField field, boolean get) throws Exception JavaDoc
46    {
47       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(clazz.getName());
48       sb.append(".")
49       .append(field.getName());
50    
51       if (get)
52       {
53          sb.append("_Get");
54       }
55       else
56       {
57          sb.append("_Set");
58       }
59    
60       return sb.toString();
61    }
62
63    protected static void addCopy(ClassPool pool, CtClass invocation, boolean isStatic, boolean isGet) throws Exception JavaDoc
64    {
65       CtClass fieldInvocation =
66               isGet
67               ? pool.get("org.jboss.aop.joinpoint.FieldReadInvocation")
68               : pool.get("org.jboss.aop.joinpoint.FieldWriteInvocation");
69       CtMethod template = fieldInvocation.getDeclaredMethod("copy");
70    
71       String JavaDoc newExpr =
72               (isGet)
73               ? invocation.getName()
74                 + " wrapper = new "
75                 + invocation.getName()
76                 + "(this.field, this.index, this.interceptors); "
77               : invocation.getName()
78                 + " wrapper = new "
79                 + invocation.getName()
80                 + "(this.field, this.index, this.value, this.interceptors); ";
81    
82       String JavaDoc code =
83               "{ "
84               + " "
85               + newExpr
86               + " wrapper.metadata = this.metadata; "
87               + " wrapper.currentInterceptor = this.currentInterceptor; "
88               + " wrapper.instanceResolver = this.instanceResolver; ";
89       if (!isStatic)
90       {
91          code += " wrapper.typedTargetObject = this.typedTargetObject; ";
92          code += " wrapper.targetObject = this.targetObject; ";
93       }
94    
95       code += " return wrapper; }";
96    
97       CtMethod copy =
98          CtNewMethod.make(template.getReturnType(),
99                           "copy",
100                           template.getParameterTypes(),
101                           template.getExceptionTypes(),
102                           code,
103                           invocation);
104       copy.setModifiers(template.getModifiers());
105       invocation.addMethod(copy);
106    
107    }
108
109    protected static String JavaDoc createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtField field, boolean get) throws Exception JavaDoc
110    {
111       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
112       CtClass fieldInvocation =
113               get
114               ? pool.get("org.jboss.aop.joinpoint.FieldReadInvocation")
115               : pool.get("org.jboss.aop.joinpoint.FieldWriteInvocation");
116    
117       String JavaDoc className = getOptimizedInvocationClassName(clazz, field, get);
118       boolean makeInnerClass = true; //!Modifier.isPublic(field.getModifiers());
119

120       try
121       {
122          //In some cases we get a class frozen exception. I guess if the invocation class
123
//existed, method was unwrapped and the wrapped again
124
CtClass existing = pool.get(className);
125          if (existing.isFrozen())
126          {
127             existing.defrost();
128          }
129       }
130       catch (NotFoundException e)
131       {
132          //Ignore, we are creating the class the first time
133
}
134       
135       CtClass invocation = makeInvocationClass(pool, makeInnerClass, clazz, className, fieldInvocation);
136       invocation.stopPruning(true);
137    
138       boolean isStatic = javassist.Modifier.isStatic(field.getModifiers());
139       if (!isStatic)
140       {
141          CtField target = new CtField(field.getDeclaringClass(), "typedTargetObject", invocation);
142          target.setModifiers(Modifier.PUBLIC);
143          invocation.addField(target);
144       }
145       addCopy(pool, invocation, isStatic, get);
146       
147       setInvocationInvokeCode(invocation, field, get);
148    
149       TransformerCommon.compileOrLoadClass(field.getDeclaringClass(), invocation);
150    
151       //Return fully qualified name of class (may be an inner class)
152
return invocation.getName();
153    }
154
155    /**
156     * Creates the optimized invoke method.
157     * @param instrumentor TODO
158     * @param clazz the class declaring <code>field</code>
159     * @param field the field whose interception invoke method will be redefined.
160     * @param get indicates if the invoke is applied to the <code>field</code> read
161     * or to the <code>field</code> write joinpoint.
162     */

163    private static void setInvocationInvokeCode(CtClass invocation, CtField field, boolean get) throws CannotCompileException, NotFoundException
164    {
165       CtMethod in = invocation.getSuperclass().getDeclaredMethod("invokeTarget");
166    
167       String JavaDoc code = "{";
168    
169       String JavaDoc ref = Modifier.isStatic(field.getModifiers()) ? field.getDeclaringClass().getName() + "." : " typedTargetObject.";
170    
171       if (get)
172       {
173          code += "return ($w) " + ref + field.getName() + ";";
174       }
175       else
176       {
177          //TODO: Must be a better way to do the cast of the value that what is done by castInvocationValueToTypeString()?
178
CtClass type = field.getType();
179          code += ref + field.getName() + " = " + JavassistToReflect.castInvocationValueToTypeString(type) + " return null;";
180       }
181    
182       code += "}";
183       CtMethod invokeTarget =
184          CtNewMethod.make(in.getReturnType(),
185                           in.getName(),
186                           in.getParameterTypes(),
187                           in.getExceptionTypes(),
188                           code,
189                           invocation);
190       invokeTarget.setModifiers(in.getModifiers());
191       invocation.addMethod(invokeTarget);
192    }
193
194
195 }
196
Popular Tags