24
import java.io.BufferedWriter;
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;
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;
49
public class T6956638 {
50
public static void main(String[] args) throws Exception {
54
void run() throws Exception {
55
File srcDir = new File("src");
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")
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;
77
} catch (Exception e) {
86
throw new Exception(errors + " tests failed");
89
void test(File... sourceFiles) throws Exception {
90
System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
92
File classesDir = new File("classes" + count);
95
StringWriter compilerOutputStream = new StringWriter();
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;
106
Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
107
Iterable<? extends Element> analyzedTrees = javacTask.analyze();
108
Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
110
System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
112
System.err.print("3-");
113
for (JavaFileObject f : generatedFiles)
114
System.err.print(" " + f);
115
System.err.println("");
117
System.err.print("5-");
118
for (File f : classesDir.listFiles())
119
System.err.print(" " + f);
120
System.err.println("");
122
System.err.println("----");
123
System.err.println(compilerOutputStream.toString());
125
if (size(generatedFiles) != size(parsedTrees)) {
126
throw new Exception("wrong number of files generated: " + size(generatedFiles)
127
+ " expected: " + size(parsedTrees));
132
private void error(Throwable t) {
140
private static <E> int size(Iterable<E> x) {
142
for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n)
147
private static File writeFile(File f, String str) throws IOException {
148
f.getParentFile().mkdirs();
149
BufferedWriter fout = new BufferedWriter(new FileWriter(f));