test/tools/javac/file/T7068437.java

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

mercurial