test/tools/javac/tree/TreePosTest.java

Print this page

        

@@ -335,11 +335,11 @@
             if (check(encl, self)) {
                 // Modifiers nodes are present throughout the tree even where
                 // there is no corresponding source text.
                 // Redundant semicolons in a class definition can cause empty
                 // initializer blocks with no positions.
-                if ((self.tag == JCTree.MODIFIERS || self.tag == JCTree.BLOCK)
+                if ((self.tag == JCTree.Tag.MODIFIERS || self.tag == JCTree.Tag.BLOCK)
                         && self.pos == NOPOS) {
                     // If pos is NOPOS, so should be the start and end positions
                     check("start == NOPOS", encl, self, self.start == NOPOS);
                     check("end == NOPOS", encl, self, self.end == NOPOS);
                 } else {

@@ -357,19 +357,19 @@
                     // and because of inconsistent nesting of left and right of
                     // array declarations:
                     //    e.g.    int[][] a = new int[2][];
                     check("encl.start <= start", encl, self, encl.start <= self.start);
                     check("start <= pos", encl, self, self.start <= self.pos);
-                    if (!(self.tag == JCTree.TYPEARRAY
-                            && (encl.tag == JCTree.VARDEF ||
-                                encl.tag == JCTree.METHODDEF ||
-                                encl.tag == JCTree.TYPEARRAY))) {
+                    if (!(self.tag == JCTree.Tag.TYPEARRAY
+                            && (encl.tag == JCTree.Tag.VARDEF ||
+                                encl.tag == JCTree.Tag.METHODDEF ||
+                                encl.tag == JCTree.Tag.TYPEARRAY))) {
                         check("encl.pos <= start || end <= encl.pos",
                                 encl, self, encl.pos <= self.start || self.end <= encl.pos);
                     }
                     check("pos <= end", encl, self, self.pos <= self.end);
-                    if (!(self.tag == JCTree.TYPEARRAY && encl.tag == JCTree.TYPEARRAY)) {
+                    if (!(self.tag == JCTree.Tag.TYPEARRAY && encl.tag == JCTree.Tag.TYPEARRAY)) {
                         check("end <= encl.end", encl, self, self.end <= encl.end);
                     }
                 }
             }
 

@@ -386,11 +386,11 @@
             // skip the synthesized bits and just check parts which came from
             // the original source text
             if ((tree.mods.flags & Flags.ENUM) != 0) {
                 scan(tree.mods);
                 if (tree.init != null) {
-                    if (tree.init.getTag() == JCTree.NEWCLASS) {
+                    if (tree.init.getTag() == JCTree.Tag.NEWCLASS) {
                         JCNewClass init = (JCNewClass) tree.init;
                         if (init.args != null && init.args.nonEmpty()) {
                             scan(init.args);
                         }
                         if (init.def != null && init.def.defs != null) {

@@ -437,11 +437,11 @@
      * Utility class providing easy access to position and other info for a tree node.
      */
     private class Info {
         Info() {
             tree = null;
-            tag = JCTree.ERRONEOUS;
+            tag = JCTree.Tag.ERRONEOUS;
             start = 0;
             pos = 0;
             end = Integer.MAX_VALUE;
         }
 

@@ -457,43 +457,24 @@
         public String toString() {
             return tagNames.get(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]";
         }
 
         final JCTree tree;
-        final int tag;
+        final JCTree.Tag tag;
         final int start;
         final int pos;
         final int end;
     }
 
     /**
      * Names for tree tags.
-     * javac does not provide an API to convert tag values to strings, so this class uses
-     * reflection to determine names of public static final int values in JCTree.
      */
     private static class TagNames {
-        String get(int tag) {
-            if (map == null) {
-                map = new HashMap<Integer, String>();
-                Class c = JCTree.class;
-                for (Field f : c.getDeclaredFields()) {
-                    if (f.getType().equals(int.class)) {
-                        int mods = f.getModifiers();
-                        if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
-                            try {
-                                map.put(f.getInt(null), f.getName());
-                            } catch (IllegalAccessException e) {
-                            }
-                        }
-                    }
-                }
-            }
-            String name = map.get(tag);
+        String get(JCTree.Tag tag) {
+            String name = tag.name();
             return (name == null) ? "??" : name;
         }
-
-        private Map<Integer, String> map;
     }
 
     /**
      * Thrown when errors are found parsing a java file.
      */