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) System.out.println("new search for " + (occ.isMethodOrConstructorInvocation() ? "method" : "variable") + " " + occ);
14 this.occ = occ;
15 }
16
17 public void execute() {
18 decl = searchUpward(occ, occ.getLocation().getScope());
19 if (TRACE) System.out.println("found " + decl);
20 }
21
22 public void execute(Scope startingScope) {
23 decl = searchUpward(occ, startingScope);
24 if (TRACE) System.out.println("found " + decl);
25 }
26
27 public NameDeclaration getResult() {
28 return decl;
29 }
30
31 private NameDeclaration searchUpward(NameOccurrence nameOccurrence, Scope scope) {
32 if (TRACE) System.out.println("checking scope " + scope + " for name occurrence " + nameOccurrence);
33 if (!scope.contains(nameOccurrence) && scope.getParent() != null) {
34 if (TRACE) System.out.println("moving up fm " + scope + " to " + scope.getParent());
35 return searchUpward(nameOccurrence, scope.getParent());
36 }
37 if (scope.contains(nameOccurrence)) {
38 if (TRACE) System.out.println("found it!");
39 return scope.addVariableNameOccurrence(nameOccurrence);
40 }
41 return null;
42 }
43 }