Fri, 24 Apr 2020 03:58:51 +0100
Merge
jjg@489 | 1 | /* |
jjg@1127 | 2 | * Copyright (c) 2010, 2011, 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 | |
jjh@808 | 146 | // See CR: 6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout |
jjh@808 | 147 | StringWriter sw = new StringWriter(); |
jjh@808 | 148 | PrintWriter pw = new PrintWriter(sw); |
jjh@808 | 149 | Reporter r = new Reporter(pw); |
jjh@808 | 150 | JavacTool tool = JavacTool.create(); |
jjh@808 | 151 | StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null); |
jjh@808 | 152 | |
jjg@489 | 153 | /** |
jjg@489 | 154 | * Read a file. |
jjg@489 | 155 | * @param file the file to be read |
jjg@489 | 156 | * @return the tree for the content of the file |
jjg@489 | 157 | * @throws IOException if any IO errors occur |
jjg@489 | 158 | * @throws TreePosTest.ParseException if any errors occur while parsing the file |
jjg@489 | 159 | */ |
jjg@489 | 160 | JCCompilationUnit read(File file) throws IOException, ParseException { |
jjg@489 | 161 | JavacTool tool = JavacTool.create(); |
jjh@808 | 162 | r.errors = 0; |
jjg@489 | 163 | Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file); |
jjg@489 | 164 | JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files); |
jjg@489 | 165 | Iterable<? extends CompilationUnitTree> trees = task.parse(); |
jjg@489 | 166 | pw.flush(); |
jjg@489 | 167 | if (r.errors > 0) |
jjg@489 | 168 | throw new ParseException(sw.toString()); |
jjg@489 | 169 | Iterator<? extends CompilationUnitTree> iter = trees.iterator(); |
jjg@489 | 170 | if (!iter.hasNext()) |
jjg@489 | 171 | throw new Error("no trees found"); |
jjg@489 | 172 | JCCompilationUnit t = (JCCompilationUnit) iter.next(); |
jjg@489 | 173 | if (iter.hasNext()) |
jjg@489 | 174 | throw new Error("too many trees found"); |
jjg@489 | 175 | return t; |
jjg@489 | 176 | } |
jjg@489 | 177 | |
jjg@489 | 178 | /** |
jjg@489 | 179 | * Report an error. When the program is complete, the program will either |
jjg@489 | 180 | * exit or throw an Error if any errors have been reported. |
jjg@489 | 181 | * @param msg the error message |
jjg@489 | 182 | */ |
jjg@489 | 183 | void error(String msg) { |
jjg@489 | 184 | System.err.println(msg); |
jjg@489 | 185 | errors++; |
jjg@489 | 186 | } |
jjg@489 | 187 | |
jjg@489 | 188 | /** |
jjg@683 | 189 | * Report an error. When the program is complete, the program will either |
jjg@683 | 190 | * exit or throw an Error if any errors have been reported. |
jjg@683 | 191 | * @param msg the error message |
jjg@683 | 192 | */ |
jjg@683 | 193 | void error(JavaFileObject file, String msg) { |
jjg@683 | 194 | System.err.println(file.getName() + ": " + msg); |
jjg@683 | 195 | errors++; |
jjg@683 | 196 | } |
jjg@683 | 197 | |
jjg@683 | 198 | /** |
jjg@489 | 199 | * Report an error for a specific tree node. |
jjg@489 | 200 | * @param file the source file for the tree |
jjg@489 | 201 | * @param t the tree node |
jjg@489 | 202 | * @param label an indication of the error |
jjg@489 | 203 | */ |
jjg@679 | 204 | void error(JavaFileObject file, Tree tree, String msg) { |
jjg@679 | 205 | JCTree t = (JCTree) tree; |
jjg@489 | 206 | error(file.getName() + ":" + getLine(file, t) + ": " + msg + " " + trim(t, 64)); |
jjg@489 | 207 | } |
jjg@489 | 208 | |
jjg@489 | 209 | /** |
jjg@489 | 210 | * Get a trimmed string for a tree node, with normalized white space and limited length. |
jjg@489 | 211 | */ |
jjg@679 | 212 | String trim(Tree tree, int len) { |
jjg@679 | 213 | JCTree t = (JCTree) tree; |
jjg@683 | 214 | String s = t.toString().replaceAll("\\s+", " "); |
jjg@489 | 215 | return (s.length() < len) ? s : s.substring(0, len); |
jjg@489 | 216 | } |
jjg@489 | 217 | |
jjg@489 | 218 | /** Number of files that have been analyzed. */ |
jjg@489 | 219 | int fileCount; |
jjg@679 | 220 | /** Number of trees that have been successfully compared. */ |
jjg@679 | 221 | int treeCount; |
jjg@489 | 222 | /** Number of errors reported. */ |
jjg@489 | 223 | int errors; |
jjg@489 | 224 | /** Flag: don't report irrelevant files. */ |
jjg@489 | 225 | boolean quiet; |
jjg@489 | 226 | /** Flag: report files as they are processed. */ |
jjg@489 | 227 | boolean verbose; |
jjg@489 | 228 | |
jjg@489 | 229 | |
jjg@489 | 230 | /** |
jjg@489 | 231 | * Thrown when errors are found parsing a java file. |
jjg@489 | 232 | */ |
jjg@489 | 233 | private static class ParseException extends Exception { |
jjg@489 | 234 | ParseException(String msg) { |
jjg@489 | 235 | super(msg); |
jjg@489 | 236 | } |
jjg@489 | 237 | } |
jjg@489 | 238 | |
jjg@489 | 239 | /** |
jjg@489 | 240 | * DiagnosticListener to report diagnostics and count any errors that occur. |
jjg@489 | 241 | */ |
jjg@489 | 242 | private static class Reporter implements DiagnosticListener<JavaFileObject> { |
jjg@489 | 243 | Reporter(PrintWriter out) { |
jjg@489 | 244 | this.out = out; |
jjg@489 | 245 | } |
jjg@489 | 246 | |
jjg@489 | 247 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
jjg@489 | 248 | out.println(diagnostic); |
jjg@489 | 249 | switch (diagnostic.getKind()) { |
jjg@489 | 250 | case ERROR: |
jjg@489 | 251 | errors++; |
jjg@489 | 252 | } |
jjg@489 | 253 | } |
jjg@489 | 254 | int errors; |
jjg@489 | 255 | PrintWriter out; |
jjg@489 | 256 | } |
jjg@489 | 257 | |
jjg@489 | 258 | /** |
jjg@489 | 259 | * Get the set of fields for a tree node that may contain child tree nodes. |
jjg@489 | 260 | * These are the fields that are subtypes of JCTree or List. |
jjg@489 | 261 | * The results are cached, based on the tree's tag. |
jjg@489 | 262 | */ |
jjg@489 | 263 | Set<Field> getFields(JCTree tree) { |
jjg@489 | 264 | Set<Field> fields = map.get(tree.getTag()); |
jjg@489 | 265 | if (fields == null) { |
jjg@489 | 266 | fields = new HashSet<Field>(); |
jjg@489 | 267 | for (Field f: tree.getClass().getFields()) { |
jjg@489 | 268 | Class<?> fc = f.getType(); |
jjg@489 | 269 | if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc)) |
jjg@489 | 270 | fields.add(f); |
jjg@489 | 271 | } |
jjg@489 | 272 | map.put(tree.getTag(), fields); |
jjg@489 | 273 | } |
jjg@489 | 274 | return fields; |
jjg@489 | 275 | } |
jjg@489 | 276 | // where |
jjg@1127 | 277 | Map<JCTree.Tag, Set<Field>> map = new HashMap<JCTree.Tag,Set<Field>>(); |
jjg@489 | 278 | |
jjg@489 | 279 | /** Get the line number for the primary position for a tree. |
jjg@489 | 280 | * The code is intended to be simple, although not necessarily efficient. |
jjg@489 | 281 | * However, note that a file manager such as JavacFileManager is likely |
jjg@489 | 282 | * to cache the results of file.getCharContent, avoiding the need to read |
jjg@489 | 283 | * the bits from disk each time this method is called. |
jjg@489 | 284 | */ |
jjg@489 | 285 | int getLine(JavaFileObject file, JCTree tree) { |
jjg@489 | 286 | try { |
jjg@489 | 287 | CharSequence cs = file.getCharContent(true); |
jjg@489 | 288 | int line = 1; |
jjg@489 | 289 | for (int i = 0; i < tree.pos; i++) { |
jjg@489 | 290 | if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings |
jjg@489 | 291 | line++; |
jjg@489 | 292 | } |
jjg@489 | 293 | return line; |
jjg@489 | 294 | } catch (IOException e) { |
jjg@489 | 295 | return -1; |
jjg@489 | 296 | } |
jjg@489 | 297 | } |
jjg@489 | 298 | } |