KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > MethodInvocationProceedingJoinPoint


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.aspectj;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 import org.aspectj.lang.JoinPoint;
22 import org.aspectj.lang.ProceedingJoinPoint;
23 import org.aspectj.lang.Signature;
24 import org.aspectj.lang.reflect.MethodSignature;
25 import org.aspectj.lang.reflect.SourceLocation;
26 import org.aspectj.runtime.internal.AroundClosure;
27
28 import org.springframework.aop.ProxyMethodInvocation;
29 import org.springframework.util.Assert;
30
31 /**
32  * Implementation of AspectJ ProceedingJoinPoint interface
33  * wrapping an AOP Alliance MethodInvocation.
34  *
35  * <p><b>Note</b>: the <code>getThis()</code> method returns the current Spring AOP proxy.
36  * The <code>getTarget()</code> method returns the current Spring AOP target (which may be
37  * <code>null</code> if there is no target), and is a plain POJO without any advice.
38  * <b>If you want to call the object and have the advice take effect, use
39  * <code>getThis()</code>.</b> A common example is casting the object to an
40  * introduced interface in the implementation of an introduction.
41  *
42  * <p>Of course there is no such distinction between target and proxy in AspectJ.
43  *
44  * @author Rod Johnson
45  * @author Juergen Hoeller
46  * @since 2.0
47  */

48 public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint, JoinPoint.StaticPart {
49     
50     private final ProxyMethodInvocation methodInvocation;
51
52     private Object JavaDoc[] defensiveCopyOfArgs;
53
54     /** Lazily initialized signature object */
55     private Signature signature;
56
57     /** Lazily initialized source location object */
58     private SourceLocation sourceLocation;
59
60
61     /**
62      * Create a new MethodInvocationProceedingJoinPoint, wrapping the given
63      * Spring ProxyMethodInvocation object.
64      * @param methodInvocation the Spring ProxyMethodInvocation object
65      */

66     public MethodInvocationProceedingJoinPoint(ProxyMethodInvocation methodInvocation) {
67         Assert.notNull(methodInvocation, "MethodInvocation must not be null");
68         this.methodInvocation = methodInvocation;
69     }
70
71     public void set$AroundClosure(AroundClosure aroundClosure) {
72         throw new UnsupportedOperationException JavaDoc();
73     }
74
75     public Object JavaDoc proceed() throws Throwable JavaDoc {
76         return this.methodInvocation.invocableClone().proceed();
77     }
78
79     public Object JavaDoc proceed(Object JavaDoc[] arguments) throws Throwable JavaDoc {
80         return this.methodInvocation.invocableClone(arguments).proceed();
81     }
82
83     /**
84      * Returns the Spring AOP proxy. Cannot be <code>null</code>.
85      */

86     public Object JavaDoc getThis() {
87         return this.methodInvocation.getProxy();
88     }
89
90     /**
91      * Returns the Spring AOP target. May be <code>null</code> if there is no target.
92      */

93     public Object JavaDoc getTarget() {
94         return this.methodInvocation.getThis();
95     }
96
97     public Object JavaDoc[] getArgs() {
98         if (this.defensiveCopyOfArgs == null) {
99             Object JavaDoc[] argsSource = this.methodInvocation.getArguments();
100             this.defensiveCopyOfArgs = new Object JavaDoc[argsSource.length];
101             System.arraycopy(argsSource, 0, this.defensiveCopyOfArgs, 0, argsSource.length);
102         }
103         return this.defensiveCopyOfArgs;
104     }
105
106     public Signature getSignature() {
107         if (this.signature == null) {
108             this.signature = new MethodSignatureImpl();
109         }
110         return signature;
111     }
112
113     public SourceLocation getSourceLocation() {
114         if (this.sourceLocation == null) {
115             this.sourceLocation = new SourceLocationImpl();
116         }
117         return this.sourceLocation;
118     }
119
120     public String JavaDoc getKind() {
121         return ProceedingJoinPoint.METHOD_EXECUTION;
122     }
123
124     public JoinPoint.StaticPart getStaticPart() {
125         return this;
126     }
127     
128
129     public String JavaDoc toShortString() {
130         return "execution(" + this.methodInvocation.getMethod().getName() + ")";
131     }
132
133     public String JavaDoc toLongString() {
134         return getClass().getName() + ": execution: [" + this.methodInvocation + "]";
135     }
136
137     public String JavaDoc toString() {
138         return getClass().getName() + ": " + toShortString();
139     }
140
141
142     /**
143      * Lazily initialized MethodSignature.
144      */

145     private class MethodSignatureImpl implements Signature, MethodSignature {
146
147         public String JavaDoc toShortString() {
148             return methodInvocation.getMethod().getName();
149         }
150
151         public String JavaDoc toLongString() {
152             return methodInvocation.getMethod().toString();
153         }
154
155         public String JavaDoc getName() {
156             return methodInvocation.getMethod().getName();
157         }
158
159         public int getModifiers() {
160             return methodInvocation.getMethod().getModifiers();
161         }
162
163         public Class JavaDoc getDeclaringType() {
164             return methodInvocation.getMethod().getDeclaringClass();
165         }
166
167         public String JavaDoc getDeclaringTypeName() {
168             return methodInvocation.getMethod().getDeclaringClass().getName();
169         }
170
171         public Class JavaDoc getReturnType() {
172             return methodInvocation.getMethod().getReturnType();
173         }
174
175         public Method JavaDoc getMethod() {
176             return methodInvocation.getMethod();
177         }
178
179         public Class JavaDoc[] getParameterTypes() {
180             return methodInvocation.getMethod().getParameterTypes();
181         }
182
183         public String JavaDoc[] getParameterNames() {
184             // TODO consider allowing use of ParameterNameDiscoverer, or tying into
185
// parameter names exposed for argument binding...
186
throw new UnsupportedOperationException JavaDoc(
187                     "Parameter names cannot be determined unless compiled by AspectJ compiler");
188         }
189
190         public Class JavaDoc[] getExceptionTypes() {
191             return methodInvocation.getMethod().getExceptionTypes();
192         }
193     }
194
195
196     /**
197      * Lazily initialized SourceLocation.
198      */

199     private class SourceLocationImpl implements SourceLocation {
200
201         public Class JavaDoc getWithinType() {
202             if (methodInvocation.getThis() == null) {
203                 throw new UnsupportedOperationException JavaDoc("No source location joinpoint available: target is null");
204             }
205             return methodInvocation.getThis().getClass();
206         }
207
208         public String JavaDoc getFileName() {
209             throw new UnsupportedOperationException JavaDoc();
210         }
211
212         public int getLine() {
213             throw new UnsupportedOperationException JavaDoc();
214         }
215
216         public int getColumn() {
217             throw new UnsupportedOperationException JavaDoc();
218         }
219     }
220
221 }
222
Popular Tags