1 package net.sourceforge.pmd.symboltable;
2
3 import net.sourceforge.pmd.ast.ASTName;
4 import net.sourceforge.pmd.ast.ASTPrimaryExpression;
5 import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
6 import net.sourceforge.pmd.ast.ASTPrimarySuffix;
7 import net.sourceforge.pmd.ast.SimpleNode;
8
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.StringTokenizer;
13
14 public class NameOccurrences {
15
16 private List names = new ArrayList();
17
18 public NameOccurrences(ASTPrimaryExpression node) {
19 buildOccurrences(node);
20 }
21
22 public List getNames() {
23 return names;
24 }
25
26 public Iterator iterator() {
27 return names.iterator();
28 }
29
30 private void buildOccurrences(ASTPrimaryExpression node) {
31 ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node.jjtGetChild(0);
32 if (prefix.usesSuperModifier()) {
33 add(new NameOccurrence(prefix, "super"));
34 } else if (prefix.usesThisModifier()) {
35 add(new NameOccurrence(prefix, "this"));
36 }
37 checkForNameChild(prefix);
38
39 for (int i = 1; i < node.jjtGetNumChildren(); i++) {
40 checkForNameChild((ASTPrimarySuffix) node.jjtGetChild(i));
41 }
42 }
43
44 private void checkForNameChild(SimpleNode node) {
45 // TODO when is this null?
46 if (node.getImage() != null) {
47 add(new NameOccurrence(node, node.getImage()));
48 }
49 if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTName) {
50 ASTName grandchild = (ASTName) node.jjtGetChild(0);
51 for (StringTokenizer st = new StringTokenizer(grandchild.getImage(), "."); st.hasMoreTokens();) {
52 add(new NameOccurrence(grandchild, st.nextToken()));
53 }
54 }
55 if (node instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) node).isArguments()) {
56 ((NameOccurrence) names.get(names.size() - 1)).setIsMethodOrConstructorInvocation();
57 }
58 }
59
60 private void add(NameOccurrence name) {
61 names.add(name);
62 if (names.size() > 1) {
63 NameOccurrence qualifiedName = (NameOccurrence) names.get(names.size() - 2);
64 qualifiedName.setNameWhichThisQualifies(name);
65 }
66 }
67
68
69 public String toString() {
70 String result = "";
71 for (Iterator i = names.iterator(); i.hasNext();) {
72 NameOccurrence occ = (NameOccurrence) i.next();
73 result += occ.getImage();
74 }
75 return result;
76 }
77 }
This page was automatically generated by Maven