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.ASTArguments;
    7   import net.sourceforge.pmd.ast.ASTMemberSelector;
    8   import net.sourceforge.pmd.ast.ASTName;
    9   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
    10  import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
    11  import net.sourceforge.pmd.ast.ASTPrimarySuffix;
    12  import net.sourceforge.pmd.ast.SimpleNode;
    13
    14  import java.util.LinkedList;
    15  import java.util.List;
    16  import java.util.StringTokenizer;
    17
    18  public class NameFinder {
    19
    20      private LinkedList<NameOccurrence> names = new LinkedList<NameOccurrence>();
    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<NameOccurrence> 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) {
    49              ASTPrimarySuffix suffix = (ASTPrimarySuffix) node;
    50              if (suffix.isArguments()) {
    51                  NameOccurrence occurrence = names.getLast();
    52                  occurrence.setIsMethodOrConstructorInvocation();
    53                  ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node).jjtGetChild(0);
    54                  occurrence.setArgumentCount(args.getArgumentCount());
    55              } else if (suffix.jjtGetNumChildren() == 1 && suffix.jjtGetChild(0) instanceof ASTMemberSelector) {
    56                  add(new NameOccurrence((SimpleNode)suffix.jjtGetChild(0), ((SimpleNode)suffix.jjtGetChild(0)).getImage()));
    57              }
    58          }
    59      }
    60
    61      private void add(NameOccurrence name) {
    62          names.add(name);
    63          if (names.size() > 1) {
    64              NameOccurrence qualifiedName = names.get(names.size() - 2);
    65              qualifiedName.setNameWhichThisQualifies(name);
    66          }
    67      }
    68
    69
    70      public String toString() {
    71          StringBuffer result = new StringBuffer();
    72          for (NameOccurrence occ: names) {
    73              result.append(occ.getImage());
    74          }
    75          return result.toString();
    76      }
    77  }