KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import org.jboss.aop.classpool.AOPClassPool;
30 import org.jboss.aop.standalone.Compiler;
31
32 import javassist.CannotCompileException;
33 import javassist.ClassPool;
34 import javassist.CtClass;
35 import javassist.CtConstructor;
36 import javassist.CtField;
37 import javassist.CtMethod;
38 import javassist.CtNewConstructor;
39 import javassist.CtNewMethod;
40 import javassist.Modifier;
41 import javassist.NotFoundException;
42
43 /**
44  * Comment
45  *
46  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
47  * @version $Revision$
48  */

49 public class OptimizedConstructorInvocations extends
50       OptimizedBehaviourInvocations
51 {
52    /**
53     * Returns the name of the optimized Invocation class.
54     * @param declaringClazz the class that contains the constructor.
55     * @param constructorIndex the index of the constructor.
56     * @return the name of the optimized Invocation class.
57     */

58    protected static String JavaDoc getOptimizedInvocationClassName(CtClass declaringClazz, int constructorIndex)
59    {
60       return declaringClazz.getName() + "_" + constructorIndex + "OptimizedConstructorInvocation";
61    }
62
63    protected static String JavaDoc createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws NotFoundException, CannotCompileException
64    {
65       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
66       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation");
67       CtClass untransformable = pool.get("org.jboss.aop.instrument.Untransformable");
68
69       String JavaDoc className = getOptimizedInvocationClassName(clazz, index);
70       boolean makeInnerClass = !Modifier.isPublic(con.getModifiers());
71
72       CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation);
73       
74       CtConstructor template = conInvocation.getDeclaredConstructors()[0];
75       CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation);
76       invocation.addConstructor(icon);
77
78       CtClass[] params = con.getParameterTypes();
79       for (int i = 0; i < params.length; i++)
80       {
81          CtField field = new CtField(params[i], "arg" + i, invocation);
82          field.setModifiers(Modifier.PUBLIC);
83          invocation.addField(field);
84       }
85
86       CtMethod in = conInvocation.getDeclaredMethod("invokeTarget");
87
88       StringBuffer JavaDoc code = new StringBuffer JavaDoc("{") ;
89
90       code.append("setTargetObject( new ").append(con.getDeclaringClass().getName()).append("(");
91       for (int i = 0; i < params.length; i++)
92       {
93          if (i > 0)
94             code.append(", ");
95           code.append("arg").append(i);
96       }
97       code.append("));");
98       code.append("return getTargetObject();");
99       code.append("}");
100
101       CtMethod invokeTarget = null;
102       try
103       {
104          invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code.toString(), invocation);
105       }
106       catch (CannotCompileException e)
107       {
108          System.out.println(code.toString());
109          throw e;
110       }
111       invocation.addMethod(invokeTarget);
112       invokeTarget.setModifiers(in.getModifiers());
113       addGetArguments(pool, invocation, con.getParameterTypes());
114       addCopy(pool, invocation, con.getParameterTypes());
115       
116       TransformerCommon.compileOrLoadClass(clazz, invocation);
117       
118       //Return fully qualified name of class (may be an inner class)
119
return invocation.getName();
120    }
121    
122    private static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws CannotCompileException, NotFoundException
123    {
124       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation");
125       CtMethod template = methodInvocation.getDeclaredMethod("copy");
126
127       String JavaDoc code =
128       "{ " +
129       " " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors); " +
130       " wrapper.constructor = this.constructor; " +
131       " wrapper.arguments = this.arguments; " +
132       " wrapper.metadata = this.metadata; " +
133       " wrapper.currentInterceptor = this.currentInterceptor; ";
134       for (int i = 0; i < params.length; i++)
135       {
136          code += " wrapper.arg" + i + " = this.arg" + i + "; ";
137       }
138       code += " return wrapper; }";
139
140       CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation);
141       copy.setModifiers(template.getModifiers());
142       invocation.addMethod(copy);
143
144    }
145
146 }
147
Popular Tags