KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > symboltable > OccurrenceFinder


1 package net.sourceforge.pmd.symboltable;
2
3 import net.sourceforge.pmd.ast.ASTPrimaryExpression;
4 import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 public class OccurrenceFinder extends JavaParserVisitorAdapter {
10
11     public Object JavaDoc visit(ASTPrimaryExpression node, Object JavaDoc data) {
12         NameFinder nameFinder = new NameFinder(node);
13
14         // Maybe do some sort of State pattern thingy for when NameDeclaration
15
// is null/not null?
16
NameDeclaration decl = null;
17
18         List JavaDoc names = nameFinder.getNames();
19         for (Iterator JavaDoc i = names.iterator(); i.hasNext();) {
20             NameOccurrence occ = (NameOccurrence) i.next();
21             Search search = new Search(occ);
22             if (decl == null) {
23                 // doing the first name lookup
24
search.execute();
25                 decl = search.getResult();
26                 if (decl == null) {
27                     // we can't find it, so just give up
28
// when we decide to do full symbol resolution
29
// force this to either find a symbol or throw a SymbolNotFoundException
30
break;
31                 }
32             } else {
33                 // now we've got a scope we're starting with, so work from there
34
search.execute(decl.getScope());
35                 decl = search.getResult();
36             }
37         }
38         return super.visit(node, data);
39     }
40
41 }
42
Popular Tags