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