KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConByConInfo;
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 ConstructorCalledByConstructorInvocation extends CallerInvocation
42 {
43    private static final long serialVersionUID = -1569257745454443521L;
44
45    protected Object JavaDoc[] arguments;
46    
47    //info fields
48
protected Constructor JavaDoc calling;
49    protected Constructor JavaDoc constructor;
50    protected Method JavaDoc wrappingMethod;
51    
52    public ConstructorCalledByConstructorInvocation(ConByConInfo info, Object JavaDoc[] args, Interceptor[] interceptors)
53    {
54       this(info.getAdvisor(), info.getCalling(), info.getConstructor(), info.getWrappingMethod(), args, interceptors);
55    }
56    
57    public ConstructorCalledByConstructorInvocation(ConByConInfo info, Interceptor[] interceptors)
58    {
59       this(info.getAdvisor(), info.getCalling(), info.getConstructor(), info.getWrappingMethod(), null, interceptors);
60    }
61    
62    public ConstructorCalledByConstructorInvocation(Advisor advisor, Constructor JavaDoc calling, Constructor JavaDoc constructor, Method JavaDoc wrappingMethod, Object JavaDoc[] args, Interceptor[] interceptors)
63    {
64       super(advisor, interceptors);
65       this.calling = calling;
66       this.constructor = constructor;
67       this.wrappingMethod = wrappingMethod;
68       this.arguments = args;
69    }
70    
71    public ConstructorCalledByConstructorInvocation(Interceptor[] interceptors)
72    {
73       super(interceptors);
74    }
75    
76    public Object JavaDoc[] getArguments()
77    {
78       return arguments;
79    }
80
81    public void setArguments(Object JavaDoc[] arguments)
82    {
83       this.arguments = arguments;
84    }
85
86    /**
87     *
88     * @return the constructor that is calling the called constructor
89     */

90    public Constructor JavaDoc getCallingConstructor()
91    {
92       return calling;
93    }
94    /**
95     *
96     * @return the constructor call being executed by the calling method
97     */

98    public Constructor JavaDoc getCalledConstructor() { return constructor; }
99
100    /**
101     * Is the called constructor aspectized? If so then there is a wrapping
102     * method that must be called.
103     * @return
104     */

105    public boolean isWrapped() { return wrappingMethod != null; }
106
107    /**
108     * Is the called constructor aspectized? If so then this method
109     * returns the method that wraps the constructor.
110     * @return
111     */

112    public Method JavaDoc getWrappingMethod() { return wrappingMethod; }
113
114    /**
115     * Invoke on the next interceptor in the chain. If this is already
116     * the end of the chain, reflection will call the constructor, field, or
117     * method you are invoking on.
118     */

119    public Object JavaDoc invokeNext() throws Throwable JavaDoc
120    {
121       if (interceptors != null && currentInterceptor < interceptors.length)
122       {
123          try
124          {
125             return interceptors[currentInterceptor++].invoke(this);
126          }
127          finally
128          {
129             // so that interceptors like clustering can reinvoke down the chain
130
currentInterceptor--;
131          }
132       }
133
134       return invokeTarget();
135    }
136    
137    /**
138     * Invokes the target joinpoint for this invocation skipping any subsequent
139     * interceptors in the chain.
140     */

141    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
142    {
143       if (wrappingMethod != null)
144       {
145          try
146          {
147             setTargetObject(wrappingMethod.invoke(null, arguments));
148             return getTargetObject();
149          }
150          catch (InvocationTargetException JavaDoc e)
151          {
152             throw e.getTargetException();
153          }
154       }
155       else
156       {
157          try
158          {
159             return constructor.newInstance(arguments);
160          }
161          catch (InstantiationException JavaDoc e)
162          {
163             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
164
}
165          catch (IllegalAccessException JavaDoc e)
166          {
167             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
168
}
169          catch (InvocationTargetException JavaDoc e)
170          {
171             throw e.getCause();
172          }
173       }
174    }
175
176    /**
177     * This method resolves metadata based on the context of the invocation.
178     * It iterates through its list of MetaDataResolvers to find out the
179     * value of the metadata desired.
180     *
181     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
182     * ClassAdvisor.getMethodMetaData (or field, or constructor)
183     * ClassAdvisor.getDefaultMetaData
184     */

185    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
186    {
187       Object JavaDoc val = super.getMetaData(group, attr);
188       if (val != null) return val;
189
190       // todo what to do for caller side metadata?
191
return null;
192    }
193
194    /**
195     * Get a wrapper invocation object that can insert a new chain of interceptors
196     * at runtime to the invocation flow. CFlow makes use of this.
197     * When the wrapper object finishes its invocation chain it delegates back to
198     * the wrapped invocation.
199     * @param newchain
200     * @return
201     */

202    public Invocation getWrapper(Interceptor[] newchain)
203    {
204       ConstructorCalledByConstructorInvocationWrapper wrapper = new ConstructorCalledByConstructorInvocationWrapper(this, newchain);
205       return wrapper;
206    }
207
208    /**
209     * Copies complete state of Invocation object.
210     * @return
211     */

212    public Invocation copy()
213    {
214       ConstructorCalledByConstructorInvocation wrapper = new ConstructorCalledByConstructorInvocation(advisor, calling, constructor, wrappingMethod, arguments, interceptors);
215       wrapper.setAdvisor(this.getAdvisor());
216       wrapper.setTargetObject(this.getTargetObject());
217       wrapper.currentInterceptor = this.currentInterceptor;
218       wrapper.instanceResolver = this.instanceResolver;
219       wrapper.metadata = this.metadata;
220       return wrapper;
221    }
222
223
224 }
225
Popular Tags