KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class OptimizedConstructionInvocations extends
43       OptimizedBehaviourInvocations
44 {
45
46    protected static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws Exception JavaDoc
47    {
48       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation");
49       CtMethod template = methodInvocation.getDeclaredMethod("copy");
50    
51    
52       String JavaDoc code =
53       "{ " +
54       " " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors, this.constructor); " +
55       " wrapper.metadata = this.metadata; " +
56       " wrapper.currentInterceptor = this.currentInterceptor; ";
57       for (int i = 0; i < params.length; i++)
58       {
59          code += " wrapper.arg" + i + " = this.arg" + i + "; ";
60       }
61       code += " return wrapper; }";
62    
63       CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation);
64       copy.setModifiers(template.getModifiers());
65       invocation.addMethod(copy);
66    
67    }
68
69    /**
70     * Returns the name of the optimized Invocation class.
71     * @param declaringClazz the class that contains the constructor.
72     * @param constructorIndex the index of the constructor.
73     * @return the name of the optimized Invocation class.
74     */

75    protected static String JavaDoc getOptimizedInvocationClassName(CtClass declaringClazz, int constructorIndex)
76    {
77       return declaringClazz.getName() + constructorIndex + "OptimizedConstructionInvocation";
78    }
79
80    protected static String JavaDoc createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws Exception JavaDoc
81    {
82       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
83       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation");
84       CtClass untransformable = pool.get("org.jboss.aop.instrument.Untransformable");
85    
86       String JavaDoc className = getOptimizedInvocationClassName(clazz, index);
87       boolean makeInnerClass = !Modifier.isPublic(con.getModifiers());
88    
89       CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation);
90       
91       CtConstructor template = null;
92       CtConstructor[] tcons = conInvocation.getDeclaredConstructors();
93       for (int i = 0; i < tcons.length; i++)
94       {
95          if (tcons[i].getParameterTypes().length == 2)
96          {
97             template = tcons[i];
98             break;
99          }
100       }
101       CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation);
102       invocation.addConstructor(icon);
103    
104       CtClass[] params = con.getParameterTypes();
105       for (int i = 0; i < params.length; i++)
106       {
107          CtField field = new CtField(params[i], "arg" + i, invocation);
108          field.setModifiers(Modifier.PUBLIC);
109          invocation.addField(field);
110       }
111    
112       addGetArguments(pool, invocation, con.getParameterTypes());
113       addCopy(pool, invocation, con.getParameterTypes());
114       // If compile time
115
TransformerCommon.compileOrLoadClass(con.getDeclaringClass(), invocation);
116    
117       //Return fully qualified name of class (may be an inner class)
118
return invocation.getName();
119    }
120
121 }
122
Popular Tags