T7068451.java revision 2734:b96d74fa60aa
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 7068451 27 * @summary Regression: javac compiles fixed sources against previous, 28 * not current, version of generated sources 29 */ 30 31import java.io.File; 32import java.io.FileNotFoundException; 33import java.io.FileWriter; 34import java.io.IOException; 35import java.io.Writer; 36import java.nio.file.NoSuchFileException; 37import java.util.Arrays; 38import java.util.Collections; 39import java.util.List; 40import java.util.Set; 41import javax.annotation.processing.AbstractProcessor; 42import javax.annotation.processing.Filer; 43import javax.annotation.processing.Messager; 44import javax.annotation.processing.RoundEnvironment; 45import javax.annotation.processing.SupportedAnnotationTypes; 46import javax.lang.model.SourceVersion; 47import javax.lang.model.element.TypeElement; 48import javax.tools.Diagnostic.Kind; 49import javax.tools.JavaCompiler; 50import javax.tools.JavaCompiler.CompilationTask; 51import javax.tools.StandardJavaFileManager; 52import javax.tools.StandardLocation; 53import javax.tools.ToolProvider; 54 55public class T7068451 { 56 public static void main(String[] args) throws Exception { 57 new T7068451().run(); 58 } 59 60 void run() throws Exception { 61 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 62 System.err.println("using " + compiler.getClass() + " from " + compiler.getClass().getProtectionDomain().getCodeSource()); 63 64 File tmp = new File("tmp"); 65 tmp.mkdir(); 66 for (File f: tmp.listFiles()) 67 f.delete(); 68 69 File input = writeFile(tmp, "X.java", "package p; class X { { p.C.first(); } }"); 70 71 List<String> opts = Arrays.asList( 72 "-s", tmp.getPath(), 73 "-d", tmp.getPath(), 74 "-XprintRounds"); 75 76 System.err.println(); 77 System.err.println("FIRST compilation"); 78 System.err.println(); 79 80 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 81 CompilationTask task = compiler.getTask(null, fm, null, opts, null, 82 fm.getJavaFileObjects(input)); 83 task.setProcessors(Collections.singleton(new Proc("first"))); 84 check("compilation", task.call()); 85 86 writeFile(tmp, "X.java", "package p; class X { { p.C.second(); } }"); 87 88 //Thread.sleep(2000); 89 90 System.err.println(); 91 System.err.println("SECOND compilation"); 92 System.err.println(); 93 94 task = compiler.getTask(null, fm, null, opts, null, 95 fm.getJavaFileObjects(input)); 96 task.setProcessors(Collections.singleton(new Proc("second"))); 97 check("compilation", task.call()); 98 99 //Thread.sleep(2000); 100 101 System.err.println(); 102 System.err.println("SECOND compilation, REPEATED"); 103 System.err.println(); 104 105 task = compiler.getTask(null, fm, null, opts, null, 106 fm.getJavaFileObjects(input)); 107 task.setProcessors(Collections.singleton(new Proc("second"))); 108 check("compilation", task.call()); 109 } 110 } 111 112 void check(String msg, boolean ok) { 113 System.err.println(msg + ": " + (ok ? "ok" : "failed")); 114 if (!ok) 115 throw new AssertionError(msg); 116 } 117 118 static File writeFile(File base, String path, String body) throws IOException { 119 File f = new File(base, path); 120 FileWriter out = new FileWriter(f); 121 out.write(body); 122 out.close(); 123 System.err.println("wrote " + path + ": " + body); 124 return f; 125 } 126 127 @SupportedAnnotationTypes("*") 128 private static class Proc extends AbstractProcessor { 129 final String m; 130 Proc(String m) { 131 this.m = m; 132 } 133 134 int count; 135 @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 136 if (roundEnv.processingOver() || count++ > 0) { 137 return false; 138 } 139 140 Filer filer = processingEnv.getFiler(); 141 Messager messager = processingEnv.getMessager(); 142 143 System.err.println("running Proc"); 144 try { 145 int len = filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length(); 146 messager.printMessage(Kind.NOTE, "C.java: found previous content of length " + len); 147 } catch (FileNotFoundException | NoSuchFileException x) { 148 messager.printMessage(Kind.NOTE, "C.java: not previously there"); 149 } catch (IOException x) { 150 messager.printMessage(Kind.ERROR, "while reading: " + x); 151 } 152 153 try { 154 String body = "package p; public class C { public static void " + m + "() {} }"; 155 Writer w = filer.createSourceFile("p.C").openWriter(); 156 w.write(body); 157 w.close(); 158 messager.printMessage(Kind.NOTE, "C.java: wrote new content: " + body); 159 } catch (IOException x) { 160 messager.printMessage(Kind.ERROR, "while writing: " + x); 161 } 162 163 return true; 164 } 165 166 @Override 167 public SourceVersion getSupportedSourceVersion() { 168 return SourceVersion.latest(); 169 } 170 } 171} 172 173