langtools/test/tools/javac/failover/CheckAttributedTree.java
changeset 15040 99fd9483d3f0
parent 14058 c7ec7facdd20
child 15384 5a8d00abf076
equal deleted inserted replaced
15039:80190ab051c0 15040:99fd9483d3f0
     1 /*
     1 /*
     2  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2010, 2013, 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.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import com.sun.source.util.TaskEvent;
    24 /*
       
    25  * @test
       
    26  * @bug 6970584
       
    27  * @summary assorted position errors in compiler syntax trees
       
    28  * @library ../lib
       
    29  * @build JavacTestingAbstractThreadedTest
       
    30  * @run main CheckAttributedTree -q -r -et ERRONEOUS .
       
    31  */
       
    32 
    25 import java.awt.BorderLayout;
    33 import java.awt.BorderLayout;
    26 import java.awt.Color;
    34 import java.awt.Color;
    27 import java.awt.Dimension;
    35 import java.awt.Dimension;
    28 import java.awt.EventQueue;
    36 import java.awt.EventQueue;
    29 import java.awt.Font;
    37 import java.awt.Font;
    32 import java.awt.Rectangle;
    40 import java.awt.Rectangle;
    33 import java.awt.event.ActionEvent;
    41 import java.awt.event.ActionEvent;
    34 import java.awt.event.ActionListener;
    42 import java.awt.event.ActionListener;
    35 import java.awt.event.MouseAdapter;
    43 import java.awt.event.MouseAdapter;
    36 import java.awt.event.MouseEvent;
    44 import java.awt.event.MouseEvent;
       
    45 import java.io.File;
       
    46 import java.io.IOException;
       
    47 import java.io.PrintStream;
       
    48 import java.io.PrintWriter;
       
    49 import java.io.StringWriter;
       
    50 import java.lang.reflect.Field;
       
    51 import java.util.ArrayList;
       
    52 import java.util.Arrays;
       
    53 import java.util.concurrent.atomic.AtomicInteger;
       
    54 import java.util.HashSet;
       
    55 import java.util.List;
       
    56 import java.util.Set;
       
    57 
       
    58 import javax.lang.model.element.Element;
    37 import javax.swing.DefaultComboBoxModel;
    59 import javax.swing.DefaultComboBoxModel;
    38 import javax.swing.JComboBox;
    60 import javax.swing.JComboBox;
    39 import javax.swing.JComponent;
    61 import javax.swing.JComponent;
    40 import javax.swing.JFrame;
    62 import javax.swing.JFrame;
    41 import javax.swing.JLabel;
    63 import javax.swing.JLabel;
    47 import javax.swing.event.CaretEvent;
    69 import javax.swing.event.CaretEvent;
    48 import javax.swing.event.CaretListener;
    70 import javax.swing.event.CaretListener;
    49 import javax.swing.text.BadLocationException;
    71 import javax.swing.text.BadLocationException;
    50 import javax.swing.text.DefaultHighlighter;
    72 import javax.swing.text.DefaultHighlighter;
    51 import javax.swing.text.Highlighter;
    73 import javax.swing.text.Highlighter;
    52 import java.io.File;
       
    53 import java.io.IOException;
       
    54 import java.io.PrintStream;
       
    55 import java.io.PrintWriter;
       
    56 import java.io.StringWriter;
       
    57 import java.lang.reflect.Field;
       
    58 import java.util.ArrayList;
       
    59 import java.util.List;
       
    60 import javax.tools.Diagnostic;
    74 import javax.tools.Diagnostic;
    61 import javax.tools.DiagnosticListener;
    75 import javax.tools.DiagnosticListener;
    62 import javax.tools.JavaFileObject;
    76 import javax.tools.JavaFileObject;
    63 import javax.tools.StandardJavaFileManager;
       
    64 
    77 
    65 import com.sun.source.tree.CompilationUnitTree;
    78 import com.sun.source.tree.CompilationUnitTree;
       
    79 import com.sun.source.util.TaskEvent;
    66 import com.sun.source.util.JavacTask;
    80 import com.sun.source.util.JavacTask;
    67 import com.sun.source.util.TaskListener;
    81 import com.sun.source.util.TaskListener;
    68 import com.sun.tools.javac.api.JavacTool;
       
    69 import com.sun.tools.javac.code.Symbol;
    82 import com.sun.tools.javac.code.Symbol;
    70 import com.sun.tools.javac.code.Type;
    83 import com.sun.tools.javac.code.Type;
    71 import com.sun.tools.javac.tree.EndPosTable;
    84 import com.sun.tools.javac.tree.EndPosTable;
    72 import com.sun.tools.javac.tree.JCTree;
    85 import com.sun.tools.javac.tree.JCTree;
    73 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    86 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    74 import com.sun.tools.javac.tree.JCTree.JCImport;
    87 import com.sun.tools.javac.tree.JCTree.JCImport;
    75 import com.sun.tools.javac.tree.TreeInfo;
    88 import com.sun.tools.javac.tree.TreeInfo;
    76 import com.sun.tools.javac.tree.TreeScanner;
    89 import com.sun.tools.javac.tree.TreeScanner;
    77 import com.sun.tools.javac.util.Pair;
    90 import com.sun.tools.javac.util.Pair;
    78 
       
    79 import java.util.Arrays;
       
    80 import java.util.HashSet;
       
    81 import java.util.Set;
       
    82 import javax.lang.model.element.Element;
       
    83 
    91 
    84 import static com.sun.tools.javac.tree.JCTree.Tag.*;
    92 import static com.sun.tools.javac.tree.JCTree.Tag.*;
    85 
    93 
    86 /**
    94 /**
    87  * Utility and test program to check validity of tree positions for tree nodes.
    95  * Utility and test program to check validity of tree positions for tree nodes.
    93  * jtreg: Note that by using the -r switch in the test description below, this test
   101  * jtreg: Note that by using the -r switch in the test description below, this test
    94  * will process all java files in the langtools/test directory, thus implicitly
   102  * will process all java files in the langtools/test directory, thus implicitly
    95  * covering any new language features that may be tested in this test suite.
   103  * covering any new language features that may be tested in this test suite.
    96  */
   104  */
    97 
   105 
    98 /*
   106 public class CheckAttributedTree extends JavacTestingAbstractThreadedTest {
    99  * @test
       
   100  * @bug 6970584
       
   101  * @summary assorted position errors in compiler syntax trees
       
   102  * @run main CheckAttributedTree -q -r -et ERRONEOUS .
       
   103  */
       
   104 public class CheckAttributedTree {
       
   105     /**
   107     /**
   106      * Main entry point.
   108      * Main entry point.
   107      * If test.src is set, program runs in jtreg mode, and will throw an Error
   109      * If test.src is set, program runs in jtreg mode, and will throw an Error
   108      * if any errors arise, otherwise System.exit will be used, unless the gui
   110      * if any errors arise, otherwise System.exit will be used, unless the gui
   109      * viewer is being used. In jtreg mode, the default base directory for file
   111      * viewer is being used. In jtreg mode, the default base directory for file
   110      * args is the value of ${test.src}. In jtreg mode, the -r option can be
   112      * args is the value of ${test.src}. In jtreg mode, the -r option can be
   111      * given to change the default base directory to the root test directory.
   113      * given to change the default base directory to the root test directory.
   112      */
   114      */
   113     public static void main(String... args) {
   115     public static void main(String... args) throws Exception {
   114         String testSrc = System.getProperty("test.src");
   116         String testSrc = System.getProperty("test.src");
   115         File baseDir = (testSrc == null) ? null : new File(testSrc);
   117         File baseDir = (testSrc == null) ? null : new File(testSrc);
       
   118         throwAssertionOnError = false;
   116         boolean ok = new CheckAttributedTree().run(baseDir, args);
   119         boolean ok = new CheckAttributedTree().run(baseDir, args);
   117         if (!ok) {
   120         if (!ok) {
   118             if (testSrc != null)  // jtreg mode
   121             if (testSrc != null)  // jtreg mode
   119                 throw new Error("failed");
   122                 throw new Error("failed");
   120             else
   123             else
   128      * directory to the test root directory. For other options, see usage().
   131      * directory to the test root directory. For other options, see usage().
   129      * @param baseDir base directory for any file arguments.
   132      * @param baseDir base directory for any file arguments.
   130      * @param args command line args
   133      * @param args command line args
   131      * @return true if successful or in gui mode
   134      * @return true if successful or in gui mode
   132      */
   135      */
   133     boolean run(File baseDir, String... args) {
   136     boolean run(File baseDir, String... args) throws Exception {
   134         if (args.length == 0) {
   137         if (args.length == 0) {
   135             usage(System.out);
   138             usage(System.out);
   136             return true;
   139             return true;
   137         }
   140         }
   138 
   141 
   143                 encoding = args[++i];
   146                 encoding = args[++i];
   144             else if (arg.equals("-gui"))
   147             else if (arg.equals("-gui"))
   145                 gui = true;
   148                 gui = true;
   146             else if (arg.equals("-q"))
   149             else if (arg.equals("-q"))
   147                 quiet = true;
   150                 quiet = true;
   148             else if (arg.equals("-v"))
   151             else if (arg.equals("-v")) {
   149                 verbose = true;
   152                 verbose = true;
       
   153                 printAll = true;
       
   154             }
   150             else if (arg.equals("-t") && i + 1 < args.length)
   155             else if (arg.equals("-t") && i + 1 < args.length)
   151                 tags.add(args[++i]);
   156                 tags.add(args[++i]);
   152             else if (arg.equals("-ef") && i + 1 < args.length)
   157             else if (arg.equals("-ef") && i + 1 < args.length)
   153                 excludeFiles.add(new File(baseDir, args[++i]));
   158                 excludeFiles.add(new File(baseDir, args[++i]));
   154             else if (arg.equals("-et") && i + 1 < args.length)
   159             else if (arg.equals("-et") && i + 1 < args.length)
   177                 test(file);
   182                 test(file);
   178             else
   183             else
   179                 error("File not found: " + file);
   184                 error("File not found: " + file);
   180         }
   185         }
   181 
   186 
   182         if (fileCount != 1)
   187         if (fileCount.get() != 1)
   183             System.err.println(fileCount + " files read");
   188             errWriter.println(fileCount + " files read");
   184         if (errors > 0)
   189         checkAfterExec(false);
   185             System.err.println(errors + " errors");
   190 
   186 
   191         return (gui || errCount.get() == 0);
   187         return (gui || errors == 0);
       
   188     }
   192     }
   189 
   193 
   190     /**
   194     /**
   191      * Print command line help.
   195      * Print command line help.
   192      * @param out output stream
   196      * @param out output stream
   213     /**
   217     /**
   214      * Test a file. If the file is a directory, it will be recursively scanned
   218      * Test a file. If the file is a directory, it will be recursively scanned
   215      * for java files.
   219      * for java files.
   216      * @param file the file or directory to test
   220      * @param file the file or directory to test
   217      */
   221      */
   218     void test(File file) {
   222     void test(final File file) {
   219         if (excludeFiles.contains(file)) {
   223         if (excludeFiles.contains(file)) {
   220             if (!quiet)
   224             if (!quiet)
   221                 error("File " + file + " excluded");
   225                 error("File " + file + " excluded");
   222             return;
   226             return;
   223         }
   227         }
   228             }
   232             }
   229             return;
   233             return;
   230         }
   234         }
   231 
   235 
   232         if (file.isFile() && file.getName().endsWith(".java")) {
   236         if (file.isFile() && file.getName().endsWith(".java")) {
   233             try {
   237             pool.execute(new Runnable() {
   234                 if (verbose)
   238                 @Override
   235                     System.err.println(file);
   239                 public void run() {
   236                 fileCount++;
   240                     try {
   237                 NPETester p = new NPETester();
   241                         if (verbose)
   238                 p.test(read(file));
   242                             errWriter.println(file);
   239             } catch (AttributionException e) {
   243                         fileCount.incrementAndGet();
   240                 if (!quiet) {
   244                         NPETester p = new NPETester();
   241                     error("Error attributing " + file + "\n" + e.getMessage());
   245                         p.test(read(file));
   242                 }
   246                     } catch (AttributionException e) {
   243             } catch (IOException e) {
   247                         if (!quiet) {
   244                 error("Error reading " + file + ": " + e);
   248                             error("Error attributing " + file + "\n" + e.getMessage());
   245             }
   249                         }
   246             return;
   250                     } catch (IOException e) {
       
   251                         error("Error reading " + file + ": " + e);
       
   252                     }
       
   253                 }
       
   254             });
   247         }
   255         }
   248 
   256 
   249         if (!quiet)
   257         if (!quiet)
   250             error("File " + file + " ignored");
   258             error("File " + file + " ignored");
   251     }
   259     }
   252 
   260 
   253     // See CR:  6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
   261     // See CR:  6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
   254     StringWriter sw = new StringWriter();
   262     StringWriter sw = new StringWriter();
   255     PrintWriter pw = new PrintWriter(sw);
   263     PrintWriter pw = new PrintWriter(sw);
   256     Reporter r = new Reporter(pw);
   264     Reporter r = new Reporter(pw);
   257     JavacTool tool = JavacTool.create();
       
   258     StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null);
       
   259 
   265 
   260     /**
   266     /**
   261      * Read a file.
   267      * Read a file.
   262      * @param file the file to be read
   268      * @param file the file to be read
   263      * @return the tree for the content of the file
   269      * @return the tree for the content of the file
   264      * @throws IOException if any IO errors occur
   270      * @throws IOException if any IO errors occur
   265      * @throws TreePosTest.ParseException if any errors occur while parsing the file
   271      * @throws TreePosTest.ParseException if any errors occur while parsing the file
   266      */
   272      */
   267     List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
   273     List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
   268         JavacTool tool = JavacTool.create();
       
   269         r.errors = 0;
   274         r.errors = 0;
   270         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
   275         Iterable<? extends JavaFileObject> files = fm.get().getJavaFileObjects(file);
   271         String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
   276         String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
   272         JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
   277         JavacTask task = (JavacTask)comp.getTask(pw, fm.get(), r, Arrays.asList(opts), null, files);
   273         final List<Element> analyzedElems = new ArrayList<>();
   278         final List<Element> analyzedElems = new ArrayList<>();
   274         task.setTaskListener(new TaskListener() {
   279         task.setTaskListener(new TaskListener() {
   275             public void started(TaskEvent e) {
   280             public void started(TaskEvent e) {
   276                 if (e.getKind() == TaskEvent.Kind.ANALYZE)
   281                 if (e.getKind() == TaskEvent.Kind.ANALYZE)
   277                         analyzedElems.add(e.getTypeElement());
   282                         analyzedElems.add(e.getTypeElement());
   306      * exit or throw an Error if any errors have been reported.
   311      * exit or throw an Error if any errors have been reported.
   307      * @param msg the error message
   312      * @param msg the error message
   308      */
   313      */
   309     void error(String msg) {
   314     void error(String msg) {
   310         System.err.println(msg);
   315         System.err.println(msg);
   311         errors++;
   316         errCount.incrementAndGet();
   312     }
   317     }
   313 
   318 
   314     /** Number of files that have been analyzed. */
       
   315     int fileCount;
       
   316     /** Number of errors reported. */
       
   317     int errors;
       
   318     /** Flag: don't report irrelevant files. */
   319     /** Flag: don't report irrelevant files. */
   319     boolean quiet;
   320     boolean quiet;
   320     /** Flag: show errors in GUI viewer. */
   321     /** Flag: show errors in GUI viewer. */
   321     boolean gui;
   322     boolean gui;
   322     /** The GUI viewer for errors. */
   323     /** The GUI viewer for errors. */
   383                 if (gui) {
   384                 if (gui) {
   384                     if (viewer == null)
   385                     if (viewer == null)
   385                         viewer = new Viewer();
   386                         viewer = new Viewer();
   386                     viewer.addEntry(sourcefile, label, encl, self);
   387                     viewer.addEntry(sourcefile, label, encl, self);
   387                 }
   388                 }
   388                 error(label + self.toString() + " encl: " + encl.toString() + " in file: " + sourcefile + "  " + self.tree);
   389                 error(label + self.toString() + " encl: " + encl.toString() +
       
   390                         " in file: " + sourcefile + "  " + self.tree);
   389             }
   391             }
   390         }
   392         }
   391 
   393 
   392         boolean checkFields(JCTree t) {
   394         boolean checkFields(JCTree t) {
   393             List<Field> fieldsToCheck = treeUtil.getFieldsOfType(t,
   395             List<Field> fieldsToCheck = treeUtil.getFieldsOfType(t,
   752             final String check;
   754             final String check;
   753             final Info encl;
   755             final Info encl;
   754             final Info self;
   756             final Info self;
   755         }
   757         }
   756     }
   758     }
       
   759 
       
   760     /** Number of files that have been analyzed. */
       
   761     static AtomicInteger fileCount = new AtomicInteger();
       
   762 
   757 }
   763 }