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