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