1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8038455
* @summary Verify that annotation processor can overwrite source and class files it generated
* during previous compilations, and that the Symbols are updated appropriately.
* @library /tools/lib /tools/javac/lib/
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.processing
* jdk.compiler/com.sun.tools.javac.util
* jdk.jdeps/com.sun.tools.javap
* @clean *
* @build toolbox.ToolBox toolbox.JavacTask
* @build OverwriteBetweenCompilations JavacTestingAbstractProcessor
* @compile/ref=OverwriteBetweenCompilations_1.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=1 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
* @compile/ref=OverwriteBetweenCompilations_2.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=2 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
* @compile/ref=OverwriteBetweenCompilations_3.out -XDaccessInternalAPI -processor OverwriteBetweenCompilations -Apass=3 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
*/
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.*;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Log.WriterKind;
import toolbox.JavacTask;
import toolbox.ToolBox;
@SupportedOptions("pass")
public class OverwriteBetweenCompilations extends JavacTestingAbstractProcessor {
int round = 1;
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Log log = Log.instance(((JavacProcessingEnvironment) processingEnv).getContext());
PrintWriter pw = log.getWriter(WriterKind.NOTICE);
pw.println("round: " + round);
TypeElement generatedSource =
processingEnv.getElementUtils().getTypeElement("GeneratedSource");
if (generatedSource != null) {
new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedSource);
pw.flush();
}
TypeElement generatedClass =
processingEnv.getElementUtils().getTypeElement("GeneratedClass");
if (generatedClass != null) {
new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedClass);
pw.flush();
}
int pass = Integer.parseInt(processingEnv.getOptions().get("pass"));
if (round++ == 1) {
try (Writer out = filer.createSourceFile("GeneratedSource").openWriter()) {
String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE;
code = code.replace("NAME", "GeneratedSource");
out.write(code);
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString());
}
try (OutputStream out = filer.createClassFile("GeneratedClass").openOutputStream()) {
String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE;
code = code.replace("NAME", "GeneratedClass");
ToolBox tb = new ToolBox();
ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager();
new JavacTask(tb)
.fileManager(mfm)
.options("-parameters")
.sources(code)
.run();
out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "GeneratedClass"));
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString());
}
}
return false;
}
//the initial generated class - "NAME" will be replaced with either "GeneratedSource" or
//"GeneratedClass" while generating the class:
private static final String GENERATED_INIT =
"@Deprecated\n" +
"public class NAME<T extends CharSequence> extends java.util.ArrayList<String>\n" +
" implements Runnable {\n" +
" public void test(int a) { }\n" +
" public void run() { }\n" +
"}";
//generated class update- "NAME" will be replaced with either "GeneratedSource" or
//"GeneratedClass" while generating the class:
private static final String GENERATED_UPDATE =
"@javax.annotation.processing.SupportedAnnotationTypes(\"*\")\n" +
"public abstract class NAME<E extends Number> extends java.util.LinkedList<Number>" +
" implements Runnable, CharSequence {\n" +
" public void test(long a) { }\n" +
"}";
}
|