test/tools/javac/tree/TreePosTest.java

changeset 1127
ca49d50318dc
parent 808
e8719f95f2d0
child 1138
7375d4979bd3
     1.1 --- a/test/tools/javac/tree/TreePosTest.java	Sat Nov 05 00:02:33 2011 -0700
     1.2 +++ b/test/tools/javac/tree/TreePosTest.java	Tue Nov 08 11:51:05 2011 -0800
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -80,6 +80,7 @@
    1.11  import com.sun.tools.javac.tree.TreeInfo;
    1.12  import com.sun.tools.javac.tree.TreeScanner;
    1.13  
    1.14 +import static com.sun.tools.javac.tree.JCTree.Tag.*;
    1.15  import static com.sun.tools.javac.util.Position.NOPOS;
    1.16  
    1.17  /**
    1.18 @@ -291,6 +292,14 @@
    1.19          errors++;
    1.20      }
    1.21  
    1.22 +    /**
    1.23 +     * Names for tree tags.
    1.24 +     */
    1.25 +    private static String getTagName(JCTree.Tag tag) {
    1.26 +        String name = tag.name();
    1.27 +        return (name == null) ? "??" : name;
    1.28 +    }
    1.29 +
    1.30      /** Number of files that have been analyzed. */
    1.31      int fileCount;
    1.32      /** Number of errors reported. */
    1.33 @@ -312,8 +321,6 @@
    1.34      Set<File> excludeFiles = new HashSet<File>();
    1.35      /** Set of tag names to be excluded from analysis. */
    1.36      Set<String> excludeTags = new HashSet<String>();
    1.37 -    /** Table of printable names for tree tag values. */
    1.38 -    TagNames tagNames = new TagNames();
    1.39  
    1.40      /**
    1.41       * Main class for testing assertions concerning tree positions for tree nodes.
    1.42 @@ -337,7 +344,7 @@
    1.43                  // there is no corresponding source text.
    1.44                  // Redundant semicolons in a class definition can cause empty
    1.45                  // initializer blocks with no positions.
    1.46 -                if ((self.tag == JCTree.MODIFIERS || self.tag == JCTree.BLOCK)
    1.47 +                if ((self.tag == MODIFIERS || self.tag == BLOCK)
    1.48                          && self.pos == NOPOS) {
    1.49                      // If pos is NOPOS, so should be the start and end positions
    1.50                      check("start == NOPOS", encl, self, self.start == NOPOS);
    1.51 @@ -359,15 +366,15 @@
    1.52                      //    e.g.    int[][] a = new int[2][];
    1.53                      check("encl.start <= start", encl, self, encl.start <= self.start);
    1.54                      check("start <= pos", encl, self, self.start <= self.pos);
    1.55 -                    if (!(self.tag == JCTree.TYPEARRAY
    1.56 -                            && (encl.tag == JCTree.VARDEF ||
    1.57 -                                encl.tag == JCTree.METHODDEF ||
    1.58 -                                encl.tag == JCTree.TYPEARRAY))) {
    1.59 +                    if (!(self.tag == TYPEARRAY
    1.60 +                            && (encl.tag == VARDEF ||
    1.61 +                                encl.tag == METHODDEF ||
    1.62 +                                encl.tag == TYPEARRAY))) {
    1.63                          check("encl.pos <= start || end <= encl.pos",
    1.64                                  encl, self, encl.pos <= self.start || self.end <= encl.pos);
    1.65                      }
    1.66                      check("pos <= end", encl, self, self.pos <= self.end);
    1.67 -                    if (!(self.tag == JCTree.TYPEARRAY && encl.tag == JCTree.TYPEARRAY)) {
    1.68 +                    if (!(self.tag == TYPEARRAY && encl.tag == TYPEARRAY)) {
    1.69                          check("end <= encl.end", encl, self, self.end <= encl.end);
    1.70                      }
    1.71                  }
    1.72 @@ -388,7 +395,7 @@
    1.73              if ((tree.mods.flags & Flags.ENUM) != 0) {
    1.74                  scan(tree.mods);
    1.75                  if (tree.init != null) {
    1.76 -                    if (tree.init.getTag() == JCTree.NEWCLASS) {
    1.77 +                    if (tree.init.hasTag(NEWCLASS)) {
    1.78                          JCNewClass init = (JCNewClass) tree.init;
    1.79                          if (init.args != null && init.args.nonEmpty()) {
    1.80                              scan(init.args);
    1.81 @@ -404,11 +411,11 @@
    1.82  
    1.83          boolean check(Info encl, Info self) {
    1.84              if (excludeTags.size() > 0) {
    1.85 -                if (encl != null && excludeTags.contains(tagNames.get(encl.tag))
    1.86 -                        || excludeTags.contains(tagNames.get(self.tag)))
    1.87 +                if (encl != null && excludeTags.contains(getTagName(encl.tag))
    1.88 +                        || excludeTags.contains(getTagName(self.tag)))
    1.89                      return false;
    1.90              }
    1.91 -            return tags.size() == 0 || tags.contains(tagNames.get(self.tag));
    1.92 +            return tags.size() == 0 || tags.contains(getTagName(self.tag));
    1.93          }
    1.94  
    1.95          void check(String label, Info encl, Info self, boolean ok) {
    1.96 @@ -439,7 +446,7 @@
    1.97      private class Info {
    1.98          Info() {
    1.99              tree = null;
   1.100 -            tag = JCTree.ERRONEOUS;
   1.101 +            tag = ERRONEOUS;
   1.102              start = 0;
   1.103              pos = 0;
   1.104              end = Integer.MAX_VALUE;
   1.105 @@ -455,46 +462,17 @@
   1.106  
   1.107          @Override
   1.108          public String toString() {
   1.109 -            return tagNames.get(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]";
   1.110 +            return getTagName(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]";
   1.111          }
   1.112  
   1.113          final JCTree tree;
   1.114 -        final int tag;
   1.115 +        final JCTree.Tag tag;
   1.116          final int start;
   1.117          final int pos;
   1.118          final int end;
   1.119      }
   1.120  
   1.121      /**
   1.122 -     * Names for tree tags.
   1.123 -     * javac does not provide an API to convert tag values to strings, so this class uses
   1.124 -     * reflection to determine names of public static final int values in JCTree.
   1.125 -     */
   1.126 -    private static class TagNames {
   1.127 -        String get(int tag) {
   1.128 -            if (map == null) {
   1.129 -                map = new HashMap<Integer, String>();
   1.130 -                Class c = JCTree.class;
   1.131 -                for (Field f : c.getDeclaredFields()) {
   1.132 -                    if (f.getType().equals(int.class)) {
   1.133 -                        int mods = f.getModifiers();
   1.134 -                        if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
   1.135 -                            try {
   1.136 -                                map.put(f.getInt(null), f.getName());
   1.137 -                            } catch (IllegalAccessException e) {
   1.138 -                            }
   1.139 -                        }
   1.140 -                    }
   1.141 -                }
   1.142 -            }
   1.143 -            String name = map.get(tag);
   1.144 -            return (name == null) ? "??" : name;
   1.145 -        }
   1.146 -
   1.147 -        private Map<Integer, String> map;
   1.148 -    }
   1.149 -
   1.150 -    /**
   1.151       * Thrown when errors are found parsing a java file.
   1.152       */
   1.153      private static class ParseException extends Exception {
   1.154 @@ -719,7 +697,7 @@
   1.155  
   1.156              void setInfo(Info info) {
   1.157                  this.info = info;
   1.158 -                tagName.setText(tagNames.get(info.tag));
   1.159 +                tagName.setText(getTagName(info.tag));
   1.160                  start.setText(String.valueOf(info.start));
   1.161                  pos.setText(String.valueOf(info.pos));
   1.162                  end.setText(String.valueOf(info.end));

mercurial