1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/* *******************************************************************
* Copyright (c) 1999-2001 Xerox Corporation,
* 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.ajde.ui;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.aspectj.ajde.Ajde;
import org.aspectj.asm.LinkNode;
import org.aspectj.asm.ProgramElementNode;
import org.aspectj.asm.RelationNode;
import org.aspectj.asm.StructureModel;
import org.aspectj.asm.StructureModelManager;
import org.aspectj.asm.StructureNode;
/**
* Prototype functionality for package view clients.
*/
public class StructureModelUtil {
/**
* This method returns a map from affected source lines in a class to
* a List of aspects affecting that line.
* Based on method of same name by mik kirsten. To be replaced when StructureModelUtil
* corrects its implementation
*
* @param the full path of the source file to get a map for
*
* @return a Map from line numbers to a List of ProgramElementNodes.
*/
public static Map getLinesToAspectMap(String sourceFilePath) {
Map annotationsMap =
StructureModelManager.getDefault().getInlineAnnotations(
sourceFilePath,
true,
true);
Map aspectMap = new HashMap();
Set keys = annotationsMap.keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
Object key = it.next();
List annotations = (List) annotationsMap.get(key);
for (Iterator it2 = annotations.iterator(); it2.hasNext();) {
ProgramElementNode node = (ProgramElementNode) it2.next();
List relations = node.getRelations();
for (Iterator it3 = relations.iterator(); it3.hasNext();) {
RelationNode relationNode = (RelationNode) it3.next();
if (relationNode.getKind().equals("Advice")) {
List children = relationNode.getChildren();
List aspects = new Vector();
for (Iterator it4 = children.iterator();
it4.hasNext();
) {
Object object = it4.next();
if (object instanceof LinkNode) {
ProgramElementNode pNode =
((LinkNode) object).getProgramElementNode();
if (pNode.getProgramElementKind()
== ProgramElementNode.Kind.ADVICE) {
StructureNode theAspect = pNode.getParent();
aspects.add(theAspect);
}
}
}
if (!aspects.isEmpty()) {
aspectMap.put(key, aspects);
}
}
}
}
}
return aspectMap;
}
/**
* This method is copied from StructureModelUtil inoder for it to use the working
* version of getLineToAspectMap()
*
* @return the set of aspects with advice that affects the specified package
*/
public static Set getAspectsAffectingPackage(ProgramElementNode packageNode) {
List files = StructureModelUtil.getFilesInPackage(packageNode);
Set aspects = new HashSet();
for (Iterator it = files.iterator(); it.hasNext();) {
ProgramElementNode fileNode = (ProgramElementNode) it.next();
Map adviceMap =
getLinesToAspectMap(
fileNode.getSourceLocation().getSourceFile().getAbsolutePath());
Collection values = adviceMap.values();
for (Iterator it2 = values.iterator(); it2.hasNext();) {
aspects.add(it2.next());
}
}
return aspects;
}
public static List getPackagesInModel() {
List packages = new ArrayList();
StructureModel model =
Ajde.getDefault().getStructureModelManager().getStructureModel();
if (model.equals(StructureModel.NO_STRUCTURE)) {
return null;
} else {
return getPackagesHelper(
(ProgramElementNode) model.getRoot(),
ProgramElementNode.Kind.PACKAGE,
null,
packages);
}
}
private static List getPackagesHelper(
ProgramElementNode node,
ProgramElementNode.Kind kind,
String prename,
List matches) {
if (kind == null || node.getProgramElementKind().equals(kind)) {
if (prename == null) {
prename = new String(node.toString());
} else {
prename = new String(prename + "." + node);
}
Object[] o = new Object[2];
o[0] = node;
o[1] = prename;
matches.add(o);
}
for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
StructureNode nextNode = (StructureNode) it.next();
if (nextNode instanceof ProgramElementNode) {
getPackagesHelper(
(ProgramElementNode) nextNode,
kind,
prename,
matches);
}
}
return matches;
}
/**
* Helper function sorts a list of resources into alphabetical order
*/
private List sortElements(List oldElements) {
Object[] temp = oldElements.toArray();
SortingComparator comparator = new SortingComparator();
Arrays.sort(temp, comparator);
List newResources = Arrays.asList(temp);
return newResources;
}
private static List sortArray(List oldElements) {
Object[] temp = oldElements.toArray();
SortArrayComparator comparator = new SortArrayComparator();
Arrays.sort(temp, comparator);
List newElements = Arrays.asList(temp);
return newElements;
}
private class SortingComparator implements Comparator {
public int compare(Object o1, Object o2) {
ProgramElementNode p1 = (ProgramElementNode) o1;
ProgramElementNode p2 = (ProgramElementNode) o2;
String name1 = p1.getName();
String name2 = p2.getName();
return name1.compareTo(name2);
}
}
private static class SortArrayComparator implements Comparator {
public int compare(Object o1, Object o2) {
Object[] array1 = (Object[]) o1;
Object[] array2 = (Object[]) o2;
ProgramElementNode p1 = (ProgramElementNode) array1[1];
ProgramElementNode p2 = (ProgramElementNode) array2[1];
String name1 = p1.getName();
String name2 = p2.getName();
return name1.compareTo(name2);
}
}
/**
* @return all of the AspectJ and Java source files in a package
*/
public static List getFilesInPackage(ProgramElementNode packageNode) {
List packageContents;
if (packageNode == null) {
return null;
} else {
packageContents = packageNode.getChildren();
}
List files = new ArrayList();
for (Iterator it = packageContents.iterator(); it.hasNext(); ) {
ProgramElementNode packageItem = (ProgramElementNode)it.next();
if (packageItem.getProgramElementKind() == ProgramElementNode.Kind.FILE_JAVA
|| packageItem.getProgramElementKind() == ProgramElementNode.Kind.FILE_ASPECTJ) {
files.add(packageItem);
}
}
return files;
}
}
|