View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package net.sourceforge.pmd.symboltable;
5   
6   public class Search {
7       private static final boolean TRACE = false;
8   
9       private NameOccurrence occ;
10      private NameDeclaration decl;
11  
12      public Search(NameOccurrence occ) {
13          if (TRACE)
14              System.out.println("new search for " + occ);
15          this.occ = occ;
16      }
17  
18      public void execute() {
19          decl = searchUpward(occ, occ.getScope());
20          if (TRACE)
21              System.out.println("found " + decl);
22      }
23  
24      public void execute(Scope startingScope) {
25          decl = searchUpward(occ, startingScope);
26          if (TRACE)
27              System.out.println("found " + decl);
28      }
29  
30      public NameDeclaration getResult() {
31          return decl;
32      }
33  
34      private NameDeclaration searchUpward(NameOccurrence nameOccurrence, Scope scope) {
35          if (!scope.contains(nameOccurrence) && scope.getParent() != null) {
36              if (TRACE)
37                  System.out.println("moving up fm " + scope + " to " + scope.getParent());
38              return searchUpward(nameOccurrence, scope.getParent());
39          }
40          if (scope.contains(nameOccurrence)) {
41              return scope.addVariableNameOccurrence(nameOccurrence);
42          }
43          return null;
44      }
45  }