KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > joinpoint > ConstructorCalledByMethodInvocation


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.joinpoint;
23
24 import org.jboss.aop.Advisor;
25 import org.jboss.aop.ConByMethodInfo;
26 import org.jboss.aop.advice.Interceptor;
27
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32
33 /**
34  * This is a helper wrapper class for an Invocation object.
35  * It is used to add or get values or metadata that pertains to
36  * an AOP method invocation.
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 46058 $
40  */

41 public class ConstructorCalledByMethodInvocation extends CallerInvocation
42 {
43    private static final long serialVersionUID = -3269308917757322223L;
44
45    protected Object JavaDoc[] arguments = null;
46
47    //info fields
48
protected Class JavaDoc callingClass;
49    protected Method JavaDoc callingMethod;
50    protected Constructor JavaDoc constructor;
51    protected Method JavaDoc wrappingMethod;
52    
53    protected Object JavaDoc callingObject;
54
55    public ConstructorCalledByMethodInvocation(ConByMethodInfo info, Object JavaDoc callingObject, Object JavaDoc[] args, Interceptor[] interceptors)
56    {
57       this(info.getAdvisor(), info.getCallingClass(), info.getCallingMethod(), info.getConstructor(), info.getWrappingMethod(), callingObject, args, interceptors);
58    }
59
60    public ConstructorCalledByMethodInvocation(ConByMethodInfo info, Object JavaDoc callingObject, Interceptor[] interceptors)
61    {
62       this(info.getAdvisor(), info.getCallingClass(), info.getCallingMethod(), info.getConstructor(), info.getWrappingMethod(), callingObject, null, interceptors);
63    }
64    
65    public ConstructorCalledByMethodInvocation(Advisor advisor, Class JavaDoc callingClass,
66          Method JavaDoc callingMethod, Constructor JavaDoc constructor, Method JavaDoc wrappingMethod, Object JavaDoc callingObject, Object JavaDoc[] args, Interceptor[] interceptors)
67    {
68       super(advisor, interceptors);
69       this.callingClass = callingClass;
70       this.callingMethod = callingMethod;
71       this.constructor = constructor;
72       this.wrappingMethod = wrappingMethod;
73       this.callingObject = callingObject;
74       this.arguments = args;
75    }
76
77    public ConstructorCalledByMethodInvocation(Interceptor[] interceptors)
78    {
79       super(interceptors);
80    }
81
82    /**
83     * @return the arguments of the called constructor
84     */

85    public Object JavaDoc[] getArguments()
86    {
87       return arguments;
88    }
89
90    /**
91     * change the arguments to the called constructor
92     *
93     * @param arguments
94     */

95    public void setArguments(Object JavaDoc[] arguments)
96    {
97       this.arguments = arguments;
98    }
99
100    /**
101     * @return The class that is making the call on the constructor
102     */

103    public Class JavaDoc getCallingClass()
104    {
105       return callingClass;
106    }
107
108    /**
109     * @return The method that is making the call on the constructor
110     */

111    public Method JavaDoc getCallingMethod()
112    {
113       return callingMethod;
114    }
115
116    /**
117     * @return the constructor call being executed by the calling method
118     */

119    public Constructor JavaDoc getCalledConstructor()
120    {
121       return constructor;
122    }
123
124    /**
125     * Is the called constructor aspectized? If so then there is a wrapping
126     * method that must be called.
127     *
128     * @return
129     */

130    public boolean isWrapped()
131    {
132       return wrappingMethod != null;
133    }
134
135    /**
136     * Is the called constructor aspectized? If so then this method
137     * returns the method that wraps the constructor.
138     *
139     * @return
140     */

141    public Method JavaDoc getWrappingMethod()
142    {
143       return wrappingMethod;
144    }
145
146    /**
147     * Invoke on the next interceptor in the chain. If this is already
148     * the end of the chain, reflection will call the constructor, field, or
149     * method you are invoking on.
150     */

151    public Object JavaDoc invokeNext() throws Throwable JavaDoc
152    {
153       if (interceptors != null && currentInterceptor < interceptors.length)
154       {
155          try
156          {
157             return interceptors[currentInterceptor++].invoke(this);
158          }
159          finally
160          {
161             // so that interceptors like clustering can reinvoke down the chain
162
currentInterceptor--;
163          }
164       }
165
166       return invokeTarget();
167    }
168
169    /**
170     * Invokes the target joinpoint for this invocation skipping any subsequent
171     * interceptors in the chain.
172     */

173    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
174    {
175       if (wrappingMethod != null)
176       {
177          try
178          {
179             setTargetObject(wrappingMethod.invoke(null, arguments));
180             return getTargetObject();
181          }
182          catch (InvocationTargetException JavaDoc e)
183          {
184             throw e.getTargetException();
185          }
186       }
187       else
188       {
189          try
190          {
191             return constructor.newInstance(arguments);
192          }
193          catch (InstantiationException JavaDoc e)
194          {
195             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
196
}
197          catch (IllegalAccessException JavaDoc e)
198          {
199             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
200
}
201          catch (InvocationTargetException JavaDoc e)
202          {
203             throw e.getCause();
204          }
205       }
206    }
207
208    /**
209     * This method resolves metadata based on the context of the invocation.
210     * It iterates through its list of MetaDataResolvers to find out the
211     * value of the metadata desired.
212     * <p/>
213     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
214     * ClassAdvisor.getMethodMetaData (or field, or constructor)
215     * ClassAdvisor.getDefaultMetaData
216     */

217    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
218    {
219       Object JavaDoc val = super.getMetaData(group, attr);
220       if (val != null) return val;
221
222       // todo what to do for caller side metadata?
223
return null;
224    }
225
226    /**
227     * Get a wrapper invocation object that can insert a new chain of interceptors
228     * at runtime to the invocation flow. CFlow makes use of this.
229     * When the wrapper object finishes its invocation chain it delegates back to
230     * the wrapped invocation.
231     *
232     * @param newchain
233     * @return
234     */

235    public Invocation getWrapper(Interceptor[] newchain)
236    {
237       ConstructorCalledByMethodInvocationWrapper wrapper = new ConstructorCalledByMethodInvocationWrapper(this, newchain);
238       return wrapper;
239    }
240
241    /**
242     * Copies complete state of Invocation object.
243     *
244     * @return
245     */

246    public Invocation copy()
247    {
248       ConstructorCalledByMethodInvocation wrapper = new ConstructorCalledByMethodInvocation(advisor, callingClass,
249             callingMethod, constructor, wrappingMethod, callingObject, arguments, interceptors);
250       wrapper.setTargetObject(this.getTargetObject());
251       wrapper.metadata = this.metadata;
252       wrapper.instanceResolver = this.instanceResolver;
253       wrapper.currentInterceptor = this.currentInterceptor;
254       return wrapper;
255    }
256
257    public Object JavaDoc getCallingObject()
258    {
259       return callingObject;
260    }
261 }
262
Popular Tags