View Javadoc
1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd.symboltable; 5 6 import net.sourceforge.pmd.ast.ASTName; 7 import net.sourceforge.pmd.ast.ASTPrimaryExpression; 8 import net.sourceforge.pmd.ast.ASTPrimaryPrefix; 9 import net.sourceforge.pmd.ast.ASTPrimarySuffix; 10 import net.sourceforge.pmd.ast.SimpleNode; 11 12 import java.util.ArrayList; 13 import java.util.Iterator; 14 import java.util.List; 15 import java.util.StringTokenizer; 16 17 public class NameOccurrences { 18 19 private List names = new ArrayList(); 20 21 public NameOccurrences(ASTPrimaryExpression node) { 22 buildOccurrences(node); 23 } 24 25 public List getNames() { 26 return names; 27 } 28 29 public Iterator iterator() { 30 return names.iterator(); 31 } 32 33 private void buildOccurrences(ASTPrimaryExpression node) { 34 ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node.jjtGetChild(0); 35 if (prefix.usesSuperModifier()) { 36 add(new NameOccurrence(prefix, "super")); 37 } else if (prefix.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 // TODO when is this null? 49 if (node.getImage() != null) { 50 add(new NameOccurrence(node, node.getImage())); 51 } 52 if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTName) { 53 ASTName grandchild = (ASTName) node.jjtGetChild(0); 54 for (StringTokenizer st = new StringTokenizer(grandchild.getImage(), "."); st.hasMoreTokens();) { 55 add(new NameOccurrence(grandchild, st.nextToken())); 56 } 57 } 58 if (node instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) node).isArguments()) { 59 ((NameOccurrence) names.get(names.size() - 1)).setIsMethodOrConstructorInvocation(); 60 } 61 } 62 63 private void add(NameOccurrence name) { 64 names.add(name); 65 if (names.size() > 1) { 66 NameOccurrence qualifiedName = (NameOccurrence) names.get(names.size() - 2); 67 qualifiedName.setNameWhichThisQualifies(name); 68 } 69 } 70 71 72 public String toString() { 73 String result = ""; 74 for (Iterator i = names.iterator(); i.hasNext();) { 75 NameOccurrence occ = (NameOccurrence) i.next(); 76 result += occ.getImage(); 77 } 78 return result; 79 } 80 }

This page was automatically generated by Maven