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