View Javadoc

1   package net.sourceforge.pmd.lang.java.symboltable;
2   
3   import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
4   import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
5   
6   import java.util.List;
7   
8   public class OccurrenceFinder extends JavaParserVisitorAdapter {
9   
10      public Object visit(ASTPrimaryExpression node, Object data) {
11          NameFinder nameFinder = new NameFinder(node);
12  
13          // Maybe do some sort of State pattern thingy for when NameDeclaration
14          // is null/not null?
15          NameDeclaration decl = null;
16  
17          List<NameOccurrence> names = nameFinder.getNames();
18          for (NameOccurrence occ: names) {
19              Search search = new Search(occ);
20              if (decl == null) {
21                  // doing the first name lookup
22                  search.execute();
23                  decl = search.getResult();
24                  if (decl == null) {
25                      // we can't find it, so just give up
26                      // when we decide to do full symbol resolution
27                      // force this to either find a symbol or throw a SymbolNotFoundException
28                      break;
29                  }
30              } else {
31                  // now we've got a scope we're starting with, so work from there
32                  search.execute(decl.getScope());
33                  decl = search.getResult();
34  
35                  if (decl == null) {
36                      // nothing found
37                      // This seems to be a lack of type resolution here.
38                      // Theoretically we have the previous declaration node and know from there the Type of
39                      // the variable. The current occurrence (occ) should then be found in the declaration of
40                      // this type. The type however may or may not be known to PMD (see aux classpath).
41  
42                      // we can't find it, so just give up
43                      // when we decide to do full symbol resolution
44                      // force this to either find a symbol or throw a SymbolNotFoundException
45                      break;
46                  }
47              }
48          }
49          return super.visit(node, data);
50      }
51  
52  }