T7068451.java revision 2675:4be0e35f385a
1254721Semaste/*
2254721Semaste * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353358Sdim *
5353358Sdim * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10344779Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13314564Sdim * accompanied this code).
14344779Sdim *
15341825Sdim * You should have received a copy of the GNU General Public License version
16341825Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17353358Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254721Semaste *
19344779Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20360784Sdim * or visit www.oracle.com if you need additional information or have any
21254721Semaste * questions.
22344779Sdim */
23344779Sdim
24321369Sdim/*
25344779Sdim * @test
26344779Sdim * @bug 7068451
27344779Sdim * @summary Regression: javac compiles fixed sources against previous,
28254721Semaste *              not current, version of generated sources
29341825Sdim */
30344779Sdim
31321369Sdimimport java.io.File;
32321369Sdimimport java.io.FileNotFoundException;
33344779Sdimimport java.io.FileWriter;
34344779Sdimimport java.io.IOException;
35321369Sdimimport java.io.Writer;
36321369Sdimimport java.util.Arrays;
37344779Sdimimport java.util.Collections;
38321369Sdimimport java.util.List;
39344779Sdimimport java.util.Set;
40344779Sdimimport javax.annotation.processing.AbstractProcessor;
41321369Sdimimport javax.annotation.processing.Filer;
42344779Sdimimport javax.annotation.processing.Messager;
43344779Sdimimport javax.annotation.processing.RoundEnvironment;
44321369Sdimimport javax.annotation.processing.SupportedAnnotationTypes;
45321369Sdimimport javax.lang.model.SourceVersion;
46321369Sdimimport javax.lang.model.element.TypeElement;
47321369Sdimimport javax.tools.Diagnostic.Kind;
48321369Sdimimport javax.tools.JavaCompiler;
49321369Sdimimport javax.tools.JavaCompiler.CompilationTask;
50321369Sdimimport javax.tools.StandardLocation;
51321369Sdimimport javax.tools.ToolProvider;
52321369Sdim
53321369Sdimpublic class T7068451 {
54321369Sdim    public static void main(String[] args) throws Exception {
55321369Sdim        new T7068451().run();
56321369Sdim    }
57321369Sdim
58321369Sdim    void run() throws Exception {
59321369Sdim        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
60321369Sdim        System.err.println("using " + compiler.getClass() + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
61254721Semaste
62254721Semaste        File tmp = new File("tmp");
63254721Semaste        tmp.mkdir();
64341825Sdim        for (File f: tmp.listFiles())
65341825Sdim            f.delete();
66360784Sdim
67360784Sdim        File input = writeFile(tmp, "X.java", "package p; class X { { p.C.first(); } }");
68341825Sdim
69360784Sdim        List<String> opts = Arrays.asList(
70360784Sdim                "-s", tmp.getPath(),
71360784Sdim                "-d", tmp.getPath(),
72360784Sdim                "-XprintRounds");
73341825Sdim
74341825Sdim        System.err.println();
75341825Sdim        System.err.println("FIRST compilation");
76341825Sdim        System.err.println();
77353358Sdim
78353358Sdim        CompilationTask task = compiler.getTask(null, null, null, opts, null,
79360784Sdim                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
80341825Sdim        task.setProcessors(Collections.singleton(new Proc("first")));
81341825Sdim        check("compilation", task.call());
82341825Sdim
83341825Sdim        writeFile(tmp, "X.java", "package p; class X { { p.C.second(); } }");
84341825Sdim
85341825Sdim        //Thread.sleep(2000);
86341825Sdim
87341825Sdim        System.err.println();
88341825Sdim        System.err.println("SECOND compilation");
89360784Sdim        System.err.println();
90341825Sdim
91341825Sdim        task = compiler.getTask(null, null, null, opts, null,
92353358Sdim                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
93353358Sdim        task.setProcessors(Collections.singleton(new Proc("second")));
94353358Sdim        check("compilation", task.call());
95353358Sdim
96353358Sdim        //Thread.sleep(2000);
97341825Sdim
98341825Sdim        System.err.println();
99341825Sdim        System.err.println("SECOND compilation, REPEATED");
100341825Sdim        System.err.println();
101341825Sdim
102341825Sdim        task = compiler.getTask(null, null, null, opts, null,
103341825Sdim                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
104341825Sdim        task.setProcessors(Collections.singleton(new Proc("second")));
105341825Sdim        check("compilation", task.call());
106341825Sdim    }
107341825Sdim
108341825Sdim    void check(String msg, boolean ok) {
109314564Sdim        System.err.println(msg + ": " + (ok ? "ok" : "failed"));
110314564Sdim        if (!ok)
111254721Semaste            throw new AssertionError(msg);
112314564Sdim    }
113314564Sdim
114314564Sdim    static File writeFile(File base, String path, String body) throws IOException {
115314564Sdim        File f = new File(base, path);
116314564Sdim        FileWriter out = new FileWriter(f);
117254721Semaste        out.write(body);
118254721Semaste        out.close();
119314564Sdim        System.err.println("wrote " + path + ": " + body);
120314564Sdim        return f;
121254721Semaste    }
122314564Sdim
123314564Sdim    @SupportedAnnotationTypes("*")
124353358Sdim    private static class Proc extends AbstractProcessor {
125360784Sdim        final String m;
126353358Sdim        Proc(String m) {
127360784Sdim            this.m = m;
128353358Sdim        }
129353358Sdim
130314564Sdim        int count;
131314564Sdim        @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
132254721Semaste            if (roundEnv.processingOver() || count++ > 0) {
133254721Semaste                return false;
134309124Sdim            }
135254721Semaste
136314564Sdim            Filer filer = processingEnv.getFiler();
137314564Sdim            Messager messager = processingEnv.getMessager();
138314564Sdim
139314564Sdim            System.err.println("running Proc");
140314564Sdim            try {
141353358Sdim                int len = filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length();
142314564Sdim                messager.printMessage(Kind.NOTE, "C.java: found previous content of length " + len);
143254721Semaste            } catch (FileNotFoundException x) {
144254721Semaste                messager.printMessage(Kind.NOTE, "C.java: not previously there");
145360784Sdim            } catch (IOException x) {
146360784Sdim                messager.printMessage(Kind.ERROR, "while reading: " + x);
147353358Sdim            }
148254721Semaste
149314564Sdim            try {
150314564Sdim                String body = "package p; public class C { public static void " + m + "() {} }";
151314564Sdim                Writer w = filer.createSourceFile("p.C").openWriter();
152254721Semaste                w.write(body);
153314564Sdim                w.close();
154314564Sdim                messager.printMessage(Kind.NOTE, "C.java: wrote new content: " + body);
155314564Sdim            } catch (IOException x) {
156314564Sdim                messager.printMessage(Kind.ERROR, "while writing: " + x);
157314564Sdim            }
158314564Sdim
159254721Semaste            return true;
160314564Sdim        }
161314564Sdim
162314564Sdim        @Override
163314564Sdim        public SourceVersion getSupportedSourceVersion() {
164314564Sdim            return SourceVersion.latest();
165314564Sdim        }
166314564Sdim    }
167254721Semaste}
168314564Sdim
169314564Sdim