View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.dfa;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   /**
10   * @author raik
11   */
12  // TODO This should be an enum?
13  public class NodeType {
14  
15      public static final int IF_EXPR = 1;
16      public static final int IF_LAST_STATEMENT = 2;
17      public static final int IF_LAST_STATEMENT_WITHOUT_ELSE = 3;
18      public static final int ELSE_LAST_STATEMENT = 4;
19  
20      public static final int WHILE_EXPR = 10;
21      public static final int WHILE_LAST_STATEMENT = 11;
22  
23      public static final int SWITCH_START = 20;
24      public static final int CASE_LAST_STATEMENT = 21;
25      public static final int SWITCH_LAST_DEFAULT_STATEMENT = 22;
26      public static final int SWITCH_END = 23;
27  
28      public static final int FOR_INIT = 30;
29      public static final int FOR_EXPR = 31;
30      public static final int FOR_UPDATE = 32;
31      public static final int FOR_BEFORE_FIRST_STATEMENT = 33;
32      public static final int FOR_END = 34;
33  
34      public static final int DO_BEFORE_FIRST_STATEMENT = 40;
35      public static final int DO_EXPR = 41;
36  
37      public static final int RETURN_STATEMENT = 50;
38      public static final int BREAK_STATEMENT = 51;
39      public static final int CONTINUE_STATEMENT = 52;
40  
41      public static final int LABEL_STATEMENT = 60;
42      public static final int LABEL_LAST_STATEMENT = 61;
43  
44      // TODO - throw statements?
45      public static final int THROW_STATEMENT = 70;
46  
47      // Poor Man's Enum until we convert the class to real enum
48      private static final Map<Integer, String> TYPE_MAP = new HashMap<>();
49      static {
50          TYPE_MAP.put(NodeType.IF_EXPR, "IF_EXPR");
51          TYPE_MAP.put(NodeType.IF_LAST_STATEMENT, "IF_LAST_STATEMENT");
52          TYPE_MAP.put(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, "IF_LAST_STATEMENT_WITHOUT_ELSE");
53          TYPE_MAP.put(NodeType.ELSE_LAST_STATEMENT, "ELSE_LAST_STATEMENT");
54          TYPE_MAP.put(NodeType.WHILE_LAST_STATEMENT, "WHILE_LAST_STATEMENT");
55          TYPE_MAP.put(NodeType.WHILE_EXPR, "WHILE_EXPR");
56          TYPE_MAP.put(NodeType.SWITCH_START, "SWITCH_START");
57          TYPE_MAP.put(NodeType.CASE_LAST_STATEMENT, "CASE_LAST_STATEMENT");
58          TYPE_MAP.put(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, "SWITCH_LAST_DEFAULT_STATEMENT");
59          TYPE_MAP.put(NodeType.SWITCH_END, "SWITCH_END");
60          TYPE_MAP.put(NodeType.FOR_INIT, "FOR_INIT");
61          TYPE_MAP.put(NodeType.FOR_EXPR, "FOR_EXPR");
62          TYPE_MAP.put(NodeType.FOR_UPDATE, "FOR_UPDATE");
63          TYPE_MAP.put(NodeType.FOR_BEFORE_FIRST_STATEMENT, "FOR_BEFORE_FIRST_STATEMENT");
64          TYPE_MAP.put(NodeType.FOR_END, "FOR_END");
65          TYPE_MAP.put(NodeType.DO_BEFORE_FIRST_STATEMENT, "DO_BEFORE_FIRST_STATEMENT");
66          TYPE_MAP.put(NodeType.DO_EXPR, "DO_EXPR");
67          TYPE_MAP.put(NodeType.RETURN_STATEMENT, "RETURN_STATEMENT");
68          TYPE_MAP.put(NodeType.BREAK_STATEMENT, "BREAK_STATEMENT");
69          TYPE_MAP.put(NodeType.CONTINUE_STATEMENT, "CONTINUE_STATEMENT");
70          TYPE_MAP.put(NodeType.LABEL_STATEMENT, "LABEL_STATEMENT");
71          TYPE_MAP.put(NodeType.LABEL_LAST_STATEMENT, "LABEL_END");
72          TYPE_MAP.put(NodeType.THROW_STATEMENT, "THROW_STATEMENT");
73      }
74  
75      public static Map<Integer, String> getTypeMap() {
76          return TYPE_MAP;
77      }
78  
79      public static String stringFromType(int intype) {
80          if (-1 == intype) {
81              return "<ROOT>";
82          }
83          if (!TYPE_MAP.containsKey(intype)) {
84              throw new RuntimeException("Couldn't find NodeType type id " + intype);
85          }
86          return TYPE_MAP.get(intype);
87      }
88  }