|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.symboltable; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.ast.ASTConstructorDeclaration; |
|
7 |
| import net.sourceforge.pmd.ast.ASTName; |
|
8 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
9 |
| import net.sourceforge.pmd.util.Applier; |
|
10 |
| |
|
11 |
| import java.util.ArrayList; |
|
12 |
| import java.util.HashMap; |
|
13 |
| import java.util.List; |
|
14 |
| import java.util.Map; |
|
15 |
| |
|
16 |
| public class MethodScope extends AbstractScope { |
|
17 |
| |
|
18 |
| protected Map variableNames = new HashMap(); |
|
19 |
| private SimpleNode node; |
|
20 |
| |
|
21 |
1217
| public MethodScope(SimpleNode node) {
|
|
22 |
1217
| this.node = node;
|
|
23 |
| } |
|
24 |
| |
|
25 |
370
| public MethodScope getEnclosingMethodScope() {
|
|
26 |
370
| return this;
|
|
27 |
| } |
|
28 |
| |
|
29 |
92
| public Map getVariableDeclarations() {
|
|
30 |
92
| VariableUsageFinderFunction f = new VariableUsageFinderFunction(variableNames);
|
|
31 |
92
| Applier.apply(f, variableNames.keySet().iterator());
|
|
32 |
92
| return f.getUsed();
|
|
33 |
| } |
|
34 |
| |
|
35 |
249
| public NameDeclaration addVariableNameOccurrence(NameOccurrence occurrence) {
|
|
36 |
249
| NameDeclaration decl = findVariableHere(occurrence);
|
|
37 |
249
| if (decl != null && !occurrence.isThisOrSuper()) {
|
|
38 |
249
| ((List) variableNames.get(decl)).add(occurrence);
|
|
39 |
249
| SimpleNode n = occurrence.getLocation();
|
|
40 |
249
| if (n instanceof ASTName) {
|
|
41 |
248
| ((ASTName) n).setNameDeclaration(decl);
|
|
42 |
| } |
|
43 |
| } |
|
44 |
249
| return decl;
|
|
45 |
| } |
|
46 |
| |
|
47 |
395
| public void addDeclaration(VariableNameDeclaration variableDecl) {
|
|
48 |
395
| if (variableNames.containsKey(variableDecl)) {
|
|
49 |
0
| throw new RuntimeException("Variable " + variableDecl + " is already in the symbol table");
|
|
50 |
| } |
|
51 |
395
| variableNames.put(variableDecl, new ArrayList());
|
|
52 |
| } |
|
53 |
| |
|
54 |
1596
| public NameDeclaration findVariableHere(NameOccurrence occurrence) {
|
|
55 |
1596
| if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
|
|
56 |
431
| return null;
|
|
57 |
| } |
|
58 |
1165
| ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
|
|
59 |
1165
| Applier.apply(finder, variableNames.keySet().iterator());
|
|
60 |
1165
| return finder.getDecl();
|
|
61 |
| } |
|
62 |
| |
|
63 |
370
| public String getName() {
|
|
64 |
370
| if (node instanceof ASTConstructorDeclaration) {
|
|
65 |
0
| return this.getEnclosingClassScope().getClassName();
|
|
66 |
| } |
|
67 |
370
| return ((SimpleNode) node.jjtGetChild(1)).getImage();
|
|
68 |
| } |
|
69 |
| |
|
70 |
0
| public String toString() {
|
|
71 |
0
| return "MethodScope:" + glomNames(variableNames.keySet().iterator());
|
|
72 |
| } |
|
73 |
| } |