View Javadoc

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