KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ide > Declaration


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.tools.ide;
26 import java.util.Enumeration JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.Serializable JavaDoc;
30
31
32 import java.util.*;
33
34 // Make sure that references to Vector use the standard version
35
// This lets us generate serialized files which interoperate with other code better
36
import java.util.Vector JavaDoc;
37
38 public class Declaration implements Serializable JavaDoc {
39     private int beginLine;
40     private int endLine;
41     private int beginColumn;
42     private int endColumn;
43
44     private String JavaDoc modifiers;
45     private String JavaDoc fullSignature;
46     private String JavaDoc signature;
47     private String JavaDoc crosscutDesignator;
48
49     private String JavaDoc packageName;
50
51     private String JavaDoc kind;
52     private String JavaDoc declaringType;
53
54     private String JavaDoc filename;
55     private String JavaDoc formalComment;
56
57     private Declaration[] declarations;
58
59     private Handle crosscutDeclarationHandle;
60     private Handle[] pointedToByHandles;
61     private Handle[] pointsToHandles;
62
63     transient private Declaration crosscutDeclaration;
64     transient private Declaration[] pointedToBy = null;
65     transient private Declaration[] pointsTo = null;
66
67     private Declaration parentDeclaration = null;
68
69     public Declaration(int beginLine, int endLine, int beginColumn, int endColumn,
70                        String JavaDoc modifiers, String JavaDoc signature, String JavaDoc fullSignature,
71                        String JavaDoc crosscutDesignator,
72                        String JavaDoc declaringType, String JavaDoc kind,
73                        String JavaDoc filename, String JavaDoc formalComment,
74                        String JavaDoc packageName)
75     {
76         this.beginLine = beginLine;
77         this.endLine = endLine;
78         this.beginColumn = beginColumn;
79         this.endColumn = endColumn;
80
81         this.modifiers = modifiers;
82         this.signature = signature;
83         this.fullSignature = fullSignature;
84
85         this.crosscutDesignator = crosscutDesignator;
86
87         this.declaringType = declaringType;
88         this.kind = kind;
89
90         this.filename = filename;
91         this.formalComment = formalComment;
92
93         this.packageName = packageName;
94
95         this.pointedToByHandles = new Handle[0];
96         this.pointsToHandles = new Handle[0];
97         //???
98
this.declarations = new Declaration[0];
99     }
100
101     public int getBeginLine() { return beginLine; }
102     public int getEndLine() { return endLine; }
103     public int getBeginColumn() { return beginColumn; }
104     public int getEndColumn() { return endColumn; }
105
106     public String JavaDoc getModifiers() { return modifiers; }
107     public String JavaDoc getFullSignature() { return fullSignature; }
108     public String JavaDoc getSignature() { return signature; }
109
110     public String JavaDoc getPackageName() { return packageName; }
111
112     public String JavaDoc getCrosscutDesignator() { return crosscutDesignator; }
113
114     public Declaration getParentDeclaration() { return parentDeclaration; }
115
116     public Declaration getCrosscutDeclaration() {
117         if (crosscutDeclaration == null && crosscutDeclarationHandle != null) {
118             crosscutDeclaration = crosscutDeclarationHandle.resolve();
119         }
120         return crosscutDeclaration;
121     }
122
123     public void setCrosscutDeclaration(Declaration _crosscutDeclaration) {
124         crosscutDeclaration = _crosscutDeclaration;
125     }
126
127     public String JavaDoc getDeclaringType() { return declaringType; }
128     public String JavaDoc getKind() {
129         if (kind.startsWith("introduced-")) {
130             return kind.substring(11);
131         } else {
132             return kind;
133         }
134     }
135
136     public String JavaDoc getFilename() { return filename; }
137     public String JavaDoc getFormalComment() { return formalComment; }
138
139     public Declaration[] getDeclarations() {
140         return declarations;
141     }
142     public void setDeclarations(Declaration[] decs) {
143         declarations = decs;
144         if (decs != null) {
145             for (int i = 0; i < decs.length; i++) {
146                 decs[i].parentDeclaration = this;
147             }
148         }
149     }
150     public void setPointedToBy(Declaration[] decs) { pointedToBy = decs; }
151     public void setPointsTo(Declaration[] decs) { pointsTo = decs; }
152
153
154     public Declaration[] getPointedToBy() {
155         if (pointedToBy == null) {
156             pointedToBy = resolveHandles(pointedToByHandles);
157         }
158         return pointedToBy; //.elements();
159
}
160
161     public Declaration[] getPointsTo() {
162         if (pointsTo == null) {
163             pointsTo = resolveHandles(pointsToHandles);
164         }
165         return pointsTo; //.elements();
166
}
167
168     private Declaration[] filterTypes(Declaration[] a_decs) {
169         List decs = new LinkedList(Arrays.asList(a_decs));
170         for(Iterator i = decs.iterator(); i.hasNext(); ) {
171             Declaration dec = (Declaration)i.next();
172             if (!dec.isType()) i.remove();
173         }
174         return (Declaration[])decs.toArray(new Declaration[decs.size()]);
175     }
176
177
178     public Declaration[] getTargets() {
179         Declaration[] pointsTo = getPointsTo();
180
181         if (kind.equals("advice")) {
182             return pointsTo;
183         } else if (kind.equals("introduction")) {
184             return filterTypes(pointsTo);
185         } else {
186             return new Declaration[0];
187         }
188     }
189
190     // Handles are used to deal with dependencies between Declarations in different files
191
private Handle getHandle() {
192         return new Handle(filename, beginLine, beginColumn);
193     }
194
195     private Declaration[] resolveHandles(Handle[] handles) {
196         Declaration[] declarations = new Declaration[handles.length];
197         int missed = 0;
198         for(int i=0; i<handles.length; i++) {
199             //if (handles[i] == null) continue;
200
declarations[i] = handles[i].resolve();
201             if (declarations[i] == null) missed++;
202         }
203         if (missed > 0) {
204             Declaration[] decs = new Declaration[declarations.length - missed];
205             for (int i=0, j=0; i < declarations.length; i++) {
206                 if (declarations[i] != null) decs[j++] = declarations[i];
207             }
208             declarations = decs;
209         }
210         return declarations;
211     }
212
213     private Handle[] getHandles(Declaration[] declarations) {
214         Handle[] handles = new Handle[declarations.length];
215         for(int i=0; i<declarations.length; i++) {
216             //if (declarations[i] == null) continue;
217
handles[i] = declarations[i].getHandle();
218         }
219         return handles;
220     }
221
222     // Make sure that all decs are convertted to handles before serialization
223
private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
224         pointedToByHandles = getHandles(getPointedToBy());
225         pointsToHandles = getHandles(getPointsTo());
226         if (crosscutDeclaration != null) {
227            crosscutDeclarationHandle = crosscutDeclaration.getHandle();
228         }
229         out.defaultWriteObject();
230     }
231
232     // support functions
233
public Declaration[] getCrosscutDeclarations() {
234         return getDeclarationsHelper("pointcut");
235     }
236
237     public Declaration[] getAdviceDeclarations() {
238         return getDeclarationsHelper("advice");
239     }
240
241     public Declaration[] getIntroductionDeclarations() {
242         return getDeclarationsHelper("introduction");
243     }
244
245     private Declaration[] getDeclarationsHelper(String JavaDoc kind) {
246         Declaration[] decls = getDeclarations();
247         List result = new ArrayList();
248         for ( int i = 0; i < decls.length; i++ ) {
249             Declaration decl = decls[i];
250             if ( decl.getKind().equals(kind) ) {
251                 result.add(decl);
252             }
253         }
254         return (Declaration[])result.toArray(new Declaration[result.size()]);
255     }
256
257
258     public boolean isType() {
259         return getKind().equals("interface") || getKind().equals("class");
260     }
261
262     public boolean hasBody() {
263         String JavaDoc kind = getKind();
264         return kind.equals("class") || kind.endsWith("constructor") ||
265             (kind.endsWith("method") && getModifiers().indexOf("abstract") == -1 &&
266               getModifiers().indexOf("native") == -1);
267     }
268
269     public boolean isIntroduced() {
270         return kind.startsWith("introduced-");
271     }
272
273     public boolean hasSignature() {
274         String JavaDoc kind = getKind();
275         if ( kind.equals( "class" ) ||
276              kind.equals( "interface" ) ||
277              kind.equals( "initializer" ) ||
278              kind.equals( "field" ) ||
279              kind.equals( "constructor" ) ||
280              kind.equals( "method" ) ) {
281             return true;
282         }
283         else {
284             return false;
285         }
286     }
287
288     private static class Handle implements Serializable JavaDoc {
289         public String JavaDoc filename;
290         public int line, column;
291
292         public Handle(String JavaDoc filename, int line, int column) {
293             this.filename = filename;
294             this.line = line;
295             this.column = column;
296         }
297
298         public Declaration resolve() {
299             SymbolManager manager = SymbolManager.getSymbolManager();
300             return manager.getDeclarationAtPoint(filename, line, column);
301         }
302     }
303 }
304
Popular Tags