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.logging.Level;
7   import java.util.logging.Logger;
8   
9   import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
10  import net.sourceforge.pmd.lang.symboltable.Scope;
11  
12  public class Search {
13      private final static Logger LOGGER = Logger.getLogger(Search.class.getName()); 
14  
15      private PLSQLNameOccurrence occ;
16      private NameDeclaration decl;
17  
18      public Search(PLSQLNameOccurrence occ) {
19          if (LOGGER.isLoggable(Level.FINEST)) {
20              LOGGER.finest("new search for " + (occ.isMethodOrConstructorInvocation() ? "method" : "variable") + " " + occ);
21          }
22          this.occ = occ;
23      }
24  
25      public void execute() {
26          decl = searchUpward(occ, occ.getLocation().getScope());
27          if (LOGGER.isLoggable(Level.FINEST)) {
28              LOGGER.finest("found " + decl);
29          }
30      }
31  
32      public void execute(Scope startingScope) {
33          decl = searchUpward(occ, startingScope);
34          if (LOGGER.isLoggable(Level.FINEST)) {
35              LOGGER.finest("found " + decl);
36          }
37      }
38  
39      public NameDeclaration getResult() {
40          return decl;
41      }
42  
43      private NameDeclaration searchUpward(PLSQLNameOccurrence nameOccurrence, Scope scope) {
44          if (LOGGER.isLoggable(Level.FINEST)) {
45              LOGGER.finest("checking scope " + scope + " for name occurrence " + nameOccurrence);
46          }
47          if (!scope.contains(nameOccurrence) && scope.getParent() != null) {
48              if (LOGGER.isLoggable(Level.FINEST)) {
49          	LOGGER.finest("moving up fm " + scope + " to " + scope.getParent());
50              }
51              return searchUpward(nameOccurrence, scope.getParent());
52          }
53          if (scope.contains(nameOccurrence)) {
54              if (LOGGER.isLoggable(Level.FINEST)) {
55          	LOGGER.finest("found it!");
56              }
57              return scope.addNameOccurrence(nameOccurrence);
58          }
59          return null;
60      }
61  }