01: package org.acm.seguin.pmd.symboltable;
02:
03: import net.sourceforge.jrefactory.ast.ASTName;
04: import net.sourceforge.jrefactory.ast.ASTPrimaryExpression;
05: import net.sourceforge.jrefactory.ast.ASTPrimaryPrefix;
06: import net.sourceforge.jrefactory.ast.ASTPrimarySuffix;
07: import net.sourceforge.jrefactory.ast.SimpleNode;
08:
09: 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
32: .jjtGetFirstChild();
33: if (prefix.usesSuperModifier()) {
34: //System.err.println("usesSuperModifier");
35: add(new NameOccurrence(prefix, "super"));
36: } else if (prefix.usesThisModifier()) {
37: //System.err.println("usesThisModifier");
38: add(new NameOccurrence(prefix, "this"));
39: }
40: checkForNameChild(prefix);
41:
42: for (int i = 1; i < node.jjtGetNumChildren(); i++) {
43: checkForNameChild((ASTPrimarySuffix) node.jjtGetChild(i));
44: }
45: }
46:
47: private void checkForNameChild(SimpleNode node) {
48: //System.err.println("NameOccurrences.checkForNameChild("+node+"), node.getImage()="+node.getImage());
49: // TODO when is this null?
50: // MRA if (node.getImage() != null) {
51: if (!node.getImage().equals("")) {// MRA
52: add(new NameOccurrence(node, node.getImage()));
53: }
54: //System.err.println("node.jjtGetNumChildren()="+node.jjtGetNumChildren());
55: if (node.jjtGetNumChildren() > 0
56: && node.jjtGetFirstChild() instanceof ASTName) {
57: ASTName grandchild = (ASTName) node.jjtGetFirstChild();
58: //System.err.println(" node.jjtGetFirstChild().getImage()="+grandchild.getImage());
59: //System.err.println(" node.jjtGetFirstChild().getName()="+grandchild.getName());
60: //FIXME: MRA the following looks wrong,
61: for (StringTokenizer st = new StringTokenizer(grandchild
62: .getImage(), "."); st.hasMoreTokens();) {
63: NameOccurrence no = new NameOccurrence(grandchild, st
64: .nextToken());
65: //System.err.println(" - found "+no);
66: add(no);
67: }
68: }
69: if (node instanceof ASTPrimarySuffix
70: && ((ASTPrimarySuffix) node).isArguments()) {
71: ((NameOccurrence) names.get(names.size() - 1))
72: .setIsMethodOrConstructorInvocation();
73: }
74: }
75:
76: private void add(NameOccurrence name) {
77: //System.err.println("NameOccurrences.add("+name+")");
78: names.add(name);
79: if (names.size() > 1) {
80: NameOccurrence qualifiedName = (NameOccurrence) names
81: .get(names.size() - 2);
82: qualifiedName.setNameWhichThisQualifies(name);
83: }
84: }
85:
86: public String toString() {
87: String result = "";
88: for (Iterator i = names.iterator(); i.hasNext();) {
89: NameOccurrence occ = (NameOccurrence) i.next();
90: result += occ.getImage();
91: }
92: return result;
93: }
94: }
|