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