KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > runtime > reflect > JoinPointImpl


1 /* *******************************************************************
2  * Copyright (c) 1999-2001 Xerox Corporation,
3  * 2002 Palo Alto Research Center, Incorporated (PARC).
4  * All rights reserved.
5  * This program and the accompanying materials are made available
6  * under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * Xerox/PARC initial implementation
12  * ******************************************************************/

13
14
15 package org.aspectj.runtime.reflect;
16
17 import org.aspectj.lang.JoinPoint;
18 import org.aspectj.lang.ProceedingJoinPoint;
19 import org.aspectj.lang.Signature;
20 import org.aspectj.lang.reflect.SourceLocation;
21 import org.aspectj.runtime.internal.AroundClosure;
22
23 class JoinPointImpl implements ProceedingJoinPoint {
24     static class StaticPartImpl implements JoinPoint.StaticPart {
25         String JavaDoc kind;
26         Signature signature;
27         SourceLocation sourceLocation;
28
29         public StaticPartImpl(String JavaDoc kind, Signature signature, SourceLocation sourceLocation) {
30             this.kind = kind;
31             this.signature = signature;
32             this.sourceLocation = sourceLocation;
33         }
34
35         public String JavaDoc getKind() { return kind; }
36         public Signature getSignature() { return signature; }
37         public SourceLocation getSourceLocation() { return sourceLocation; }
38
39         String JavaDoc toString(StringMaker sm) {
40             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
41             buf.append(sm.makeKindName(getKind()));
42             buf.append("(");
43             buf.append(((SignatureImpl)getSignature()).toString(sm));
44             buf.append(")");
45             return buf.toString();
46         }
47
48         public final String JavaDoc toString() { return toString(StringMaker.middleStringMaker); }
49         public final String JavaDoc toShortString() { return toString(StringMaker.shortStringMaker); }
50         public final String JavaDoc toLongString() { return toString(StringMaker.longStringMaker); }
51     }
52
53     static class EnclosingStaticPartImpl extends StaticPartImpl implements EnclosingStaticPart {
54         public EnclosingStaticPartImpl(String JavaDoc kind, Signature signature, SourceLocation sourceLocation) {
55             super(kind, signature, sourceLocation);
56         }
57     }
58
59     Object JavaDoc _this;
60     Object JavaDoc target;
61     Object JavaDoc[] args;
62     org.aspectj.lang.JoinPoint.StaticPart staticPart;
63
64     public JoinPointImpl(org.aspectj.lang.JoinPoint.StaticPart staticPart, Object JavaDoc _this, Object JavaDoc target, Object JavaDoc[] args) {
65         this.staticPart = staticPart;
66         this._this = _this;
67         this.target = target;
68         this.args = args;
69     }
70
71     public Object JavaDoc getThis() { return _this; }
72     public Object JavaDoc getTarget() { return target; }
73     public Object JavaDoc[] getArgs() {
74         if (args == null) { args = new Object JavaDoc[0]; }
75         Object JavaDoc[] argsCopy = new Object JavaDoc[args.length];
76         System.arraycopy(args,0,argsCopy,0,args.length);
77         return argsCopy;
78     }
79
80     public org.aspectj.lang.JoinPoint.StaticPart getStaticPart() { return staticPart; }
81
82     public String JavaDoc getKind() { return staticPart.getKind(); }
83     public Signature getSignature() { return staticPart.getSignature(); }
84     public SourceLocation getSourceLocation() { return staticPart.getSourceLocation(); }
85
86     public final String JavaDoc toString() { return staticPart.toString(); }
87     public final String JavaDoc toShortString() { return staticPart.toShortString(); }
88     public final String JavaDoc toLongString() { return staticPart.toLongString(); }
89
90     // To proceed we need a closure to proceed on
91
private AroundClosure arc;
92     public void set$AroundClosure(AroundClosure arc) {
93         this.arc = arc;
94     }
95
96     public Object JavaDoc proceed() throws Throwable JavaDoc {
97         // when called from a before advice, but be a no-op
98
if (arc == null)
99             return null;
100         else
101             return arc.run(arc.getState());
102     }
103
104     public Object JavaDoc proceed(Object JavaDoc[] adviceBindings) throws Throwable JavaDoc {
105         // when called from a before advice, but be a no-op
106
if (arc == null)
107             return null;
108         else {
109
110             // Based on the bit flags in the AroundClosure we can determine what to
111
// expect in the adviceBindings array. We may or may not be expecting
112
// the first value to be a new this or a new target... (see pr126167)
113
int flags = arc.getFlags();
114             boolean unset = (flags &0x100000)!=0;
115             boolean thisTargetTheSame = (flags &0x010000)!=0;
116             boolean hasThis = (flags &0x001000)!=0;
117             boolean bindsThis = (flags &0x000100)!=0;
118             boolean hasTarget = (flags &0x000010)!=0;
119             boolean bindsTarget = (flags &0x000001)!=0;
120             
121             // state is always consistent with caller?,callee?,formals...,jp
122
Object JavaDoc[] state = arc.getState();
123             
124             // these next two numbers can differ because some join points have a this and
125
// target that are the same (eg. call) - and yet you can bind this and target
126
// separately.
127

128             // In the state array, [0] may be this, [1] may be target
129

130             int firstArgumentIndexIntoAdviceBindings = 0;
131             int firstArgumentIndexIntoState = 0;
132             firstArgumentIndexIntoState+=(hasThis?1:0);
133             firstArgumentIndexIntoState+=(hasTarget&&!thisTargetTheSame?1:0);
134             if (hasThis) {
135                 if (bindsThis) {
136                     // replace [0] (this)
137
firstArgumentIndexIntoAdviceBindings=1;
138                     state[0]=adviceBindings[0];
139                 } else {
140                     // leave state[0] alone, its OK
141
}
142             }
143             if (hasTarget) {
144                 if (bindsTarget) {
145                   if (thisTargetTheSame) {
146                       // this and target are the same so replace state[0]
147
firstArgumentIndexIntoAdviceBindings=1+(bindsThis?1:0);
148                       state[0]=adviceBindings[(bindsThis?1:0)];
149                   } else {
150                       // need to replace the target, and it is different to this, whether
151
// that means replacing state[0] or state[1] depends on whether
152
// the join point has a this
153
firstArgumentIndexIntoAdviceBindings=(hasThis?1:0)+1;
154                       state[hasThis?1:0]=adviceBindings[hasThis?1:0];
155                   }
156                 } else {
157                     // leave state[0]/state[1] alone, they are OK
158
}
159             }
160             
161             // copy the rest across
162
for (int i=firstArgumentIndexIntoAdviceBindings;i<adviceBindings.length;i++) {
163                 state[firstArgumentIndexIntoState+(i-firstArgumentIndexIntoAdviceBindings)]=adviceBindings[i];
164             }
165             
166             // old code that did this, didnt allow this/target overriding
167
// for (int i = state.length-2; i >= 0; i--) {
168
// int formalIndex = (adviceBindings.length - 1) - (state.length-2) + i;
169
// if (formalIndex >= 0 && formalIndex < adviceBindings.length) {
170
// state[i] = adviceBindings[formalIndex];
171
// }
172
// }
173
return arc.run(state);
174         }
175     }
176     
177
178 }
179
Popular Tags