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