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 (!scope.contains(nameOccurrence) && scope.getParent() != null) { 33 if (TRACE) System.out.println("moving up fm " + scope + " to " + scope.getParent()); 34 return searchUpward(nameOccurrence, scope.getParent()); 35 } 36 if (scope.contains(nameOccurrence)) { 37 if (TRACE) System.out.println("found it!"); 38 return scope.addVariableNameOccurrence(nameOccurrence); 39 } 40 return null; 41 } 42 }