OverwriteBetweenCompilations.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2014, 2015, 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 8038455 27 * @summary Verify that annotation processor can overwrite source and class files it generated 28 * during previous compilations, and that the Symbols are updated appropriatelly. 29 * @library /tools/lib /tools/javac/lib/ 30 * @modules jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.file 32 * jdk.compiler/com.sun.tools.javac.main 33 * jdk.compiler/com.sun.tools.javac.processing 34 * jdk.compiler/com.sun.tools.javac.util 35 * jdk.jdeps/com.sun.tools.javap 36 * @clean * 37 * @build OverwriteBetweenCompilations ToolBox JavacTestingAbstractProcessor 38 * @compile/ref=OverwriteBetweenCompilations_1.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=1 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java 39 * @compile/ref=OverwriteBetweenCompilations_2.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=2 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java 40 * @compile/ref=OverwriteBetweenCompilations_3.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=3 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java 41 */ 42 43import java.io.*; 44import java.util.*; 45 46import javax.annotation.processing.*; 47import javax.lang.model.element.*; 48import javax.tools.*; 49 50import com.sun.tools.javac.processing.JavacProcessingEnvironment; 51import com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor; 52import com.sun.tools.javac.util.Log; 53import com.sun.tools.javac.util.Log.WriterKind; 54 55@SupportedOptions("pass") 56public class OverwriteBetweenCompilations extends JavacTestingAbstractProcessor { 57 int round = 1; 58 @Override 59 public boolean process(Set<? extends TypeElement> annotations, 60 RoundEnvironment roundEnv) { 61 Log log = Log.instance(((JavacProcessingEnvironment) processingEnv).getContext()); 62 PrintWriter pw = log.getWriter(WriterKind.NOTICE); 63 64 pw.println("round: " + round); 65 66 TypeElement generatedSource = 67 processingEnv.getElementUtils().getTypeElement("GeneratedSource"); 68 69 if (generatedSource != null) { 70 new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedSource); 71 pw.flush(); 72 } 73 74 TypeElement generatedClass = 75 processingEnv.getElementUtils().getTypeElement("GeneratedClass"); 76 77 if (generatedClass != null) { 78 new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedClass); 79 pw.flush(); 80 } 81 82 int pass = Integer.parseInt(processingEnv.getOptions().get("pass")); 83 84 if (round++ == 1) { 85 try (Writer out = filer.createSourceFile("GeneratedSource").openWriter()) { 86 String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE; 87 code = code.replace("NAME", "GeneratedSource"); 88 out.write(code); 89 } catch (IOException e) { 90 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); 91 } 92 try (OutputStream out = filer.createClassFile("GeneratedClass").openOutputStream()) { 93 String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE; 94 code = code.replace("NAME", "GeneratedClass"); 95 96 ToolBox tb = new ToolBox(); 97 ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager(); 98 tb.new JavacTask() 99 .fileManager(mfm) 100 .options("-parameters") 101 .sources(code) 102 .run(); 103 104 out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "GeneratedClass")); 105 } catch (IOException e) { 106 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); 107 } 108 } 109 110 return false; 111 } 112 113 //the initial generated class - "NAME" will be replaced with either "GeneratedSource" or 114 //"GeneratedClass" while generating the class: 115 private static final String GENERATED_INIT = 116 "@Deprecated\n" + 117 "public class NAME<T extends CharSequence> extends java.util.ArrayList<String>\n" + 118 " implements Runnable {\n" + 119 " public void test(int a) { }\n" + 120 " public void run() { }\n" + 121 "}"; 122 123 //generated class update- "NAME" will be replaced with either "GeneratedSource" or 124 //"GeneratedClass" while generating the class: 125 private static final String GENERATED_UPDATE = 126 "@javax.annotation.processing.SupportedAnnotationTypes(\"*\")\n" + 127 "public abstract class NAME<E extends Number> extends java.util.LinkedList<Number>" + 128 " implements Runnable, CharSequence {\n" + 129 " public void test(long a) { }\n" + 130 "}"; 131} 132