1/*
2 * Copyright (c) 2011, 2015, 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 */
23
24/*
25 * @test
26 * @bug 7068451
27 * @summary Regression: javac compiles fixed sources against previous,
28 *              not current, version of generated sources
29 * @modules java.compiler
30 *          jdk.compiler
31 */
32
33import java.io.File;
34import java.io.FileNotFoundException;
35import java.io.FileWriter;
36import java.io.IOException;
37import java.io.Writer;
38import java.nio.file.NoSuchFileException;
39import java.util.Arrays;
40import java.util.Collections;
41import java.util.List;
42import java.util.Set;
43import javax.annotation.processing.AbstractProcessor;
44import javax.annotation.processing.Filer;
45import javax.annotation.processing.Messager;
46import javax.annotation.processing.RoundEnvironment;
47import javax.annotation.processing.SupportedAnnotationTypes;
48import javax.lang.model.SourceVersion;
49import javax.lang.model.element.TypeElement;
50import javax.tools.Diagnostic.Kind;
51import javax.tools.JavaCompiler;
52import javax.tools.JavaCompiler.CompilationTask;
53import javax.tools.StandardJavaFileManager;
54import javax.tools.StandardLocation;
55import javax.tools.ToolProvider;
56
57public class T7068451 {
58    public static void main(String[] args) throws Exception {
59        new T7068451().run();
60    }
61
62    void run() throws Exception {
63        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
64        System.err.println("using " + compiler.getClass() + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
65
66        File tmp = new File("tmp");
67        tmp.mkdir();
68        for (File f: tmp.listFiles())
69            f.delete();
70
71        File input = writeFile(tmp, "X.java", "package p; class X { { p.C.first(); } }");
72
73        List<String> opts = Arrays.asList(
74                "-s", tmp.getPath(),
75                "-d", tmp.getPath(),
76                "-XprintRounds");
77
78        System.err.println();
79        System.err.println("FIRST compilation");
80        System.err.println();
81
82        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
83            CompilationTask task = compiler.getTask(null, fm, null, opts, null,
84                    fm.getJavaFileObjects(input));
85            task.setProcessors(Collections.singleton(new Proc("first")));
86            check("compilation", task.call());
87
88            writeFile(tmp, "X.java", "package p; class X { { p.C.second(); } }");
89
90            //Thread.sleep(2000);
91
92            System.err.println();
93            System.err.println("SECOND compilation");
94            System.err.println();
95
96            task = compiler.getTask(null, fm, null, opts, null,
97                    fm.getJavaFileObjects(input));
98            task.setProcessors(Collections.singleton(new Proc("second")));
99            check("compilation", task.call());
100
101            //Thread.sleep(2000);
102
103            System.err.println();
104            System.err.println("SECOND compilation, REPEATED");
105            System.err.println();
106
107            task = compiler.getTask(null, fm, null, opts, null,
108                    fm.getJavaFileObjects(input));
109            task.setProcessors(Collections.singleton(new Proc("second")));
110            check("compilation", task.call());
111        }
112    }
113
114    void check(String msg, boolean ok) {
115        System.err.println(msg + ": " + (ok ? "ok" : "failed"));
116        if (!ok)
117            throw new AssertionError(msg);
118    }
119
120    static File writeFile(File base, String path, String body) throws IOException {
121        File f = new File(base, path);
122        FileWriter out = new FileWriter(f);
123        out.write(body);
124        out.close();
125        System.err.println("wrote " + path + ": " + body);
126        return f;
127    }
128
129    @SupportedAnnotationTypes("*")
130    private static class Proc extends AbstractProcessor {
131        final String m;
132        Proc(String m) {
133            this.m = m;
134        }
135
136        int count;
137        @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
138            if (roundEnv.processingOver() || count++ > 0) {
139                return false;
140            }
141
142            Filer filer = processingEnv.getFiler();
143            Messager messager = processingEnv.getMessager();
144
145            System.err.println("running Proc");
146            try {
147                int len = filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length();
148                messager.printMessage(Kind.NOTE, "C.java: found previous content of length " + len);
149            } catch (FileNotFoundException | NoSuchFileException x) {
150                messager.printMessage(Kind.NOTE, "C.java: not previously there");
151            } catch (IOException x) {
152                messager.printMessage(Kind.ERROR, "while reading: " + x);
153            }
154
155            try {
156                String body = "package p; public class C { public static void " + m + "() {} }";
157                Writer w = filer.createSourceFile("p.C").openWriter();
158                w.write(body);
159                w.close();
160                messager.printMessage(Kind.NOTE, "C.java: wrote new content: " + body);
161            } catch (IOException x) {
162                messager.printMessage(Kind.ERROR, "while writing: " + x);
163            }
164
165            return true;
166        }
167
168        @Override
169        public SourceVersion getSupportedSourceVersion() {
170            return SourceVersion.latest();
171        }
172    }
173}
174
175