1062N/A/*
1062N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1062N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1062N/A *
1062N/A * This code is free software; you can redistribute it and/or modify it
1062N/A * under the terms of the GNU General Public License version 2 only, as
1062N/A * published by the Free Software Foundation.
1062N/A *
1062N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1062N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1062N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1062N/A * version 2 for more details (a copy is included in the LICENSE file that
1062N/A * accompanied this code).
1062N/A *
1062N/A * You should have received a copy of the GNU General Public License version
1062N/A * 2 along with this work; if not, write to the Free Software Foundation,
1062N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1062N/A *
1062N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1062N/A * or visit www.oracle.com if you need additional information or have any
1062N/A * questions.
1062N/A */
1062N/A
1062N/A/*
1062N/A * @test
1062N/A * @bug 7068437
1062N/A * @summary Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
1062N/A */
1062N/A
1062N/Aimport java.io.FileNotFoundException;
1062N/Aimport java.io.IOException;
1062N/Aimport java.io.Writer;
1062N/Aimport java.util.Arrays;
1062N/Aimport java.util.Collections;
1062N/Aimport java.util.Map;
1062N/Aimport java.util.Set;
1062N/Aimport javax.annotation.processing.AbstractProcessor;
1062N/Aimport javax.annotation.processing.Filer;
1062N/Aimport javax.annotation.processing.Messager;
1062N/Aimport javax.annotation.processing.RoundEnvironment;
1062N/Aimport javax.annotation.processing.SupportedAnnotationTypes;
1062N/Aimport javax.annotation.processing.SupportedOptions;
1062N/Aimport javax.annotation.processing.SupportedSourceVersion;
1062N/Aimport javax.lang.model.SourceVersion;
1062N/Aimport javax.lang.model.element.TypeElement;
1062N/Aimport javax.tools.Diagnostic.Kind;
1062N/Aimport javax.tools.JavaCompiler;
1062N/Aimport javax.tools.JavaCompiler.CompilationTask;
1062N/Aimport javax.tools.StandardLocation;
1062N/Aimport javax.tools.ToolProvider;
1062N/A
1062N/Apublic class T7068437 {
1062N/A public static void main(String[] args) throws Exception {
1062N/A new T7068437().run();
1062N/A }
1062N/A
1062N/A void run() throws Exception {
1062N/A JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
1062N/A System.err.println("using " + compiler.getClass()
1062N/A + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
1062N/A
1062N/A CompilationTask task = compiler.getTask(null, null, null,
1062N/A Collections.singleton("-proc:only"),
1062N/A Collections.singleton("java.lang.Object"),
1062N/A null);
1062N/A task.setProcessors(Collections.singleton(new Proc()));
1062N/A check("compilation", task.call());
1062N/A
1062N/A task = compiler.getTask(null, null, null,
1062N/A Arrays.asList("-proc:only", "-AexpectFile"),
1062N/A Collections.singleton("java.lang.Object"),
1062N/A null);
1062N/A task.setProcessors(Collections.singleton(new Proc()));
1062N/A check("compilation", task.call());
1062N/A }
1062N/A
1062N/A void check(String msg, boolean ok) {
1062N/A System.err.println(msg + ": " + (ok ? "ok" : "failed"));
1062N/A if (!ok)
1062N/A throw new AssertionError(msg);
1062N/A }
1062N/A
1062N/A @SupportedAnnotationTypes("*")
1062N/A @SupportedOptions("expectFile")
1062N/A private static class Proc extends AbstractProcessor {
1062N/A int count;
1062N/A
1062N/A @Override
1062N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1062N/A if (roundEnv.processingOver() || count++ > 0) {
1062N/A return false;
1062N/A }
1062N/A
1062N/A Filer filer = processingEnv.getFiler();
1062N/A Messager messager = processingEnv.getMessager();
1062N/A Map<String, String> options = processingEnv.getOptions();
1062N/A System.err.println(options);
1062N/A boolean expectFile = options.containsKey("expectFile");
1062N/A
1062N/A System.err.println("running Proc: expectFile=" + expectFile);
1062N/A
1062N/A boolean found;
1062N/A try {
1062N/A messager.printMessage(Kind.NOTE, "found previous content of length " +
1062N/A filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length());
1062N/A found = true;
1062N/A } catch (FileNotFoundException x) {
1062N/A messager.printMessage(Kind.NOTE, "not previously there");
1062N/A found = false;
1062N/A } catch (IOException x) {
1062N/A messager.printMessage(Kind.ERROR, "while reading: " + x);
1062N/A found = false;
1062N/A }
1062N/A
1062N/A if (expectFile && !found) {
1062N/A messager.printMessage(Kind.ERROR, "expected file but file not found");
1062N/A }
1062N/A
1062N/A try {
1062N/A Writer w = filer.createSourceFile("p.C").openWriter();
1062N/A w.write("/* hello! */ package p; class C {}");
1062N/A w.close();
1062N/A messager.printMessage(Kind.NOTE, "wrote new content");
1062N/A } catch (IOException x) {
1062N/A messager.printMessage(Kind.ERROR, "while writing: " + x);
1062N/A }
1062N/A
1062N/A return true;
1062N/A }
1062N/A
1062N/A @Override
1062N/A public SourceVersion getSupportedSourceVersion() {
1062N/A return SourceVersion.latest();
1062N/A }
1062N/A }
1062N/A}