~ubuntu-branches/ubuntu/wily/aspectj/wily-proposed

« back to all changes in this revision

Viewing changes to org.aspectj/modules/asm/src/org/aspectj/asm/internal/AspectJElementHierarchy.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-15 23:54:31 UTC
  • mfrom: (1.2.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110315235431-iq2gxbsx08kpwuiw
* New upstream release.
* Updated Standards-Version to 3.9.1 (no changes needed).
* Fix local Javadoc links:
  - d/patches/07_javadoc_links.diff: Use locally installed
   javadoc packages and hyperlink with them.
  - d/control: Add B-D on default-java-doc and libasm3-java-doc.
* d/control: Drop B-D on itself (our new bootstrap infrastructure doesn't need
  that anymore).
* Split packages into :
  - aspectj: only contains CLI tools.
  - libaspectj-java: JAR librairies for /usr/share/java.
  - libaspectj-java-doc: 4 API's Javadoc.
  - aspectj-doc: Programming Guides and SDK Documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import java.util.Collection;
19
19
import java.util.Collections;
20
20
import java.util.HashMap;
21
 
import java.util.Iterator;
22
21
import java.util.List;
23
22
import java.util.Map;
24
23
import java.util.Set;
43
42
 
44
43
        // Access to the handleMap and typeMap are now synchronized - at least the find methods and the updateHandleMap function
45
44
        // see pr305788
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;
49
48
 
50
49
        public AspectJElementHierarchy(AsmManager asm) {
51
50
                this.asm = asm;
63
62
                return root;
64
63
        }
65
64
 
 
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");
 
73
                }
 
74
                s.append("TypeMap:\n");
 
75
                for (Map.Entry<String, IProgramElement> typeMapEntry : typeMap.entrySet()) {
 
76
                        s.append(typeMapEntry).append("\n");
 
77
                }
 
78
                s.append("HandleMap:\n");
 
79
                for (Map.Entry<String, IProgramElement> handleMapEntry : handleMap.entrySet()) {
 
80
                        s.append(handleMapEntry).append("\n");
 
81
                }
 
82
                return s.toString();
 
83
        }
 
84
 
66
85
        public void setRoot(IProgramElement root) {
67
86
                this.root = root;
68
 
                handleMap = new HashMap();
69
 
                typeMap = new HashMap();
 
87
                handleMap = new HashMap<String, IProgramElement>();
 
88
                typeMap = new HashMap<String, IProgramElement>();
70
89
        }
71
90
 
72
 
        public void addToFileMap(Object key, Object value) {
 
91
        public void addToFileMap(String key, IProgramElement value) {
73
92
                fileMap.put(key, value);
74
93
        }
75
94
 
76
 
        public boolean removeFromFileMap(Object key) {
77
 
                if (fileMap.containsKey(key)) {
78
 
                        return (fileMap.remove(key) != null);
79
 
                }
80
 
                return true;
 
95
        public boolean removeFromFileMap(String canonicalFilePath) {
 
96
                return fileMap.remove(canonicalFilePath) != null;
81
97
        }
82
98
 
83
 
        public void setFileMap(HashMap fileMap) {
 
99
        public void setFileMap(HashMap<String, IProgramElement> fileMap) {
84
100
                this.fileMap = fileMap;
85
101
        }
86
102
 
88
104
                return fileMap.get(key);
89
105
        }
90
106
 
91
 
        public Set getFileMapEntrySet() {
 
107
        public Set<Map.Entry<String, IProgramElement>> getFileMapEntrySet() {
92
108
                return fileMap.entrySet();
93
109
        }
94
110
 
104
120
         * @return null if not found
105
121
         */
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())) {
110
125
                                return node;
111
126
                        } else {
119
134
        }
120
135
 
121
136
        public IProgramElement findElementForLabel(IProgramElement parent, IProgramElement.Kind kind, String label) {
122
 
 
123
 
                for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
124
 
                        IProgramElement node = (IProgramElement) it.next();
 
137
                for (IProgramElement node : parent.getChildren()) {
125
138
                        if (node.getKind() == kind && label.equals(node.toLabelString())) {
126
139
                                return node;
127
140
                        } else {
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;
154
167
                        }
155
168
 
156
 
                        List packageNodes = findMatchingPackages(packageName);
 
169
                        List<IProgramElement> packageNodes = findMatchingPackages(packageName);
157
170
 
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
212
223
         */
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();
218
229
                }
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);
244
253
                                return result;
245
254
                        }
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);
251
259
                                }
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);
263
270
                                }
264
271
                        }
265
272
                        if (result.isEmpty()) {
266
 
                                return Collections.EMPTY_LIST;
 
273
                                return Collections.emptyList();
267
274
                        } else {
268
275
                                return result;
269
276
                        }
270
277
                }
271
278
        }
272
279
 
273
 
        private IProgramElement findClassInNodes(Collection nodes, String name, String typeName) {
 
280
        private IProgramElement findClassInNodes(Collection<IProgramElement> nodes, String name, String typeName) {
274
281
                String baseName;
275
282
                String innerName;
276
283
                int dollar = name.indexOf('$');
282
289
                        innerName = name.substring(dollar + 1);
283
290
                }
284
291
 
285
 
                for (Iterator j = nodes.iterator(); j.hasNext();) {
286
 
                        IProgramElement classNode = (IProgramElement) j.next();
 
292
                for (IProgramElement classNode : nodes) {
287
293
                        if (baseName.equals(classNode.getName())) {
288
294
                                if (innerName == null) {
289
295
                                        return classNode;
383
389
                        return null; // no need to search children of a source file node
384
390
                } else {
385
391
                        // check the children
386
 
                        for (Iterator iterator = node.getChildren().iterator(); iterator.hasNext();) {
387
 
                                IProgramElement foundit = findNodeForSourceFile((IProgramElement) iterator.next(), sourcefilePath);
 
392
                        for (IProgramElement child : node.getChildren()) {
 
393
                                IProgramElement foundit = findNodeForSourceFile(child, sourcefilePath);
388
394
                                if (foundit != null) {
389
395
                                        return foundit;
390
396
                                }
435
441
                if (node == null || node.getChildren() == null) {
436
442
                        return null;
437
443
                }
438
 
                for (Iterator childrenIter = node.getChildren().iterator(); childrenIter.hasNext();) {
439
 
                        IProgramElement child = (IProgramElement) childrenIter.next();
 
444
                for (IProgramElement child : node.getChildren()) {
440
445
                        ISourceLocation childLoc = child.getSourceLocation();
441
446
                        if (childLoc != null) {
442
447
                                if (childLoc.getLine() <= lineno && childLoc.getEndLine() >= lineno) {
465
470
                }
466
471
 
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;
472
477
                                }
496
501
        }
497
502
 
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)) {
502
506
                                return true;
503
507
                        }
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) {
529
533
                                return ipe;
530
534
                        }
531
 
 
532
535
                        ipe = findElementForHandle(root, handle);
533
536
                        if (ipe == null && create) {
534
537
                                ipe = createFileStructureNode(getFilename(handle));
541
544
        }
542
545
 
543
546
        private IProgramElement findElementForHandle(IProgramElement parent, String handle) {
544
 
                for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
545
 
                        IProgramElement node = (IProgramElement) it.next();
 
547
                for (IProgramElement node : parent.getChildren()) {
546
548
                        String nodeHid = node.getHandleIdentifier();
547
549
                        if (handle.equals(nodeHid)) {
548
550
                                return node;
559
561
                return null;
560
562
        }
561
563
 
562
 
        //      
 
564
        //
563
565
        // private IProgramElement findElementForBytecodeInfo(
564
566
        // IProgramElement node,
565
567
        // String parentName,
598
600
                fileMap.clear();
599
601
        }
600
602
 
 
603
        public void forget(IProgramElement compilationUnitNode, IProgramElement typeNode) {
 
604
                String k = null;
 
605
                synchronized (this) {
 
606
                        // handle map
 
607
                        // type map
 
608
                        for (Map.Entry<String, IProgramElement> typeMapEntry : typeMap.entrySet()) {
 
609
                                if (typeMapEntry.getValue() == typeNode) {
 
610
                                        k = typeMapEntry.getKey();
 
611
                                        break;
 
612
                                }
 
613
                        }
 
614
                        if (k != null) {
 
615
                                typeMap.remove(k);
 
616
                        }
 
617
                }
 
618
 
 
619
                if (compilationUnitNode != null) {
 
620
                        k = null;
 
621
                        for (Map.Entry<String, IProgramElement> entry : fileMap.entrySet()) {
 
622
                                if (entry.getValue() == compilationUnitNode) {
 
623
                                        k = entry.getKey();
 
624
                                        break;
 
625
                                }
 
626
                        }
 
627
                        if (k != null) {
 
628
                                fileMap.remove(k);
 
629
                        }
 
630
                }
 
631
        }
 
632
 
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();
605
 
                Set k = null;
 
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);
 
642
                                if (ipe == null) {
 
643
                                        System.err.println("handleMap expectation not met, where is the IPE for " + handle);
 
644
                                }
 
645
                                if (ipe == null || deletedFiles.contains(getCanonicalFilePath(ipe))) {
612
646
                                        forRemoval.add(handle);
613
647
                                }
614
648
                        }
615
 
                        for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
616
 
                                String handle = (String) iter.next();
 
649
                        for (String handle : forRemoval) {
617
650
                                handleMap.remove(handle);
618
651
                        }
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);
626
658
                                }
627
659
                        }
628
 
                        for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
629
 
                                String typeName = (String) iter.next();
 
660
                        for (String typeName : forRemoval) {
630
661
                                typeMap.remove(typeName);
631
662
                        }
632
663
                        forRemoval.clear();
633
664
                }
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);
640
669
                        }
641
670
                }
642
 
                for (Iterator iter = forRemoval.iterator(); iter.hasNext();) {
643
 
                        String filePath = (String) iter.next();
 
671
                for (String filePath : forRemoval) {
644
672
                        fileMap.remove(filePath);
645
673
                }
646
674
        }