KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > symboltable > MethodScope


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
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 JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 public class MethodScope extends AbstractScope {
17
18     protected Map JavaDoc variableNames = new HashMap JavaDoc();
19     private SimpleNode node;
20
21     public MethodScope(SimpleNode node) {
22         this.node = node;
23     }
24
25     public MethodScope getEnclosingMethodScope() {
26         return this;
27     }
28
29     public Map JavaDoc getVariableDeclarations() {
30         VariableUsageFinderFunction f = new VariableUsageFinderFunction(variableNames);
31         Applier.apply(f, variableNames.keySet().iterator());
32         return f.getUsed();
33     }
34
35     public NameDeclaration addVariableNameOccurrence(NameOccurrence occurrence) {
36         NameDeclaration decl = findVariableHere(occurrence);
37         if (decl != null && !occurrence.isThisOrSuper()) {
38             ((List JavaDoc) variableNames.get(decl)).add(occurrence);
39             SimpleNode n = occurrence.getLocation();
40             if (n instanceof ASTName) {
41                 ((ASTName) n).setNameDeclaration(decl);
42             } // TODO what to do with PrimarySuffix case?
43
}
44         return decl;
45     }
46
47     public void addDeclaration(VariableNameDeclaration variableDecl) {
48         if (variableNames.containsKey(variableDecl)) {
49             throw new RuntimeException JavaDoc("Variable " + variableDecl + " is already in the symbol table");
50         }
51         variableNames.put(variableDecl, new ArrayList JavaDoc());
52     }
53
54     public NameDeclaration findVariableHere(NameOccurrence occurrence) {
55         if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
56             return null;
57         }
58         ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
59         Applier.apply(finder, variableNames.keySet().iterator());
60         return finder.getDecl();
61     }
62
63     public String JavaDoc getName() {
64         if (node instanceof ASTConstructorDeclaration) {
65             return this.getEnclosingClassScope().getClassName();
66         }
67         return ((SimpleNode) node.jjtGetChild(1)).getImage();
68     }
69
70     public String JavaDoc toString() {
71         return "MethodScope:" + glomNames(variableNames.keySet().iterator());
72     }
73 }
74
Popular Tags