KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class GeneratedAdvisorConstructorExecutionTransformer extends
42       ConstructorExecutionTransformer
43 {
44
45    public GeneratedAdvisorConstructorExecutionTransformer(Instrumentor instrumentor)
46    {
47       super(instrumentor);
48    }
49
50    protected boolean addInfoAsWeakReference()
51    {
52       return false;
53    }
54
55    protected void generateConstructorInfoField(CtClass clazz, CtConstructor constructor, int index) throws CannotCompileException, NotFoundException
56    {
57       String JavaDoc ciname = addConstructorInfoField(
58             Modifier.PROTECTED,
59             ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor(),
60             getConstructorInfoFieldName(clazz.getSimpleName(), index));
61
62       addJoinpoint(clazz, constructor, ciname, index);
63
64       final long wrapperHash = JavassistMethodHashing.methodHash(
65             clazz.getDeclaredMethod(
66                   constructorFactory(clazz.getSimpleName()),
67                   constructor.getParameterTypes()));
68       final long constructorHash = JavassistMethodHashing.constructorHash(constructor);
69
70       ((GeneratedAdvisorInstrumentor)getInstrumentor()).initialiseConstructorInfoField(ciname, index, constructorHash, wrapperHash);
71    }
72
73    private void addJoinpoint(CtClass clazz, CtConstructor constructor, String JavaDoc ciname, int index)throws CannotCompileException, NotFoundException
74    {
75       CtClass joinpoint = createJoinpointClass(clazz, constructor, ciname, index);
76       CtClass genadvisor = ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor();
77       CtField field = new CtField(
78             joinpoint,
79             ConstructorJoinPointGenerator.getInfoFieldName(clazz.getSimpleName(), index),
80             genadvisor);
81       field.setModifiers(Modifier.PROTECTED);
82       genadvisor.addField(field);
83    }
84
85    private CtClass createJoinpointClass(CtClass clazz, CtConstructor constructor, String JavaDoc ciname, int index) throws CannotCompileException, NotFoundException
86    {
87       return ConstructorJoinPointGenerator.createJoinpointBaseClass(
88             (GeneratedAdvisorInstrumentor)getInstrumentor(),
89             clazz,
90             constructor,
91             ciname,
92             index);
93    }
94
95    protected void initialiseWrapper(int mod, CtConstructor constructor, int index) throws NotFoundException, CannotCompileException
96    {
97       GeneratedAdvisorInstrumentor instrumentor = (GeneratedAdvisorInstrumentor)getInstrumentor();
98       CtClass genadvisor = getGenAdvisor();
99       CtClass clazz = constructor.getDeclaringClass();
100
101       CtClass[] exceptions = constructor.getExceptionTypes();
102       CtClass type = constructor.getDeclaringClass();
103
104       CtMethod innerWmethod = CtNewMethod.make(
105             clazz,
106             getInnerWrapperMethodName(constructor),
107             constructor.getParameterTypes(),
108             exceptions,
109             null,
110             genadvisor);
111       innerWmethod.setModifiers(Modifier.clear(mod, Modifier.STATIC));
112       setTemporaryWrapperCode(type, innerWmethod);
113       genadvisor.addMethod(innerWmethod);
114
115       CtMethod wmethod = CtNewMethod.make(
116             clazz,
117             constructorFactory(clazz.getSimpleName()),
118             constructor.getParameterTypes(),
119             exceptions,
120             null,
121             clazz);
122       wmethod.setModifiers(mod);
123       wmethod.setBody("{return ((" + instrumentor.getAdvisorFQN(clazz) +
124             ")" + Instrumentor.HELPER_FIELD_NAME + ")." + getInnerWrapperMethodName(constructor) + "($$);}");
125       clazz.addMethod(wmethod);
126
127       // prepare ForWrapping
128
getWrapper().prepareForWrapping(constructor, CONSTRUCTOR_STATUS);
129    }
130
131    protected void setEmptyWrapperCode(CtConstructor constructor)throws NotFoundException
132    {
133       CtClass clazz = constructor.getDeclaringClass();
134
135
136       String JavaDoc innerCode =
137          "{ " +
138          " return new " + clazz.getName() + "($$); " +
139          "}";
140       try
141       {
142          CtMethod innerWrapperMethod = getInnerWrapperMethod(constructor);
143         innerWrapperMethod.setBody(innerCode);
144       }
145       catch (CannotCompileException e)
146       {
147         System.out.println(innerCode);
148         throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
149
}
150
151       try
152       {
153          CtMethod outerWrappedMethod = getWrapperMethod(constructor);
154          outerWrappedMethod.setBody(outerDelegatingBody(constructor));
155       }
156       catch(CannotCompileException e)
157       {
158          System.out.println(outerDelegatingBody(constructor));
159          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
160
}
161
162    }
163
164    protected void setEmptyWrapperCodeLater(CtConstructor constructor) throws NotFoundException
165    {
166       CtMethod outerWrapperMethod = getWrapperMethod(constructor);
167       this.codifier.addPendingCode(outerWrapperMethod, outerDelegatingBody(constructor));
168
169       CtMethod innerWrapperMethod = getInnerWrapperMethod(constructor);
170       String JavaDoc innerCode =
171          "{ " +
172          " return new " + constructor.getDeclaringClass().getName() + "($$); " +
173          "}";
174       this.codifier.addPendingCode(innerWrapperMethod, innerCode);
175    }
176
177    private CtMethod getInnerWrapperMethod(CtConstructor constructor)throws NotFoundException
178    {
179       CtClass genadvisor = getGenAdvisor();
180       return genadvisor.getDeclaredMethod(
181             getInnerWrapperMethodName(constructor),
182             constructor.getParameterTypes());
183
184    }
185
186    private String JavaDoc getInnerWrapperMethodName(CtConstructor constructor)
187    {
188       return constructorFactory(((GeneratedAdvisorInstrumentor)getInstrumentor()).getAdvisorName(constructor.getDeclaringClass()));
189    }
190
191    private String JavaDoc outerDelegatingBody(CtConstructor constructor)
192    {
193       return "{return ((" +
194       ((GeneratedAdvisorInstrumentor)getInstrumentor()).getAdvisorFQN(constructor.getDeclaringClass()) +
195       ")" + Instrumentor.HELPER_FIELD_NAME + ")." + getInnerWrapperMethodName(constructor) + "($$);}";
196    }
197
198    private CtClass getGenAdvisor()
199    {
200       return ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor();
201    }
202
203    protected void createWrapper(ConstructorTransformation trans)
204          throws CannotCompileException, NotFoundException
205    {
206       CtMethod outerWrapper = getWrapperMethod(trans.getConstructor());
207       outerWrapper.setBody(outerDelegatingBody(trans.getConstructor()));
208
209       CtMethod innerWrapper = getInnerWrapperMethod(trans.getConstructor());
210
211       String JavaDoc infoName = ConstructorJoinPointGenerator.getInfoFieldName(
212             trans.getConstructor().getDeclaringClass().getSimpleName(),
213             trans.getIndex());
214
215       String JavaDoc generatorName = ConstructorJoinPointGenerator.getJoinPointGeneratorFieldName(
216             trans.getConstructor().getDeclaringClass().getSimpleName(),
217             trans.getIndex());
218
219       String JavaDoc code =
220          "{" +
221          " if (" + infoName + " == null && " + generatorName + " != null)" +
222          " {" +
223          " " + generatorName + "." + JoinPointGenerator.GENERATE_JOINPOINT_CLASS + "();" +
224          " }" +
225          " if (" + infoName + " == null)" +
226          " { " +
227          " return new " + trans.getClassName() + "($$); " +
228          " }" +
229          " else" +
230          " {" +
231          " return " + infoName + "." + JoinPointGenerator.INVOKE_JOINPOINT + "($$);" +
232          " }" +
233          "}";
234
235       codifier.addPendingCode(innerWrapper, code);
236    }
237
238
239
240
241 }
242
Popular Tags