44
43
// Access to the handleMap and typeMap are now synchronized - at least the find methods and the updateHandleMap function
46
private Map fileMap = null;
47
private Map handleMap = new HashMap();
48
private Map typeMap = null;
45
private Map<String, IProgramElement> fileMap = null;
46
private Map<String, IProgramElement> handleMap = new HashMap<String, IProgramElement>();
47
private Map<String, IProgramElement> typeMap = null;
50
49
public AspectJElementHierarchy(AsmManager asm) {
65
public String toSummaryString() {
66
StringBuilder s = new StringBuilder();
67
s.append("FileMap has " + fileMap.size() + " entries\n");
68
s.append("HandleMap has " + handleMap.size() + " entries\n");
69
s.append("TypeMap has " + handleMap.size() + " entries\n");
70
s.append("FileMap:\n");
71
for (Map.Entry<String, IProgramElement> fileMapEntry : fileMap.entrySet()) {
72
s.append(fileMapEntry).append("\n");
74
s.append("TypeMap:\n");
75
for (Map.Entry<String, IProgramElement> typeMapEntry : typeMap.entrySet()) {
76
s.append(typeMapEntry).append("\n");
78
s.append("HandleMap:\n");
79
for (Map.Entry<String, IProgramElement> handleMapEntry : handleMap.entrySet()) {
80
s.append(handleMapEntry).append("\n");
66
85
public void setRoot(IProgramElement root) {
68
handleMap = new HashMap();
69
typeMap = new HashMap();
87
handleMap = new HashMap<String, IProgramElement>();
88
typeMap = new HashMap<String, IProgramElement>();
72
public void addToFileMap(Object key, Object value) {
91
public void addToFileMap(String key, IProgramElement value) {
73
92
fileMap.put(key, value);
76
public boolean removeFromFileMap(Object key) {
77
if (fileMap.containsKey(key)) {
78
return (fileMap.remove(key) != null);
95
public boolean removeFromFileMap(String canonicalFilePath) {
96
return fileMap.remove(canonicalFilePath) != null;
83
public void setFileMap(HashMap fileMap) {
99
public void setFileMap(HashMap<String, IProgramElement> fileMap) {
84
100
this.fileMap = fileMap;
104
120
* @return null if not found
106
122
public IProgramElement findElementForSignature(IProgramElement parent, IProgramElement.Kind kind, String signature) {
107
for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
108
IProgramElement node = (IProgramElement) it.next();
123
for (IProgramElement node : parent.getChildren()) {
109
124
if (node.getKind() == kind && signature.equals(node.toSignatureString())) {
148
161
StringBuffer keyb = (packageName == null) ? new StringBuffer() : new StringBuffer(packageName);
149
162
keyb.append(".").append(typeName);
150
163
String key = keyb.toString();
151
IProgramElement cachedValue = (IProgramElement) typeMap.get(key);
164
IProgramElement cachedValue = typeMap.get(key);
152
165
if (cachedValue != null) {
153
166
return cachedValue;
156
List packageNodes = findMatchingPackages(packageName);
169
List<IProgramElement> packageNodes = findMatchingPackages(packageName);
158
for (Iterator iterator = packageNodes.iterator(); iterator.hasNext();) {
159
IProgramElement pkg = (IProgramElement) iterator.next();
171
for (IProgramElement pkg : packageNodes) {
160
172
// this searches each file for a class
161
for (Iterator it = pkg.getChildren().iterator(); it.hasNext();) {
162
IProgramElement fileNode = (IProgramElement) it.next();
173
for (IProgramElement fileNode : pkg.getChildren()) {
163
174
IProgramElement cNode = findClassInNodes(fileNode.getChildren(), typeName, typeName);
164
175
if (cNode != null) {
165
176
typeMap.put(key, cNode);
210
221
* @param packagename the packagename being searched for
211
222
* @return a list of package nodes that match that name
213
public List/* IProgramElement */findMatchingPackages(String packagename) {
214
List children = root.getChildren();
224
public List<IProgramElement> findMatchingPackages(String packagename) {
225
List<IProgramElement> children = root.getChildren();
215
226
// The children might be source folders or packages
216
227
if (children.size() == 0) {
217
return Collections.EMPTY_LIST;
228
return Collections.emptyList();
219
if (((IProgramElement) children.get(0)).getKind() == IProgramElement.Kind.SOURCE_FOLDER) {
230
if ((children.get(0)).getKind() == IProgramElement.Kind.SOURCE_FOLDER) {
220
231
String searchPackageName = (packagename == null ? "" : packagename); // default package means match on ""
221
232
// dealing with source folders
222
List matchingPackageNodes = new ArrayList();
223
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
224
IProgramElement sourceFolder = (IProgramElement) iterator.next();
225
List possiblePackageNodes = sourceFolder.getChildren();
226
for (Iterator iterator2 = possiblePackageNodes.iterator(); iterator2.hasNext();) {
227
IProgramElement possiblePackageNode = (IProgramElement) iterator2.next();
233
List<IProgramElement> matchingPackageNodes = new ArrayList<IProgramElement>();
234
for (IProgramElement sourceFolder : children) {
235
List<IProgramElement> possiblePackageNodes = sourceFolder.getChildren();
236
for (IProgramElement possiblePackageNode : possiblePackageNodes) {
228
237
if (possiblePackageNode.getKind() == IProgramElement.Kind.PACKAGE) {
229
238
if (possiblePackageNode.getName().equals(searchPackageName)) {
230
239
matchingPackageNodes.add(possiblePackageNode);
239
248
// thing to return in the list
240
249
if (packagename == null) {
241
250
// default package
242
List result = new ArrayList();
251
List<IProgramElement> result = new ArrayList<IProgramElement>();
243
252
result.add(root);
246
List result = new ArrayList();
247
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
248
IProgramElement possiblePackage = (IProgramElement) iterator.next();
255
List<IProgramElement> result = new ArrayList<IProgramElement>();
256
for (IProgramElement possiblePackage : children) {
249
257
if (possiblePackage.getKind() == IProgramElement.Kind.PACKAGE && possiblePackage.getName().equals(packagename)) {
250
258
result.add(possiblePackage);
252
260
if (possiblePackage.getKind() == IProgramElement.Kind.SOURCE_FOLDER) { // might be 'binaries'
253
261
if (possiblePackage.getName().equals("binaries")) {
254
for (Iterator iter2 = possiblePackage.getChildren().iterator(); iter2.hasNext();) {
255
IProgramElement possiblePackage2 = (IProgramElement) iter2.next();
262
for (IProgramElement possiblePackage2 : possiblePackage.getChildren()) {
256
263
if (possiblePackage2.getKind() == IProgramElement.Kind.PACKAGE
257
264
&& possiblePackage2.getName().equals(packagename)) {
258
265
result.add(possiblePackage2);
265
272
if (result.isEmpty()) {
266
return Collections.EMPTY_LIST;
273
return Collections.emptyList();
273
private IProgramElement findClassInNodes(Collection nodes, String name, String typeName) {
280
private IProgramElement findClassInNodes(Collection<IProgramElement> nodes, String name, String typeName) {
275
282
String innerName;
276
283
int dollar = name.indexOf('$');
467
472
if (node != null) {
468
for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
469
IProgramElement foundNode = findNodeForSourceLineHelper((IProgramElement) it.next(), sourceFilePath, lineno, offset);
473
for (IProgramElement child : node.getChildren()) {
474
IProgramElement foundNode = findNodeForSourceLineHelper(child, sourceFilePath, lineno, offset);
470
475
if (foundNode != null) {
471
476
return foundNode;
498
503
private boolean hasMoreSpecificChild(IProgramElement node, String sourceFilePath, int lineNumber, int offSet) {
499
for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
500
IProgramElement child = (IProgramElement) it.next();
504
for (IProgramElement child : node.getChildren()) {
501
505
if (matches(child, sourceFilePath, lineNumber, offSet)) {
524
528
// try the cache first...
525
529
IProgramElement ipe = null;
526
530
synchronized (this) {
527
ipe = (IProgramElement) handleMap.get(handle);
531
ipe = handleMap.get(handle);
528
532
if (ipe != null) {
532
535
ipe = findElementForHandle(root, handle);
533
536
if (ipe == null && create) {
534
537
ipe = createFileStructureNode(getFilename(handle));
603
public void forget(IProgramElement compilationUnitNode, IProgramElement typeNode) {
605
synchronized (this) {
608
for (Map.Entry<String, IProgramElement> typeMapEntry : typeMap.entrySet()) {
609
if (typeMapEntry.getValue() == typeNode) {
610
k = typeMapEntry.getKey();
619
if (compilationUnitNode != null) {
621
for (Map.Entry<String, IProgramElement> entry : fileMap.entrySet()) {
622
if (entry.getValue() == compilationUnitNode) {
601
633
// TODO rename this method ... it does more than just the handle map
602
public void updateHandleMap(Set deletedFiles) {
634
public void updateHandleMap(Set<String> deletedFiles) {
603
635
// Only delete the entries we need to from the handle map - for performance reasons
604
List forRemoval = new ArrayList();
636
List<String> forRemoval = new ArrayList<String>();
637
Set<String> k = null;
606
638
synchronized (this) {
607
639
k = handleMap.keySet();
608
for (Iterator iter = k.iterator(); iter.hasNext();) {
609
String handle = (String) iter.next();
610
IProgramElement ipe = (IProgramElement) handleMap.get(handle);
611
if (deletedFiles.contains(getCanonicalFilePath(ipe))) {
640
for (String handle : k) {
641
IProgramElement ipe = handleMap.get(handle);
643
System.err.println("handleMap expectation not met, where is the IPE for " + handle);
645
if (ipe == null || deletedFiles.contains(getCanonicalFilePath(ipe))) {
612
646
forRemoval.add(handle);
615
for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
616
String handle = (String) iter.next();
649
for (String handle : forRemoval) {
617
650
handleMap.remove(handle);
619
652
forRemoval.clear();
620
653
k = typeMap.keySet();
621
for (Iterator iter = k.iterator(); iter.hasNext();) {
622
String typeName = (String) iter.next();
623
IProgramElement ipe = (IProgramElement) typeMap.get(typeName);
654
for (String typeName : k) {
655
IProgramElement ipe = typeMap.get(typeName);
624
656
if (deletedFiles.contains(getCanonicalFilePath(ipe))) {
625
657
forRemoval.add(typeName);
628
for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
629
String typeName = (String) iter.next();
660
for (String typeName : forRemoval) {
630
661
typeMap.remove(typeName);
632
663
forRemoval.clear();
634
k = fileMap.keySet();
635
for (Iterator iter = k.iterator(); iter.hasNext();) {
636
String filePath = (String) iter.next();
637
IProgramElement ipe = (IProgramElement) fileMap.get(filePath);
638
if (deletedFiles.contains(getCanonicalFilePath(ipe))) {
665
for (Map.Entry<String, IProgramElement> entry : fileMap.entrySet()) {
666
String filePath = entry.getKey();
667
if (deletedFiles.contains(getCanonicalFilePath(entry.getValue()))) {
639
668
forRemoval.add(filePath);
642
for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
643
String filePath = (String) iter.next();
671
for (String filePath : forRemoval) {
644
672
fileMap.remove(filePath);