test/tools/javac/tree/AbstractTreeScannerTest.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 683
bbc9765d9ec6
child 808
e8719f95f2d0
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

jjg@489 1 /*
ohair@554 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
jjg@489 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@489 4 *
jjg@489 5 * This code is free software; you can redistribute it and/or modify it
jjg@489 6 * under the terms of the GNU General Public License version 2 only, as
jjg@489 7 * published by the Free Software Foundation.
jjg@489 8 *
jjg@489 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@489 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@489 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@489 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@489 13 * accompanied this code).
jjg@489 14 *
jjg@489 15 * You should have received a copy of the GNU General Public License version
jjg@489 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@489 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@489 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@489 22 */
jjg@489 23
jjg@489 24 import java.io.*;
jjg@489 25 import java.lang.reflect.*;
jjg@489 26 import java.util.*;
jjg@489 27 import javax.tools.*;
jjg@489 28
jjg@489 29 import com.sun.source.tree.CompilationUnitTree;
jjg@679 30 import com.sun.source.tree.Tree;
jjg@489 31 import com.sun.source.util.JavacTask;
jjg@489 32 import com.sun.tools.javac.api.JavacTool;
jjg@679 33 import com.sun.tools.javac.tree.JCTree;
jjg@679 34 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
jjg@489 35 import com.sun.tools.javac.util.List;
jjg@489 36
jjg@679 37 public abstract class AbstractTreeScannerTest {
jjg@489 38
jjg@489 39 /**
jjg@489 40 * Run the program. A base directory can be provided for file arguments.
jjg@489 41 * In jtreg mode, the -r option can be given to change the default base
jjg@489 42 * directory to the test root directory. For other options, see usage().
jjg@489 43 * @param baseDir base directory for any file arguments.
jjg@489 44 * @param args command line args
jjg@489 45 * @return true if successful or in gui mode
jjg@489 46 */
jjg@489 47 boolean run(File baseDir, String... args) {
jjg@489 48 if (args.length == 0) {
jjg@489 49 usage(System.out);
jjg@489 50 return true;
jjg@489 51 }
jjg@489 52
jjg@489 53 ArrayList<File> files = new ArrayList<File>();
jjg@489 54 for (int i = 0; i < args.length; i++) {
jjg@489 55 String arg = args[i];
jjg@489 56 if (arg.equals("-q"))
jjg@489 57 quiet = true;
jjg@489 58 else if (arg.equals("-v"))
jjg@489 59 verbose = true;
jjg@489 60 else if (arg.equals("-r")) {
jjg@489 61 File d = baseDir;
jjg@489 62 while (!new File(d, "TEST.ROOT").exists()) {
jjg@489 63 d = d.getParentFile();
jjg@489 64 if (d == null)
jjg@489 65 throw new Error("cannot find TEST.ROOT");
jjg@489 66 }
jjg@489 67 baseDir = d;
jjg@489 68 }
jjg@489 69 else if (arg.startsWith("-"))
jjg@489 70 throw new Error("unknown option: " + arg);
jjg@489 71 else {
jjg@489 72 while (i < args.length)
jjg@489 73 files.add(new File(baseDir, args[i++]));
jjg@489 74 }
jjg@489 75 }
jjg@489 76
jjg@489 77 for (File file: files) {
jjg@489 78 if (file.exists())
jjg@489 79 test(file);
jjg@489 80 else
jjg@489 81 error("File not found: " + file);
jjg@489 82 }
jjg@489 83
jjg@489 84 if (fileCount != 1)
jjg@489 85 System.err.println(fileCount + " files read");
jjg@679 86 System.err.println(treeCount + " tree nodes compared");
jjg@489 87 if (errors > 0)
jjg@489 88 System.err.println(errors + " errors");
jjg@489 89
jjg@489 90 return (errors == 0);
jjg@489 91 }
jjg@489 92
jjg@489 93 /**
jjg@489 94 * Print command line help.
jjg@489 95 * @param out output stream
jjg@489 96 */
jjg@489 97 void usage(PrintStream out) {
jjg@489 98 out.println("Usage:");
jjg@679 99 out.println(" java " + getClass().getName() + " options... files...");
jjg@489 100 out.println("");
jjg@489 101 out.println("where options include:");
jjg@489 102 out.println("-q Quiet: don't report on inapplicable files");
jjg@489 103 out.println("-v Verbose: report on files as they are being read");
jjg@489 104 out.println("");
jjg@489 105 out.println("files may be directories or files");
jjg@489 106 out.println("directories will be scanned recursively");
jjg@489 107 out.println("non java files, or java files which cannot be parsed, will be ignored");
jjg@489 108 out.println("");
jjg@489 109 }
jjg@489 110
jjg@489 111 /**
jjg@489 112 * Test a file. If the file is a directory, it will be recursively scanned
jjg@489 113 * for java files.
jjg@489 114 * @param file the file or directory to test
jjg@489 115 */
jjg@489 116 void test(File file) {
jjg@489 117 if (file.isDirectory()) {
jjg@489 118 for (File f: file.listFiles()) {
jjg@489 119 test(f);
jjg@489 120 }
jjg@489 121 return;
jjg@489 122 }
jjg@489 123
jjg@489 124 if (file.isFile() && file.getName().endsWith(".java")) {
jjg@489 125 try {
jjg@489 126 if (verbose)
jjg@489 127 System.err.println(file);
jjg@489 128 fileCount++;
jjg@679 129 treeCount += test(read(file));
jjg@489 130 } catch (ParseException e) {
jjg@489 131 if (!quiet) {
jjg@489 132 error("Error parsing " + file + "\n" + e.getMessage());
jjg@489 133 }
jjg@489 134 } catch (IOException e) {
jjg@489 135 error("Error reading " + file + ": " + e);
jjg@489 136 }
jjg@489 137 return;
jjg@489 138 }
jjg@489 139
jjg@489 140 if (!quiet)
jjg@489 141 error("File " + file + " ignored");
jjg@489 142 }
jjg@489 143
jjg@679 144 abstract int test(JCCompilationUnit t);
jjg@679 145
jjg@489 146 /**
jjg@489 147 * Read a file.
jjg@489 148 * @param file the file to be read
jjg@489 149 * @return the tree for the content of the file
jjg@489 150 * @throws IOException if any IO errors occur
jjg@489 151 * @throws TreePosTest.ParseException if any errors occur while parsing the file
jjg@489 152 */
jjg@489 153 JCCompilationUnit read(File file) throws IOException, ParseException {
jjg@489 154 StringWriter sw = new StringWriter();
jjg@489 155 PrintWriter pw = new PrintWriter(sw);
jjg@489 156 Reporter r = new Reporter(pw);
jjg@489 157 JavacTool tool = JavacTool.create();
jjg@489 158 StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null);
jjg@489 159 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
jjg@489 160 JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
jjg@489 161 Iterable<? extends CompilationUnitTree> trees = task.parse();
jjg@489 162 pw.flush();
jjg@489 163 if (r.errors > 0)
jjg@489 164 throw new ParseException(sw.toString());
jjg@489 165 Iterator<? extends CompilationUnitTree> iter = trees.iterator();
jjg@489 166 if (!iter.hasNext())
jjg@489 167 throw new Error("no trees found");
jjg@489 168 JCCompilationUnit t = (JCCompilationUnit) iter.next();
jjg@489 169 if (iter.hasNext())
jjg@489 170 throw new Error("too many trees found");
jjg@489 171 return t;
jjg@489 172 }
jjg@489 173
jjg@489 174 /**
jjg@489 175 * Report an error. When the program is complete, the program will either
jjg@489 176 * exit or throw an Error if any errors have been reported.
jjg@489 177 * @param msg the error message
jjg@489 178 */
jjg@489 179 void error(String msg) {
jjg@489 180 System.err.println(msg);
jjg@489 181 errors++;
jjg@489 182 }
jjg@489 183
jjg@489 184 /**
jjg@683 185 * Report an error. When the program is complete, the program will either
jjg@683 186 * exit or throw an Error if any errors have been reported.
jjg@683 187 * @param msg the error message
jjg@683 188 */
jjg@683 189 void error(JavaFileObject file, String msg) {
jjg@683 190 System.err.println(file.getName() + ": " + msg);
jjg@683 191 errors++;
jjg@683 192 }
jjg@683 193
jjg@683 194 /**
jjg@489 195 * Report an error for a specific tree node.
jjg@489 196 * @param file the source file for the tree
jjg@489 197 * @param t the tree node
jjg@489 198 * @param label an indication of the error
jjg@489 199 */
jjg@679 200 void error(JavaFileObject file, Tree tree, String msg) {
jjg@679 201 JCTree t = (JCTree) tree;
jjg@489 202 error(file.getName() + ":" + getLine(file, t) + ": " + msg + " " + trim(t, 64));
jjg@489 203 }
jjg@489 204
jjg@489 205 /**
jjg@489 206 * Get a trimmed string for a tree node, with normalized white space and limited length.
jjg@489 207 */
jjg@679 208 String trim(Tree tree, int len) {
jjg@679 209 JCTree t = (JCTree) tree;
jjg@683 210 String s = t.toString().replaceAll("\\s+", " ");
jjg@489 211 return (s.length() < len) ? s : s.substring(0, len);
jjg@489 212 }
jjg@489 213
jjg@489 214 /** Number of files that have been analyzed. */
jjg@489 215 int fileCount;
jjg@679 216 /** Number of trees that have been successfully compared. */
jjg@679 217 int treeCount;
jjg@489 218 /** Number of errors reported. */
jjg@489 219 int errors;
jjg@489 220 /** Flag: don't report irrelevant files. */
jjg@489 221 boolean quiet;
jjg@489 222 /** Flag: report files as they are processed. */
jjg@489 223 boolean verbose;
jjg@489 224
jjg@489 225
jjg@489 226 /**
jjg@489 227 * Thrown when errors are found parsing a java file.
jjg@489 228 */
jjg@489 229 private static class ParseException extends Exception {
jjg@489 230 ParseException(String msg) {
jjg@489 231 super(msg);
jjg@489 232 }
jjg@489 233 }
jjg@489 234
jjg@489 235 /**
jjg@489 236 * DiagnosticListener to report diagnostics and count any errors that occur.
jjg@489 237 */
jjg@489 238 private static class Reporter implements DiagnosticListener<JavaFileObject> {
jjg@489 239 Reporter(PrintWriter out) {
jjg@489 240 this.out = out;
jjg@489 241 }
jjg@489 242
jjg@489 243 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
jjg@489 244 out.println(diagnostic);
jjg@489 245 switch (diagnostic.getKind()) {
jjg@489 246 case ERROR:
jjg@489 247 errors++;
jjg@489 248 }
jjg@489 249 }
jjg@489 250 int errors;
jjg@489 251 PrintWriter out;
jjg@489 252 }
jjg@489 253
jjg@489 254 /**
jjg@489 255 * Get the set of fields for a tree node that may contain child tree nodes.
jjg@489 256 * These are the fields that are subtypes of JCTree or List.
jjg@489 257 * The results are cached, based on the tree's tag.
jjg@489 258 */
jjg@489 259 Set<Field> getFields(JCTree tree) {
jjg@489 260 Set<Field> fields = map.get(tree.getTag());
jjg@489 261 if (fields == null) {
jjg@489 262 fields = new HashSet<Field>();
jjg@489 263 for (Field f: tree.getClass().getFields()) {
jjg@489 264 Class<?> fc = f.getType();
jjg@489 265 if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
jjg@489 266 fields.add(f);
jjg@489 267 }
jjg@489 268 map.put(tree.getTag(), fields);
jjg@489 269 }
jjg@489 270 return fields;
jjg@489 271 }
jjg@489 272 // where
jjg@489 273 Map<Integer, Set<Field>> map = new HashMap<Integer,Set<Field>>();
jjg@489 274
jjg@489 275 /** Get the line number for the primary position for a tree.
jjg@489 276 * The code is intended to be simple, although not necessarily efficient.
jjg@489 277 * However, note that a file manager such as JavacFileManager is likely
jjg@489 278 * to cache the results of file.getCharContent, avoiding the need to read
jjg@489 279 * the bits from disk each time this method is called.
jjg@489 280 */
jjg@489 281 int getLine(JavaFileObject file, JCTree tree) {
jjg@489 282 try {
jjg@489 283 CharSequence cs = file.getCharContent(true);
jjg@489 284 int line = 1;
jjg@489 285 for (int i = 0; i < tree.pos; i++) {
jjg@489 286 if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
jjg@489 287 line++;
jjg@489 288 }
jjg@489 289 return line;
jjg@489 290 } catch (IOException e) {
jjg@489 291 return -1;
jjg@489 292 }
jjg@489 293 }
jjg@489 294 }

mercurial