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