01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.symboltable;
04:
05: public class Search {
06: private static final boolean TRACE = false;
07:
08: private NameOccurrence occ;
09: private NameDeclaration decl;
10:
11: public Search(NameOccurrence occ) {
12: if (TRACE)
13: System.out.println("new search for "
14: + (occ.isMethodOrConstructorInvocation() ? "method"
15: : "variable") + " " + occ);
16: this .occ = occ;
17: }
18:
19: public void execute() {
20: decl = searchUpward(occ, occ.getLocation().getScope());
21: if (TRACE)
22: System.out.println("found " + decl);
23: }
24:
25: public void execute(Scope startingScope) {
26: decl = searchUpward(occ, startingScope);
27: if (TRACE)
28: System.out.println("found " + decl);
29: }
30:
31: public NameDeclaration getResult() {
32: return decl;
33: }
34:
35: private NameDeclaration searchUpward(NameOccurrence nameOccurrence,
36: Scope scope) {
37: if (TRACE)
38: System.out.println("checking scope " + scope
39: + " for name occurrence " + nameOccurrence);
40: if (!scope.contains(nameOccurrence)
41: && scope.getParent() != null) {
42: if (TRACE)
43: System.out.println("moving up fm " + scope + " to "
44: + scope.getParent());
45: return searchUpward(nameOccurrence, scope.getParent());
46: }
47: if (scope.contains(nameOccurrence)) {
48: if (TRACE)
49: System.out.println("found it!");
50: return scope.addVariableNameOccurrence(nameOccurrence);
51: }
52: return null;
53: }
54: }
|