1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/file/T7068437.java Wed Sep 14 18:26:57 2011 -0700 1.3 @@ -0,0 +1,136 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7068437 1.30 + * @summary Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s 1.31 + */ 1.32 + 1.33 +import java.io.FileNotFoundException; 1.34 +import java.io.IOException; 1.35 +import java.io.Writer; 1.36 +import java.util.Arrays; 1.37 +import java.util.Collections; 1.38 +import java.util.Map; 1.39 +import java.util.Set; 1.40 +import javax.annotation.processing.AbstractProcessor; 1.41 +import javax.annotation.processing.Filer; 1.42 +import javax.annotation.processing.Messager; 1.43 +import javax.annotation.processing.RoundEnvironment; 1.44 +import javax.annotation.processing.SupportedAnnotationTypes; 1.45 +import javax.annotation.processing.SupportedOptions; 1.46 +import javax.annotation.processing.SupportedSourceVersion; 1.47 +import javax.lang.model.SourceVersion; 1.48 +import javax.lang.model.element.TypeElement; 1.49 +import javax.tools.Diagnostic.Kind; 1.50 +import javax.tools.JavaCompiler; 1.51 +import javax.tools.JavaCompiler.CompilationTask; 1.52 +import javax.tools.StandardLocation; 1.53 +import javax.tools.ToolProvider; 1.54 + 1.55 +public class T7068437 { 1.56 + public static void main(String[] args) throws Exception { 1.57 + new T7068437().run(); 1.58 + } 1.59 + 1.60 + void run() throws Exception { 1.61 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.62 + System.err.println("using " + compiler.getClass() 1.63 + + " from " + compiler.getClass().getProtectionDomain().getCodeSource()); 1.64 + 1.65 + CompilationTask task = compiler.getTask(null, null, null, 1.66 + Collections.singleton("-proc:only"), 1.67 + Collections.singleton("java.lang.Object"), 1.68 + null); 1.69 + task.setProcessors(Collections.singleton(new Proc())); 1.70 + check("compilation", task.call()); 1.71 + 1.72 + task = compiler.getTask(null, null, null, 1.73 + Arrays.asList("-proc:only", "-AexpectFile"), 1.74 + Collections.singleton("java.lang.Object"), 1.75 + null); 1.76 + task.setProcessors(Collections.singleton(new Proc())); 1.77 + check("compilation", task.call()); 1.78 + } 1.79 + 1.80 + void check(String msg, boolean ok) { 1.81 + System.err.println(msg + ": " + (ok ? "ok" : "failed")); 1.82 + if (!ok) 1.83 + throw new AssertionError(msg); 1.84 + } 1.85 + 1.86 + @SupportedAnnotationTypes("*") 1.87 + @SupportedOptions("expectFile") 1.88 + private static class Proc extends AbstractProcessor { 1.89 + int count; 1.90 + 1.91 + @Override 1.92 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 1.93 + if (roundEnv.processingOver() || count++ > 0) { 1.94 + return false; 1.95 + } 1.96 + 1.97 + Filer filer = processingEnv.getFiler(); 1.98 + Messager messager = processingEnv.getMessager(); 1.99 + Map<String, String> options = processingEnv.getOptions(); 1.100 + System.err.println(options); 1.101 + boolean expectFile = options.containsKey("expectFile"); 1.102 + 1.103 + System.err.println("running Proc: expectFile=" + expectFile); 1.104 + 1.105 + boolean found; 1.106 + try { 1.107 + messager.printMessage(Kind.NOTE, "found previous content of length " + 1.108 + filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length()); 1.109 + found = true; 1.110 + } catch (FileNotFoundException x) { 1.111 + messager.printMessage(Kind.NOTE, "not previously there"); 1.112 + found = false; 1.113 + } catch (IOException x) { 1.114 + messager.printMessage(Kind.ERROR, "while reading: " + x); 1.115 + found = false; 1.116 + } 1.117 + 1.118 + if (expectFile && !found) { 1.119 + messager.printMessage(Kind.ERROR, "expected file but file not found"); 1.120 + } 1.121 + 1.122 + try { 1.123 + Writer w = filer.createSourceFile("p.C").openWriter(); 1.124 + w.write("/* hello! */ package p; class C {}"); 1.125 + w.close(); 1.126 + messager.printMessage(Kind.NOTE, "wrote new content"); 1.127 + } catch (IOException x) { 1.128 + messager.printMessage(Kind.ERROR, "while writing: " + x); 1.129 + } 1.130 + 1.131 + return true; 1.132 + } 1.133 + 1.134 + @Override 1.135 + public SourceVersion getSupportedSourceVersion() { 1.136 + return SourceVersion.latest(); 1.137 + } 1.138 + } 1.139 +}