1 /* |
1 /* |
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
52 import java.io.PrintWriter; |
52 import java.io.PrintWriter; |
53 import java.io.StringWriter; |
53 import java.io.StringWriter; |
54 import java.lang.reflect.Field; |
54 import java.lang.reflect.Field; |
55 import java.util.ArrayList; |
55 import java.util.ArrayList; |
56 import java.util.Arrays; |
56 import java.util.Arrays; |
57 import java.util.concurrent.atomic.AtomicInteger; |
|
58 import java.util.HashSet; |
57 import java.util.HashSet; |
59 import java.util.List; |
58 import java.util.List; |
60 import java.util.Set; |
59 import java.util.Set; |
|
60 import java.util.concurrent.atomic.AtomicInteger; |
61 |
61 |
62 import javax.lang.model.element.Element; |
62 import javax.lang.model.element.Element; |
63 import javax.swing.DefaultComboBoxModel; |
63 import javax.swing.DefaultComboBoxModel; |
64 import javax.swing.JComboBox; |
64 import javax.swing.JComboBox; |
65 import javax.swing.JComponent; |
65 import javax.swing.JComponent; |
78 import javax.tools.Diagnostic; |
78 import javax.tools.Diagnostic; |
79 import javax.tools.DiagnosticListener; |
79 import javax.tools.DiagnosticListener; |
80 import javax.tools.JavaFileObject; |
80 import javax.tools.JavaFileObject; |
81 |
81 |
82 import com.sun.source.tree.CompilationUnitTree; |
82 import com.sun.source.tree.CompilationUnitTree; |
|
83 import com.sun.source.util.JavacTask; |
83 import com.sun.source.util.TaskEvent; |
84 import com.sun.source.util.TaskEvent; |
84 import com.sun.source.util.JavacTask; |
|
85 import com.sun.source.util.TaskListener; |
85 import com.sun.source.util.TaskListener; |
|
86 import com.sun.tools.javac.api.JavacTaskImpl; |
86 import com.sun.tools.javac.code.Symbol; |
87 import com.sun.tools.javac.code.Symbol; |
87 import com.sun.tools.javac.code.Type; |
88 import com.sun.tools.javac.code.Type; |
|
89 import com.sun.tools.javac.main.JavaCompiler; |
88 import com.sun.tools.javac.tree.EndPosTable; |
90 import com.sun.tools.javac.tree.EndPosTable; |
89 import com.sun.tools.javac.tree.JCTree; |
91 import com.sun.tools.javac.tree.JCTree; |
90 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
92 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
91 import com.sun.tools.javac.tree.JCTree.JCImport; |
93 import com.sun.tools.javac.tree.JCTree.JCImport; |
92 import com.sun.tools.javac.tree.TreeInfo; |
94 import com.sun.tools.javac.tree.TreeInfo; |
270 /** |
273 /** |
271 * Read a file. |
274 * Read a file. |
272 * @param file the file to be read |
275 * @param file the file to be read |
273 * @return the tree for the content of the file |
276 * @return the tree for the content of the file |
274 * @throws IOException if any IO errors occur |
277 * @throws IOException if any IO errors occur |
275 * @throws TreePosTest.ParseException if any errors occur while parsing the file |
278 * @throws AttributionException if any errors occur while analyzing the file |
276 */ |
279 */ |
277 List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException { |
280 List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException { |
278 r.errors = 0; |
281 r.errors = 0; |
279 Iterable<? extends JavaFileObject> files = fm.get().getJavaFileObjects(file); |
282 Iterable<? extends JavaFileObject> files = fm.get().getJavaFileObjects(file); |
280 String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" }; |
283 String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" }; |
281 JavacTask task = (JavacTask)comp.getTask(pw, fm.get(), r, Arrays.asList(opts), null, files); |
284 JavacTask task = (JavacTask)comp.getTask(pw, fm.get(), r, Arrays.asList(opts), null, files); |
282 final List<Element> analyzedElems = new ArrayList<>(); |
285 final List<Element> analyzedElems = new ArrayList<>(); |
283 task.setTaskListener(new TaskListener() { |
286 task.setTaskListener(new TaskListener() { |
284 public void started(TaskEvent e) { |
287 public void started(TaskEvent e) { |
285 if (e.getKind() == TaskEvent.Kind.ANALYZE) |
288 if (e.getKind() == TaskEvent.Kind.ANALYZE) |
286 analyzedElems.add(e.getTypeElement()); |
289 analyzedElems.add(e.getTypeElement()); |
287 } |
290 } |
288 public void finished(TaskEvent e) { } |
291 public void finished(TaskEvent e) { } |
289 }); |
292 }); |
290 |
293 |
291 try { |
294 try { |
292 Iterable<? extends CompilationUnitTree> trees = task.parse(); |
295 Iterable<? extends CompilationUnitTree> trees = task.parse(); |
293 task.analyze(); |
296 // JavaCompiler c = JavaCompiler.instance(((JavacTaskImpl) task).getContext()); |
|
297 // System.err.println("verboseCompilePolicy: " + c.verboseCompilePolicy); |
|
298 // System.err.println("shouldStopIfError: " + c.shouldStopPolicyIfError); |
|
299 // System.err.println("shouldStopIfNoError: " + c.shouldStopPolicyIfNoError); |
|
300 Iterable<? extends Element> elems = task.analyze(); |
|
301 if (!elems.iterator().hasNext()) |
|
302 throw new AttributionException("No results from analyze"); |
294 List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>(); |
303 List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>(); |
295 //System.out.println("Try to add pairs. Elems are " + analyzedElems); |
304 //System.err.println("Try to add pairs. Elems are " + analyzedElems); |
296 for (CompilationUnitTree t : trees) { |
305 for (CompilationUnitTree t : trees) { |
297 JCCompilationUnit cu = (JCCompilationUnit)t; |
306 JCCompilationUnit cu = (JCCompilationUnit)t; |
298 for (JCTree def : cu.defs) { |
307 for (JCTree def : cu.defs) { |
299 if (def.hasTag(CLASSDEF) && |
308 if (def.hasTag(CLASSDEF) && |
300 analyzedElems.contains(((JCTree.JCClassDecl)def).sym)) { |
309 analyzedElems.contains(((JCTree.JCClassDecl)def).sym)) { |
301 //System.out.println("Adding pair..."); |
310 //System.err.println("Adding pair..." + cu.sourcefile + " " + ((JCTree.JCClassDecl) def).name); |
302 res.add(new Pair<>(cu, def)); |
311 res.add(new Pair<>(cu, def)); |
303 } |
312 } |
304 } |
313 } |
305 } |
314 } |
306 return res; |
315 return res; |
314 * Report an error. When the program is complete, the program will either |
323 * Report an error. When the program is complete, the program will either |
315 * exit or throw an Error if any errors have been reported. |
324 * exit or throw an Error if any errors have been reported. |
316 * @param msg the error message |
325 * @param msg the error message |
317 */ |
326 */ |
318 void error(String msg) { |
327 void error(String msg) { |
|
328 System.err.println(); |
319 System.err.println(msg); |
329 System.err.println(msg); |
|
330 System.err.println(); |
320 errCount.incrementAndGet(); |
331 errCount.incrementAndGet(); |
321 } |
332 } |
322 |
333 |
323 /** Flag: don't report irrelevant files. */ |
334 /** Flag: don't report irrelevant files. */ |
324 boolean quiet; |
335 boolean quiet; |
345 * left uninitialized after attribution |
356 * left uninitialized after attribution |
346 */ |
357 */ |
347 private class NPETester extends TreeScanner { |
358 private class NPETester extends TreeScanner { |
348 void test(List<Pair<JCCompilationUnit, JCTree>> trees) { |
359 void test(List<Pair<JCCompilationUnit, JCTree>> trees) { |
349 for (Pair<JCCompilationUnit, JCTree> p : trees) { |
360 for (Pair<JCCompilationUnit, JCTree> p : trees) { |
|
361 // System.err.println("checking " + p.fst.sourcefile); |
350 sourcefile = p.fst.sourcefile; |
362 sourcefile = p.fst.sourcefile; |
351 endPosTable = p.fst.endPositions; |
363 endPosTable = p.fst.endPositions; |
352 encl = new Info(p.snd, endPosTable); |
364 encl = new Info(p.snd, endPosTable); |
353 p.snd.accept(this); |
365 p.snd.accept(this); |
354 } |
366 } |
364 Info self = new Info(tree, endPosTable); |
376 Info self = new Info(tree, endPosTable); |
365 if (mandatoryType(tree)) { |
377 if (mandatoryType(tree)) { |
366 check(tree.type != null, |
378 check(tree.type != null, |
367 "'null' field 'type' found in tree ", self); |
379 "'null' field 'type' found in tree ", self); |
368 if (tree.type==null) |
380 if (tree.type==null) |
369 new Throwable().printStackTrace(); |
381 Thread.dumpStack(); |
370 } |
382 } |
371 |
383 |
372 Field errField = checkFields(tree); |
384 Field errField = checkFields(tree); |
373 if (errField!=null) { |
385 if (errField!=null) { |
374 check(false, |
386 check(false, |