test/tools/javac/T6956638.java

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

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 0
959103a6100f
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2010, 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  */
    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;
    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;
    40 import com.sun.source.tree.CompilationUnitTree;
    41 import com.sun.source.util.JavacTask;
    43 /**
    44  * @test
    45  * @bug 6956638
    46  * @summary JavacTask.generate does not generate all required files
    47  */
    48 public class T6956638 {
    49     public static void main(String[] args) throws Exception {
    50         new T6956638().run();
    51     }
    53     void run() throws Exception {
    54         File srcDir = new File("src");
    56         File[] files = {
    57             writeFile(new File(srcDir, "T1.java"),
    58                 "public class T1 extends T2 {}\n"),
    59             writeFile(new File(srcDir, "T2.java"),
    60                 "public class T2 extends T3 {}\n"),
    61             writeFile(new File(srcDir, "T3.java"),
    62                 "public class T3 { public static final int C = 1; }\n"),
    63             writeFile(new File(srcDir, "Test.java"),
    64                 "public class Test { public static final int D = T1.C; }\n")
    65         };
    67         for (File f1: files) {
    68             for (File f2: files) {
    69                 if (f2 == f1) continue;
    70                 for (File f3: files) {
    71                     if (f3 == f2 || f3 == f1) continue;
    72                     for (File f4: files) {
    73                         if (f4 == f3 || f4 == f2 || f4 == f1) continue;
    74                         try {
    75                             test(f1, f2, f3, f4);
    76                         } catch (Exception e) {
    77                             error(e);
    78                         }
    79                     }
    80                 }
    81             }
    82         }
    84         if (errors > 0)
    85             throw new Exception(errors + " tests failed");
    86     }
    88     void test(File... sourceFiles) throws Exception {
    89         System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
    91         File classesDir = new File("classes" + count);
    92         classesDir.mkdirs();
    94         StringWriter compilerOutputStream = new StringWriter();
    96         List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
    97         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    98         DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
    99         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
   100         Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
   101         System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
   102         JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
   103         JavacTask javacTask = (JavacTask) task;
   105         Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
   106         Iterable<? extends Element> analyzedTrees = javacTask.analyze();
   107         Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
   109         System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
   111         System.err.print("3-");
   112         for (JavaFileObject f : generatedFiles)
   113             System.err.print(" " + f);
   114         System.err.println("");
   116         System.err.print("5-");
   117         for (File f : classesDir.listFiles())
   118             System.err.print(" " + f);
   119         System.err.println("");
   121         System.err.println("----");
   122         System.err.println(compilerOutputStream.toString());
   124         if (size(generatedFiles) != size(parsedTrees)) {
   125             throw new Exception("wrong number of files generated: " + size(generatedFiles)
   126                     + " expected: " + size(parsedTrees));
   127         }
   128     }
   130     private void error(Throwable t) {
   131         t.printStackTrace();
   132         errors++;
   133     }
   135     int count;
   136     int errors;
   138     private static <E> int size(Iterable<E> x) {
   139         int n = 0;
   140         for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n)
   141             iter.next();
   142         return n;
   143     }
   145     private static File writeFile(File f, String str) throws IOException {
   146         f.getParentFile().mkdirs();
   147         BufferedWriter fout = new BufferedWriter(new FileWriter(f));
   148         try {
   149             fout.write(str);
   150             fout.flush();
   151         } finally {
   152             fout.close();
   153         }
   154         return f;
   155     }
   156 }

mercurial