test/langtools/tools/javac/T6956638.java
changeset 47216 71c04702a3d5
parent 30730 d3ce7619db2c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    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
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.BufferedWriter;
       
    25 import java.io.File;
       
    26 import java.io.FileWriter;
       
    27 import java.io.IOException;
       
    28 import java.io.StringWriter;
       
    29 import java.util.Arrays;
       
    30 import java.util.Iterator;
       
    31 import java.util.List;
       
    32 
       
    33 import javax.lang.model.element.Element;
       
    34 import javax.tools.DiagnosticCollector;
       
    35 import javax.tools.JavaCompiler;
       
    36 import javax.tools.JavaFileObject;
       
    37 import javax.tools.StandardJavaFileManager;
       
    38 import javax.tools.ToolProvider;
       
    39 
       
    40 import com.sun.source.tree.CompilationUnitTree;
       
    41 import com.sun.source.util.JavacTask;
       
    42 
       
    43 /**
       
    44  * @test
       
    45  * @bug 6956638
       
    46  * @summary JavacTask.generate does not generate all required files
       
    47  * @modules jdk.compiler
       
    48  */
       
    49 public class T6956638 {
       
    50     public static void main(String[] args) throws Exception {
       
    51         new T6956638().run();
       
    52     }
       
    53 
       
    54     void run() throws Exception {
       
    55         File srcDir = new File("src");
       
    56 
       
    57         File[] files = {
       
    58             writeFile(new File(srcDir, "T1.java"),
       
    59                 "public class T1 extends T2 {}\n"),
       
    60             writeFile(new File(srcDir, "T2.java"),
       
    61                 "public class T2 extends T3 {}\n"),
       
    62             writeFile(new File(srcDir, "T3.java"),
       
    63                 "public class T3 { public static final int C = 1; }\n"),
       
    64             writeFile(new File(srcDir, "Test.java"),
       
    65                 "public class Test { public static final int D = T1.C; }\n")
       
    66         };
       
    67 
       
    68         for (File f1: files) {
       
    69             for (File f2: files) {
       
    70                 if (f2 == f1) continue;
       
    71                 for (File f3: files) {
       
    72                     if (f3 == f2 || f3 == f1) continue;
       
    73                     for (File f4: files) {
       
    74                         if (f4 == f3 || f4 == f2 || f4 == f1) continue;
       
    75                         try {
       
    76                             test(f1, f2, f3, f4);
       
    77                         } catch (Exception e) {
       
    78                             error(e);
       
    79                         }
       
    80                     }
       
    81                 }
       
    82             }
       
    83         }
       
    84 
       
    85         if (errors > 0)
       
    86             throw new Exception(errors + " tests failed");
       
    87     }
       
    88 
       
    89     void test(File... sourceFiles) throws Exception {
       
    90         System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
       
    91 
       
    92         File classesDir = new File("classes" + count);
       
    93         classesDir.mkdirs();
       
    94 
       
    95         StringWriter compilerOutputStream = new StringWriter();
       
    96 
       
    97         List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
       
    98         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    99         DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
       
   100         try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null)) {
       
   101             Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
       
   102             System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
       
   103             JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
       
   104             JavacTask javacTask = (JavacTask) task;
       
   105 
       
   106             Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
       
   107             Iterable<? extends Element> analyzedTrees = javacTask.analyze();
       
   108             Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
       
   109 
       
   110             System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
       
   111 
       
   112             System.err.print("3-");
       
   113             for (JavaFileObject f : generatedFiles)
       
   114                 System.err.print(" " + f);
       
   115             System.err.println("");
       
   116 
       
   117             System.err.print("5-");
       
   118             for (File f : classesDir.listFiles())
       
   119                 System.err.print(" " + f);
       
   120             System.err.println("");
       
   121 
       
   122             System.err.println("----");
       
   123             System.err.println(compilerOutputStream.toString());
       
   124 
       
   125             if (size(generatedFiles) != size(parsedTrees)) {
       
   126                 throw new Exception("wrong number of files generated: " + size(generatedFiles)
       
   127                         + " expected: " + size(parsedTrees));
       
   128             }
       
   129         }
       
   130     }
       
   131 
       
   132     private void error(Throwable t) {
       
   133         t.printStackTrace();
       
   134         errors++;
       
   135     }
       
   136 
       
   137     int count;
       
   138     int errors;
       
   139 
       
   140     private static <E> int size(Iterable<E> x) {
       
   141         int n = 0;
       
   142         for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n)
       
   143             iter.next();
       
   144         return n;
       
   145     }
       
   146 
       
   147     private static File writeFile(File f, String str) throws IOException {
       
   148         f.getParentFile().mkdirs();
       
   149         BufferedWriter fout = new BufferedWriter(new FileWriter(f));
       
   150         try {
       
   151             fout.write(str);
       
   152             fout.flush();
       
   153         } finally {
       
   154             fout.close();
       
   155         }
       
   156         return f;
       
   157     }
       
   158 }