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