1
2
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 import net.sourceforge.pmd.lang.symboltable.Scope;
12
13 public class OccurrenceFinder extends JavaParserVisitorAdapter {
14
15 public Object visit(ASTPrimaryExpression node, Object data) {
16 NameFinder nameFinder = new NameFinder(node);
17
18
19
20 NameDeclaration decl = null;
21
22 List<JavaNameOccurrence> names = nameFinder.getNames();
23 for (JavaNameOccurrence occ : names) {
24 Search search = new Search(occ);
25 if (decl == null) {
26
27 search.execute();
28 decl = search.getResult();
29 if (decl == null) {
30
31
32
33
34 break;
35 }
36 } else {
37
38 Scope startingScope = decl.getScope();
39
40
41
42
43
44 if (decl instanceof VariableNameDeclaration) {
45 String typeImage = ((VariableNameDeclaration) decl).getTypeImage();
46 ClassNameDeclaration clazzDeclaration = startingScope.getEnclosingScope(SourceFileScope.class)
47 .findClassNameDeclaration(typeImage);
48 if (clazzDeclaration != null) {
49 startingScope = clazzDeclaration.getScope();
50 }
51 }
52 search.execute(startingScope);
53 decl = search.getResult();
54
55 if (decl == null) {
56
57
58
59
60
61
62
63
64
65
66
67
68
69 break;
70 }
71 }
72 }
73 return super.visit(node, data);
74 }
75
76 }