View Javadoc

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