KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class OptimizedConstructorExecutionTransformer extends
37       ConstructorExecutionTransformer
38 {
39    public OptimizedConstructorExecutionTransformer(Instrumentor instrumentor)
40    {
41       super(instrumentor);
42    }
43    
44    protected void createWrapper(ConstructorTransformation trans)throws NotFoundException, CannotCompileException
45     {
46       String JavaDoc invocationClass = OptimizedConstructorInvocations.getOptimizedInvocationClassName(trans.getClazz(), trans.getIndex());
47       if(!Modifier.isPublic(trans.getConstructor().getModifiers())) {
48          invocationClass = invocationClass.substring(invocationClass.lastIndexOf('.') + 1);
49          invocationClass = trans.getClazz().getName() + "$" + invocationClass;
50       }
51
52       String JavaDoc infoName = getConstructorInfoFieldName(trans.getSimpleName(), trans.getIndex());
53       String JavaDoc code =
54          "{ " +
55          " " + constructorInfoFromWeakReference("info", infoName) +
56          " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors(); " +
57          " if (interceptors != (org.jboss.aop.advice.Interceptor[])null) " +
58          " { " +
59          " " + invocationClass + " invocation = new " + invocationClass + "(interceptors); " +
60          " invocation.setConstructor(info.getConstructor()); " +
61          OptimizedBehaviourInvocations.setArguments(trans.getConstructor().getParameterTypes().length) +
62          " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " +
63          " return ($r)invocation.invokeNext(); " +
64          " } " +
65          " return new " + trans.getClazz().getName() + "($$); " +
66          "}";
67       
68       // fill wrapped code only after the constructor invocations have been converted
69
// by wrapper calls
70
codifier.addPendingCode(trans.getWrapperMethod(), code);
71    }
72    
73    protected void generateConstructorInfoField(CtClass clazz, CtConstructor constructor, int index) throws CannotCompileException, NotFoundException
74    {
75       super.generateConstructorInfoField(clazz, constructor, index);
76       OptimizedConstructorInvocations.createOptimizedInvocationClass(getInstrumentor(), constructor.getDeclaringClass(), constructor, index);
77    }
78
79 }
80
Popular Tags