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 7068437
27 * @summary Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
28 * @modules java.compiler
29 *          jdk.compiler
30 */
31
32import java.io.FileNotFoundException;
33import java.io.IOException;
34import java.io.Writer;
35import java.nio.file.NoSuchFileException;
36import java.util.Arrays;
37import java.util.Collections;
38import java.util.Map;
39import java.util.Set;
40import javax.annotation.processing.AbstractProcessor;
41import javax.annotation.processing.Filer;
42import javax.annotation.processing.Messager;
43import javax.annotation.processing.RoundEnvironment;
44import javax.annotation.processing.SupportedAnnotationTypes;
45import javax.annotation.processing.SupportedOptions;
46import javax.annotation.processing.SupportedSourceVersion;
47import javax.lang.model.SourceVersion;
48import javax.lang.model.element.TypeElement;
49import javax.tools.Diagnostic.Kind;
50import javax.tools.JavaCompiler;
51import javax.tools.JavaCompiler.CompilationTask;
52import javax.tools.StandardLocation;
53import javax.tools.ToolProvider;
54
55public class T7068437 {
56    public static void main(String[] args) throws Exception {
57        new T7068437().run();
58    }
59
60    void run() throws Exception {
61        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
62        System.err.println("using " + compiler.getClass()
63                + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
64
65        CompilationTask task = compiler.getTask(null, null, null,
66                Collections.singleton("-proc:only"),
67                Collections.singleton("java.lang.Object"),
68                null);
69        task.setProcessors(Collections.singleton(new Proc()));
70        check("compilation", task.call());
71
72        task = compiler.getTask(null, null, null,
73                Arrays.asList("-proc:only", "-AexpectFile"),
74                Collections.singleton("java.lang.Object"),
75                null);
76        task.setProcessors(Collections.singleton(new Proc()));
77        check("compilation", task.call());
78    }
79
80    void check(String msg, boolean ok) {
81        System.err.println(msg + ": " + (ok ? "ok" : "failed"));
82        if (!ok)
83            throw new AssertionError(msg);
84    }
85
86    @SupportedAnnotationTypes("*")
87    @SupportedOptions("expectFile")
88    private static class Proc extends AbstractProcessor {
89        int count;
90
91        @Override
92        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
93            if (roundEnv.processingOver() || count++ > 0) {
94                return false;
95            }
96
97            Filer filer = processingEnv.getFiler();
98            Messager messager = processingEnv.getMessager();
99            Map<String, String> options = processingEnv.getOptions();
100                System.err.println(options);
101            boolean expectFile = options.containsKey("expectFile");
102
103            System.err.println("running Proc: expectFile=" + expectFile);
104
105            boolean found;
106            try {
107                messager.printMessage(Kind.NOTE, "found previous content of length " +
108                        filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length());
109                found = true;
110            } catch (FileNotFoundException | NoSuchFileException x) {
111                messager.printMessage(Kind.NOTE, "not previously there");
112                found = false;
113            } catch (IOException x) {
114                messager.printMessage(Kind.ERROR, "while reading: " + x);
115                found = false;
116            }
117
118            if (expectFile && !found) {
119                messager.printMessage(Kind.ERROR, "expected file but file not found");
120            }
121
122            try {
123                Writer w = filer.createSourceFile("p.C").openWriter();
124                w.write("/* hello! */ package p; class C {}");
125                w.close();
126                messager.printMessage(Kind.NOTE, "wrote new content");
127            } catch (IOException x) {
128                messager.printMessage(Kind.ERROR, "while writing: " + x);
129            }
130
131            return true;
132        }
133
134        @Override
135        public SourceVersion getSupportedSourceVersion() {
136            return SourceVersion.latest();
137        }
138    }
139}
140