View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package net.sourceforge.pmd.symboltable;
5   
6   import net.sourceforge.pmd.ast.ASTCompilationUnit;
7   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
8   import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
9   
10  import java.util.Iterator;
11  
12  public class SymbolFacade extends JavaParserVisitorAdapter {
13  
14      public void initializeWith(ASTCompilationUnit node) {
15          // first, traverse the AST and create all the scopes
16          BasicScopeCreationVisitor sc = new BasicScopeCreationVisitor(new BasicScopeFactory());
17          node.jjtAccept(sc, null);
18  
19          // traverse the AST and pick up all the declarations
20          DeclarationFinder df = new DeclarationFinder();
21          node.jjtAccept(df, null);
22  
23          // finally, traverse the AST and pick up all the name occurrences
24          node.jjtAccept(this, null);
25      }
26  
27      public Object visit(ASTPrimaryExpression node, Object data) {
28          NameOccurrences qualifiedNames = new NameOccurrences(node);
29          NameDeclaration decl = null;
30          for (Iterator i = qualifiedNames.iterator(); i.hasNext();) {
31              NameOccurrence occ = (NameOccurrence) i.next();
32              Search search = new Search(occ);
33              if (decl == null) {
34                  // doing the first name lookup
35                  search.execute();
36                  decl = search.getResult();
37                  if (decl == null) {
38                      // we can't find it, so just give up
39                      // when we decide searches across compilation units like a compiler would, we'll
40                      // force this to either find a symbol or throw a "cannot resolve symbol" Exception
41                      break;
42                  }
43              } else {
44                  // now we've got a scope we're starting with, so work from there
45                  search.execute(decl.getScope());
46                  decl = search.getResult();
47              }
48          }
49          return super.visit(node, data);
50      }
51  
52  }