01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.symboltable;
04:
05: import net.sourceforge.pmd.ast.ASTArguments;
06: import net.sourceforge.pmd.ast.ASTName;
07: import net.sourceforge.pmd.ast.ASTPrimaryExpression;
08: import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
09: import net.sourceforge.pmd.ast.ASTPrimarySuffix;
10: import net.sourceforge.pmd.ast.SimpleNode;
11:
12: import java.util.LinkedList;
13: import java.util.List;
14: import java.util.StringTokenizer;
15:
16: public class NameFinder {
17:
18: private LinkedList<NameOccurrence> names = new LinkedList<NameOccurrence>();
19:
20: public NameFinder(ASTPrimaryExpression node) {
21: ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node
22: .jjtGetChild(0);
23: if (prefix.usesSuperModifier()) {
24: add(new NameOccurrence(prefix, "super"));
25: } else if (prefix.usesThisModifier()) {
26: add(new NameOccurrence(prefix, "this"));
27: }
28: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
29: checkForNameChild((SimpleNode) node.jjtGetChild(i));
30: }
31: }
32:
33: public List<NameOccurrence> getNames() {
34: return names;
35: }
36:
37: private void checkForNameChild(SimpleNode node) {
38: if (node.getImage() != null) {
39: add(new NameOccurrence(node, node.getImage()));
40: }
41: if (node.jjtGetNumChildren() > 0
42: && node.jjtGetChild(0) instanceof ASTName) {
43: ASTName grandchild = (ASTName) node.jjtGetChild(0);
44: for (StringTokenizer st = new StringTokenizer(grandchild
45: .getImage(), "."); st.hasMoreTokens();) {
46: add(new NameOccurrence(grandchild, st.nextToken()));
47: }
48: }
49: if (node instanceof ASTPrimarySuffix
50: && ((ASTPrimarySuffix) node).isArguments()) {
51: NameOccurrence occurrence = names.getLast();
52: occurrence.setIsMethodOrConstructorInvocation();
53: ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node)
54: .jjtGetChild(0);
55: occurrence.setArgumentCount(args.getArgumentCount());
56:
57: }
58: }
59:
60: private void add(NameOccurrence name) {
61: names.add(name);
62: if (names.size() > 1) {
63: NameOccurrence qualifiedName = names.get(names.size() - 2);
64: qualifiedName.setNameWhichThisQualifies(name);
65: }
66: }
67:
68: public String toString() {
69: StringBuffer result = new StringBuffer();
70: for (NameOccurrence occ : names) {
71: result.append(occ.getImage());
72: }
73: return result.toString();
74: }
75: }
|