KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > jdk15 > dynamic > common > POJOWrappingInfo


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.test.aop.jdk15.dynamic.common;
23
24 import javassist.CannotCompileException;
25 import javassist.CtClass;
26 import javassist.CtField;
27 import javassist.CtMethod;
28 import javassist.NotFoundException;
29 import javassist.expr.ExprEditor;
30 import javassist.expr.FieldAccess;
31 import javassist.expr.MethodCall;
32 import javassist.expr.NewExpr;
33
34 import org.jboss.aop.ClassAdvisor;
35 import org.jboss.aop.instrument.ConstructorExecutionTransformer;
36 import org.jboss.aop.joinpoint.InvocationBase;
37 /**
38  * Class that represents the POJO joinpoints wrapping state in a specific moment
39  * of the test execution.
40  * @author Flavia Rainone
41  *
42  */

43 public class POJOWrappingInfo
44 {
45    /**
46     * Finds out if a method is wrapped.
47     */

48    private class POJOMethodIntrospector extends ExprEditor
49    {
50       private String JavaDoc notAdvisedMethod;
51       /**
52        * Constructor.
53        * @param notAdvisedMethod the not advised name of the method whose execution
54        * joinpoint must be found out.
55        */

56       public POJOMethodIntrospector(String JavaDoc notAdvisedMethod)
57       {
58          this.notAdvisedMethod = notAdvisedMethod;
59       }
60       public void edit(MethodCall methodCall) throws CannotCompileException
61       {
62          if (methodCall.where().getName().endsWith("$aop"))
63             return;
64          if (methodCall.getClassName().equals(getClass().getPackage().getName() + ".scenario.POJO") &&
65                methodCall.getMethodName().equals(notAdvisedMethod))
66          {
67             methodWrapped = true;
68          }
69       }
70    }
71    
72    /**
73     * Introspects a pojo client to find out if the constructor and field accesses
74     * joinpoints are wrapped.
75     */

76    private class POJOClientIntrospector extends ExprEditor
77    {
78       public void edit(NewExpr newExpr)
79       {
80          // pojo constructor is being invoked.
81
if (newExpr.getClassName().equals(getClass().getPackage().getName() + "scenario.POJO"))
82          {
83             constructorWrapped = false;
84          }
85       }
86       
87       public void edit(FieldAccess fieldAccess)
88       {
89          if (fieldAccess.getFieldName().equals("counter") &&
90                fieldAccess.getClassName().equals(getClass().getPackage().getName() + "scenario.POJO"))
91          {
92             // pojo field value is being read
93
if (fieldAccess.isReader())
94             {
95                fieldReadWrapped = false;
96             }
97             // pojo field value is being written
98
else
99             {
100                fieldWriteWrapped = false;
101             }
102          }
103       }
104    }
105    
106    /**
107     * Inspects a wrapper to find out if it is calling any class advisor method.
108     * @author fla
109     *
110     */

111    private class WrapperIntrospector extends ExprEditor
112    {
113       boolean wrapperEmpty = true; // indicates that the wrapper doesn't call interceptors
114

115       public void edit(MethodCall call)
116       {
117          if (call.getClassName().equals(ClassAdvisor.class.getName())
118          || call.getClassName().equals(InvocationBase.class.getName()))
119          {
120             wrapperEmpty = false;
121          }
122       }
123    }
124
125    private boolean constructorWrapped;
126    private boolean fieldReadWrapped;
127    private boolean fieldWriteWrapped;
128    private boolean methodWrapped;
129    public POJOWrappingInfo(){}
130    
131    /**
132     * Constructor. Retrieves the POJO wrapping state and store this information
133     * at the instance being created.
134     * @throws CannotCompileException
135     * @throws NotFoundException
136     */

137    public POJOWrappingInfo(CtClass pojoClass, CtField field, CtMethod method,
138          String JavaDoc notAdvisedMethod, CtClass pojoClient) throws CannotCompileException, NotFoundException
139    {
140       this.constructorWrapped = true;
141       this.fieldReadWrapped = true;
142       this.fieldWriteWrapped = true;
143       this.methodWrapped = false;
144       if (pojoClass.isFrozen())
145       {
146          pojoClass.defrost();
147       }
148       method.instrument(new POJOMethodIntrospector(notAdvisedMethod));
149       if (pojoClient.isFrozen())
150       {
151          pojoClient.defrost();
152       }
153       pojoClient.instrument(new POJOClientIntrospector());
154       if (this.constructorWrapped)
155       {
156          // check if the wrapper calls interceptors: if it does not, constructor
157
// is unwrapped after being previously wrapped
158
String JavaDoc constructorWrapper = ConstructorExecutionTransformer.constructorFactory(pojoClass.getSimpleName());
159          constructorWrapped = !isWrapperEmpty(pojoClass, constructorWrapper);
160       }
161       if (this.fieldReadWrapped)
162       {
163          // check if the wrapper calls interceptors: if it does not, field read
164
// is unwrapped after being previously wrapped
165
String JavaDoc fieldWrapper = field.getName() + "_r_" + ClassAdvisor.NOT_TRANSFORMABLE_SUFFIX;
166          fieldReadWrapped = !isWrapperEmpty(pojoClass, fieldWrapper);
167       }
168       if (this.fieldWriteWrapped)
169       {
170          // check if the wrapper calls interceptors: if it does not, field write
171
// is unwrapped after being previously wrapped
172
String JavaDoc fieldWrapper = field.getName() + "_w_" + ClassAdvisor.NOT_TRANSFORMABLE_SUFFIX;
173          fieldWriteWrapped = !isWrapperEmpty(pojoClass,fieldWrapper);
174       }
175    }
176
177    /**
178     * Checks if the wrapper calls advisor methods. If it does not,
179     * wrapper target (the joinpoint) is unwrapped.
180     * @param wrapperName the name of the POJO wrapper method.
181     * @return <code>true</code> if the wrapper is empty.
182     */

183    private boolean isWrapperEmpty(CtClass pojoClass, String JavaDoc wrapperName) throws CannotCompileException
184    {
185       CtMethod wrapper;
186       try {
187           wrapper = pojoClass.getDeclaredMethod(wrapperName);
188       } catch (NotFoundException e)
189       {
190          return true;
191       }
192       WrapperIntrospector introspector = new WrapperIntrospector();
193       wrapper.instrument(introspector);
194       return introspector.wrapperEmpty;
195    }
196    
197    /**
198     * Returns <code>true</code> if the pojo constructor execution joinpoint is wrapped.
199     */

200    public boolean isConstructorWrapped()
201    {
202       return this.constructorWrapped;
203    }
204    
205    /**
206     * Returns <code>true</code> if the pojo "counter" field read joinpoint is wrapped.
207     */

208    public boolean isFieldReadWrapped()
209    {
210       return this.fieldReadWrapped;
211    }
212
213    /**
214     * Returns <code>true</code> if the pojo "counter" field write joinpoint is wrapped.
215     */

216    public boolean isFieldWriteWrapped()
217    {
218       return this.fieldWriteWrapped;
219    }
220    
221    /**
222     * Returns <code>true</code> if the pojo "someMethod" method execution joinpoint is wrapped.
223     */

224    public boolean isMethodWrapped()
225    {
226       return this.methodWrapped;
227    }
228 }
Popular Tags