584N/A/*
584N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
584N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
584N/A *
584N/A * This code is free software; you can redistribute it and/or modify it
584N/A * under the terms of the GNU General Public License version 2 only, as
584N/A * published by the Free Software Foundation.
584N/A *
584N/A * This code is distributed in the hope that it will be useful, but WITHOUT
584N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
584N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
584N/A * version 2 for more details (a copy is included in the LICENSE file that
584N/A * accompanied this code).
584N/A *
584N/A * You should have received a copy of the GNU General Public License version
584N/A * 2 along with this work; if not, write to the Free Software Foundation,
584N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
584N/A *
584N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
584N/A * or visit www.oracle.com if you need additional information or have any
584N/A * questions.
584N/A */
584N/A
584N/Aimport java.io.BufferedWriter;
584N/Aimport java.io.File;
584N/Aimport java.io.FileWriter;
584N/Aimport java.io.IOException;
584N/Aimport java.io.StringWriter;
584N/Aimport java.util.Arrays;
584N/Aimport java.util.Iterator;
584N/Aimport java.util.List;
584N/A
584N/Aimport javax.lang.model.element.Element;
584N/Aimport javax.tools.DiagnosticCollector;
584N/Aimport javax.tools.JavaCompiler;
584N/Aimport javax.tools.JavaFileObject;
584N/Aimport javax.tools.StandardJavaFileManager;
584N/Aimport javax.tools.ToolProvider;
584N/A
584N/Aimport com.sun.source.tree.CompilationUnitTree;
584N/Aimport com.sun.source.util.JavacTask;
584N/A
584N/A/**
584N/A * @test
584N/A * @bug 6956638
584N/A * @summary JavacTask.generate does not generate all required files
584N/A */
584N/Apublic class T6956638 {
584N/A public static void main(String[] args) throws Exception {
584N/A new T6956638().run();
584N/A }
584N/A
584N/A void run() throws Exception {
584N/A File srcDir = new File("src");
584N/A
584N/A File[] files = {
584N/A writeFile(new File(srcDir, "T1.java"),
584N/A "public class T1 extends T2 {}\n"),
584N/A writeFile(new File(srcDir, "T2.java"),
584N/A "public class T2 extends T3 {}\n"),
584N/A writeFile(new File(srcDir, "T3.java"),
584N/A "public class T3 { public static final int C = 1; }\n"),
584N/A writeFile(new File(srcDir, "Test.java"),
584N/A "public class Test { public static final int D = T1.C; }\n")
584N/A };
584N/A
584N/A for (File f1: files) {
584N/A for (File f2: files) {
584N/A if (f2 == f1) continue;
584N/A for (File f3: files) {
584N/A if (f3 == f2 || f3 == f1) continue;
584N/A for (File f4: files) {
584N/A if (f4 == f3 || f4 == f2 || f4 == f1) continue;
584N/A try {
584N/A test(f1, f2, f3, f4);
584N/A } catch (Exception e) {
584N/A error(e);
584N/A }
584N/A }
584N/A }
584N/A }
584N/A }
584N/A
584N/A if (errors > 0)
584N/A throw new Exception(errors + " tests failed");
584N/A }
584N/A
584N/A void test(File... sourceFiles) throws Exception {
584N/A System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
584N/A
584N/A File classesDir = new File("classes" + count);
584N/A classesDir.mkdirs();
584N/A
584N/A StringWriter compilerOutputStream = new StringWriter();
584N/A
584N/A List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
584N/A JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
584N/A DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
584N/A StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
584N/A Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
584N/A System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
584N/A JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
584N/A JavacTask javacTask = (JavacTask) task;
584N/A
584N/A Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
584N/A Iterable<? extends Element> analyzedTrees = javacTask.analyze();
584N/A Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
584N/A
584N/A System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
584N/A
584N/A System.err.print("3-");
584N/A for (JavaFileObject f : generatedFiles)
584N/A System.err.print(" " + f);
584N/A System.err.println("");
584N/A
584N/A System.err.print("5-");
584N/A for (File f : classesDir.listFiles())
584N/A System.err.print(" " + f);
584N/A System.err.println("");
584N/A
584N/A System.err.println("----");
584N/A System.err.println(compilerOutputStream.toString());
584N/A
584N/A if (size(generatedFiles) != size(parsedTrees)) {
584N/A throw new Exception("wrong number of files generated: " + size(generatedFiles)
584N/A + " expected: " + size(parsedTrees));
584N/A }
584N/A }
584N/A
584N/A private void error(Throwable t) {
584N/A t.printStackTrace();
584N/A errors++;
584N/A }
584N/A
584N/A int count;
584N/A int errors;
584N/A
584N/A private static <E> int size(Iterable<E> x) {
584N/A int n = 0;
584N/A for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n)
584N/A iter.next();
584N/A return n;
584N/A }
584N/A
584N/A private static File writeFile(File f, String str) throws IOException {
584N/A f.getParentFile().mkdirs();
584N/A BufferedWriter fout = new BufferedWriter(new FileWriter(f));
584N/A try {
584N/A fout.write(str);
584N/A fout.flush();
584N/A } finally {
584N/A fout.close();
584N/A }
584N/A return f;
584N/A }
584N/A}