test/tools/javac/failover/CheckAttributedTree.java

Print this page

        

@@ -287,11 +287,11 @@
             List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
             //System.out.println("Try to add pairs. Elems are " + analyzedElems);
             for (CompilationUnitTree t : trees) {
                JCCompilationUnit cu = (JCCompilationUnit)t;
                for (JCTree def : cu.defs) {
-                   if (def.getTag() == JCTree.CLASSDEF &&
+                   if (def.getTag() == JCTree.Tag.CLASSDEF &&
                            analyzedElems.contains(((JCTree.JCClassDecl)def).sym)) {
                        //System.out.println("Adding pair...");
                        res.add(new Pair<>(cu, def));
                    }
                }

@@ -371,13 +371,13 @@
             encl = prevEncl;
         }
 
         private boolean mandatoryType(JCTree that) {
             return that instanceof JCTree.JCExpression ||
-                    that.getTag() == JCTree.VARDEF ||
-                    that.getTag() == JCTree.METHODDEF ||
-                    that.getTag() == JCTree.CLASSDEF;
+                    that.getTag() == JCTree.Tag.VARDEF ||
+                    that.getTag() == JCTree.Tag.METHODDEF ||
+                    that.getTag() == JCTree.Tag.CLASSDEF;
         }
 
         private final List<String> excludedFields = Arrays.asList("varargsElement");
 
         void check(boolean ok, String label, Info self) {

@@ -427,11 +427,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;
         }
 

@@ -447,39 +447,22 @@
         public String toString() {
             return treeUtil.nameFromTag(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 TreeUtil {
-        String nameFromTag(int tag) {
-            if (names == null) {
-                names = 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 {
-                                names.put(f.getInt(null), f.getName());
-                            } catch (IllegalAccessException e) {
-                            }
-                        }
-                    }
-                }
-            }
-            String name = names.get(tag);
+        String nameFromTag(JCTree.Tag tag) {
+            String name = tag.name();
             return (name == null) ? "??" : name;
         }
 
         List<Field> getFieldsOfType(JCTree t, List<String> excludeNames, Class<?>... types) {
             List<Field> buf = new ArrayList<Field>();

@@ -494,12 +477,10 @@
                     }
                 }
             }
             return buf;
         }
-
-        private Map<Integer, String> names;
     }
 
     /**
      * Thrown when errors are found parsing a java file.
      */