KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodByConInfo;
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 MethodCalledByConstructorInvocation extends CallerInvocation
42 {
43    private static final long serialVersionUID = -1903092605231217830L;
44    
45    //info fields
46
protected Constructor JavaDoc calling;
47    protected Method JavaDoc method;
48    protected Object JavaDoc[] arguments = null;
49
50    public MethodCalledByConstructorInvocation(MethodByConInfo info, Object JavaDoc target, Object JavaDoc[] args, Interceptor[] interceptors)
51    {
52       this(info.getAdvisor(), info.getCalling(), info.getMethod(), target, args, interceptors);
53    }
54
55    public MethodCalledByConstructorInvocation(MethodByConInfo info, Object JavaDoc target, Interceptor[] interceptors)
56    {
57       this(info.getAdvisor(), info.getCalling(), info.getMethod(), target, null, interceptors);
58    }
59    
60    public MethodCalledByConstructorInvocation(Advisor advisor, Constructor JavaDoc calling, Method JavaDoc method, Object JavaDoc target, Object JavaDoc[] args, Interceptor[] interceptors)
61    {
62       super(advisor, interceptors);
63       this.calling = calling;
64       this.method = method;
65       setTargetObject(target);
66       this.arguments = args;
67    }
68
69    public MethodCalledByConstructorInvocation(Interceptor[] interceptors)
70    {
71       super(interceptors);
72    }
73
74    /**
75     * Invoke on the next interceptor in the chain. If this is already
76     * the end of the chain, reflection will call the constructor, field, or
77     * method you are invoking on.
78     */

79    public Object JavaDoc invokeNext() throws Throwable JavaDoc
80    {
81       if (interceptors != null && currentInterceptor < interceptors.length)
82       {
83          try
84          {
85             return interceptors[currentInterceptor++].invoke(this);
86          }
87          finally
88          {
89             // so that interceptors like clustering can reinvoke down the chain
90
currentInterceptor--;
91          }
92       }
93
94       return invokeTarget();
95    }
96    
97    /**
98     * Invokes the target joinpoint for this invocation skipping any subsequent
99     * interceptors in the chain.
100     */

101    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
102    {
103       try
104       {
105          return method.invoke(getTargetObject(), arguments);
106       }
107       catch (InvocationTargetException JavaDoc e)
108       {
109          throw e.getTargetException();
110       }
111    }
112    
113    /**
114     * This method resolves metadata based on the context of the invocation.
115     * It iterates through its list of MetaDataResolvers to find out the
116     * value of the metadata desired.
117     *
118     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
119     * ClassAdvisor.getMethodMetaData (or field, or constructor)
120     * ClassAdvisor.getDefaultMetaData
121     */

122    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
123    {
124       Object JavaDoc val = super.getMetaData(group, attr);
125       if (val != null) return val;
126
127       // todo what to do for caller side metadata?
128
return null;
129    }
130
131    /**
132     * Get a wrapper invocation object that can insert a new chain of interceptors
133     * at runtime to the invocation flow. CFlow makes use of this.
134     * When the wrapper object finishes its invocation chain it delegates back to
135     * the wrapped invocation.
136     * @param newchain
137     * @return
138     */

139    public Invocation getWrapper(Interceptor[] newchain)
140    {
141       MethodCalledByConstructorInvocationWrapper wrapper = new MethodCalledByConstructorInvocationWrapper(this, newchain);
142       return wrapper;
143    }
144
145    /**
146     * Copies complete state of Invocation object.
147     * @return
148     */

149    public Invocation copy()
150    {
151       MethodCalledByConstructorInvocation wrapper = new MethodCalledByConstructorInvocation(
152             advisor, calling, method, targetObject, arguments, interceptors);
153       wrapper.currentInterceptor = this.currentInterceptor;
154       wrapper.metadata = this.metadata;
155       wrapper.instanceResolver = this.instanceResolver;
156       return wrapper;
157    }
158
159    /**
160     * What are the arguments of the method call
161     * The are expressed as an Object[] array.
162     *
163     * @return the arguments of the method call
164     */

165    public Object JavaDoc[] getArguments()
166    {
167       return arguments;
168    }
169
170    /**
171     * Change the arguments of the method call
172     * @param arguments
173     */

174    public void setArguments(Object JavaDoc[] arguments)
175    {
176       this.arguments = arguments;
177    }
178
179    /**
180     * The constructor that is calling the method
181     *
182     * @return The constructor that is calling the method
183     */

184    public Constructor JavaDoc getCalling()
185    {
186       return calling;
187    }
188
189    /**
190     *
191     * @return the method that is being called
192     */

193    public Method JavaDoc getCalledMethod()
194    {
195       return method;
196    }
197 }
198
Popular Tags