View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql.symboltable;
5   
6   import java.util.List;
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.lang.ast.Node;
10  import net.sourceforge.pmd.lang.plsql.ast.ASTName;
11  import net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode;
12  import net.sourceforge.pmd.lang.symboltable.AbstractScope;
13  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
14  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
15  
16  public class MethodScope extends AbstractScope {
17  
18      private Node node;
19  
20      public MethodScope(Node node) {
21          this.node = node;
22      }
23  
24      public MethodScope getEnclosingMethodScope() {
25          return this;
26      }
27  
28      public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() {
29          return getDeclarations(VariableNameDeclaration.class);
30      }
31  
32      @Override
33      public NameDeclaration addNameOccurrence(NameOccurrence occ) {
34          PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence)occ;
35          NameDeclaration decl = findVariableHere(occurrence);
36          if (decl != null && !occurrence.isThisOrSuper()) {
37              getVariableDeclarations().get(decl).add(occurrence);
38              Node n = occurrence.getLocation();
39              if (n instanceof ASTName) {
40                  ((ASTName) n).setNameDeclaration(decl);
41              } // TODO what to do with PrimarySuffix case?
42          }
43          return decl;
44      }
45  
46      @Override
47      public void addDeclaration(NameDeclaration declaration) {
48          if (declaration instanceof VariableNameDeclaration && getDeclarations().keySet().contains(declaration)) {
49              throw new RuntimeException(declaration + " is already in the symbol table");
50          }
51          super.addDeclaration(declaration);
52      }
53  
54      public NameDeclaration findVariableHere(PLSQLNameOccurrence occurrence) {
55          if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
56              return null;
57          }
58          ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
59          Applier.apply(finder, getVariableDeclarations().keySet().iterator());
60          return finder.getDecl();
61      }
62  
63      public String getName() {
64          return ( (AbstractPLSQLNode) node.jjtGetChild(1) ) .getCanonicalImage();
65      }
66  
67      public String toString() {
68          return "MethodScope:" + getVariableDeclarations().keySet();
69      }
70  }