KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > aop > Invocation


1 package org.sapia.soto.aop;
2
3 import net.sf.cglib.proxy.MethodProxy;
4
5 import java.lang.reflect.Method JavaDoc;
6
7
8 /**
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class Invocation {
17   private boolean _invokeNext;
18   private Method JavaDoc _method;
19   private MethodProxy _proxy;
20   private Object JavaDoc _instance;
21   private Object JavaDoc _returnVal;
22   private Object JavaDoc[] _args;
23   boolean _invoked;
24
25   /**
26    * Constructor for Invocation.
27    */

28   Invocation(Object JavaDoc instance, Method JavaDoc meth, MethodProxy proxy, Object JavaDoc[] args) {
29     _instance = instance;
30     _method = meth;
31     _proxy = proxy;
32     _args = args;
33   }
34
35   /**
36    * Allows to abort the method invocation that is eventually executed by this
37    * instance. The caller must in turn pass a return value that will eventually
38    * be returned to the client.
39    * <p>
40    * This functionality has been implemented in order to bypass a method's
41    * invocation, if required. It should be used with care.
42    * <p>
43    * This method must be called within <code>BeforeAdvice</code> instances only - otherwise
44    * does not make sense.
45    *
46    * @param returnVal the "fake" return value that is eventually returned to client
47    * code.
48    *
49    */

50   public void abortInvoke(Object JavaDoc returnVal) {
51     _invoked = true;
52     _returnVal = returnVal;
53   }
54
55   // /**
56
// * Signals that the invocation should be stopped.
57
// *
58
// * @see #enable()
59
// * @see #hasNext()
60
// */
61
// public void disable(){
62
// _invokeNext = false;
63
// }
64
//
65
// /**
66
// * Returns true if the next advices in the chain should be invoked.
67
// *
68
// * @see #disable()
69
// * @see #enable()
70
// */
71
// public boolean hasNext(){
72
// return _invokeNext;
73
// }
74
//
75
// /**
76
// * Enables the invocation of the "next" advices in the chain.
77
// *
78
// * @see #disable()
79
// * @see #hasNext()
80
// */
81
// public void enable(){
82
// _invokeNext = true;
83
// }
84

85   /**
86    * Returns the name of the method to which this instance corresponds.
87    *
88    * @return a method name.
89    */

90   public String JavaDoc getMethodName() {
91     return _method.getName();
92   }
93
94   /**
95    * Returns the types of the parameters that this method takes.
96    *
97    * @return a <code>Class</code> array representing this instance's method
98    * parameter types.
99    */

100   public Class JavaDoc[] getParameterTypes() {
101     return _method.getParameterTypes();
102   }
103
104   /**
105    * Return the return type of the method to which this instance corresponds.
106    *
107    * @return a <code>Class</code> object corresponding to this instance's method return type.
108    */

109   public Class JavaDoc getReturnType() {
110     return _method.getReturnType();
111   }
112
113   /**
114    * Returns the value that was returned by the invocation of the method to which
115    * this instance corresponds.
116    *
117    * @return an <code>Object</code>
118    * @throws IllegalStateException if the method has not yet been invoked.
119    */

120   public Object JavaDoc getReturnValue() throws IllegalStateException JavaDoc {
121     if (!_invoked) {
122       throw new IllegalStateException JavaDoc("Method " + _method + " not yet invoked");
123     }
124
125     return _returnVal;
126   }
127
128   /**
129    * Invokes the method to which this instance corresponds.
130    *
131    * @throws a <code>Throwable<code> generated by the underlying method if a
132    * problem occurs.
133    */

134   public Object JavaDoc invoke() throws Throwable JavaDoc {
135     if (_invoked) {
136       throw new IllegalStateException JavaDoc("Method " + _method +
137         "was already invoked");
138     }
139
140     _invoked = true;
141
142     return _returnVal = _proxy.invokeSuper(_instance, _args);
143   }
144
145   /**
146    * Returns <code>true</code> if the method to which this instance corresponds has been invoked.
147    *
148    * @return <code>true</code> if the method to which this instance corresponds has been invoked.
149    */

150   public boolean wasInvoked() {
151     return _invoked;
152   }
153
154   /**
155    * Returns the instance on which this invocation is to be performed.
156    *
157    * @return an <code>Object</code>.
158    */

159   public Object JavaDoc getTarget() {
160     return _instance;
161   }
162
163   /**
164    * Returns the arguments to be passed to the method that this instance
165    * corresponds to.
166    *
167    * @return an array of <code>Object</code>.
168    */

169   public Object JavaDoc[] getArgs() {
170     return _args;
171   }
172
173   /**
174    * Returns the method object that this invocation corresponds to.
175    *
176    * @return a <code>Method</code>
177    */

178   Method JavaDoc getMethod() {
179     return _method;
180   }
181
182   void setReturnValue(Object JavaDoc val) {
183     if (!_invoked) {
184       throw new IllegalStateException JavaDoc("Method " + _method + " not yet invoked");
185     }
186   }
187 }
188
Popular Tags