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.symboltable.AbstractScope;
12  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
13  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
14  
15  public class LocalScope extends AbstractScope {
16  
17      @Override
18      public NameDeclaration addNameOccurrence(NameOccurrence occ) {
19          PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence)occ;
20          NameDeclaration decl = findVariableHere(occurrence);
21          if (decl != null && !occurrence.isThisOrSuper()) {
22              List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
23              nameOccurrences.add(occurrence);
24              Node n = occurrence.getLocation();
25              if (n instanceof ASTName) {
26                  ((ASTName) n).setNameDeclaration(decl);
27              } // TODO what to do with PrimarySuffix case?
28          }
29          return decl;
30      }
31  
32      public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() {
33          return getDeclarations(VariableNameDeclaration.class);
34      }
35  
36      @Override
37      public void addDeclaration(NameDeclaration declaration) {
38          if (declaration instanceof VariableNameDeclaration && getDeclarations().keySet().contains(declaration)) {
39              throw new RuntimeException(declaration + " is already in the symbol table");
40          }
41          super.addDeclaration(declaration);
42      }
43  
44      public NameDeclaration findVariableHere(PLSQLNameOccurrence occurrence) {
45          if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
46              return null;
47          }
48          ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
49          Applier.apply(finder, getVariableDeclarations().keySet().iterator());
50          return finder.getDecl();
51      }
52  
53      public String toString() {
54          return "LocalScope:" + getVariableDeclarations().keySet();
55      }
56  }