1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.symboltable; |
5 |
| |
6 |
| import net.sourceforge.pmd.ast.ASTName; |
7 |
| import net.sourceforge.pmd.ast.SimpleNode; |
8 |
| import net.sourceforge.pmd.util.Applier; |
9 |
| |
10 |
| import java.util.ArrayList; |
11 |
| import java.util.HashMap; |
12 |
| import java.util.List; |
13 |
| import java.util.Map; |
14 |
| |
15 |
| public class LocalScope extends AbstractScope { |
16 |
| |
17 |
| protected Map variableNames = new HashMap(); |
18 |
| |
19 |
646
| public NameDeclaration addVariableNameOccurrence(NameOccurrence occurrence) {
|
20 |
646
| NameDeclaration decl = findVariableHere(occurrence);
|
21 |
646
| if (decl != null && !occurrence.isThisOrSuper()) {
|
22 |
644
| List nameOccurrences = (List) variableNames.get(decl);
|
23 |
644
| nameOccurrences.add(occurrence);
|
24 |
644
| SimpleNode n = occurrence.getLocation();
|
25 |
644
| if (n instanceof ASTName) {
|
26 |
644
| ((ASTName) n).setNameDeclaration(decl);
|
27 |
| } |
28 |
| } |
29 |
646
| return decl;
|
30 |
| } |
31 |
| |
32 |
339
| public Map getVariableDeclarations() {
|
33 |
339
| VariableUsageFinderFunction f = new VariableUsageFinderFunction(variableNames);
|
34 |
339
| Applier.apply(f, variableNames.keySet().iterator());
|
35 |
339
| return f.getUsed();
|
36 |
| } |
37 |
| |
38 |
567
| public void addDeclaration(VariableNameDeclaration nameDecl) {
|
39 |
567
| if (variableNames.containsKey(nameDecl)) {
|
40 |
0
| throw new RuntimeException("Variable " + nameDecl + " is already in the symbol table");
|
41 |
| } |
42 |
567
| variableNames.put(nameDecl, new ArrayList());
|
43 |
| } |
44 |
| |
45 |
3668
| public NameDeclaration findVariableHere(NameOccurrence occurrence) {
|
46 |
3668
| if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
|
47 |
324
| return null;
|
48 |
| } |
49 |
3344
| ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
|
50 |
3344
| Applier.apply(finder, variableNames.keySet().iterator());
|
51 |
3344
| return finder.getDecl();
|
52 |
| } |
53 |
| |
54 |
0
| public String toString() {
|
55 |
0
| return "LocalScope:" + glomNames(variableNames.keySet().iterator());
|
56 |
| } |
57 |
| } |