test/tools/javac/annotations/typeAnnotations/TypeProcOnly.java

Tue, 12 Mar 2013 17:39:34 +0100

author
jfranck
date
Tue, 12 Mar 2013 17:39:34 +0100
changeset 1629
f427043f8c65
parent 1534
bec996065c45
child 1690
76537856a54e
permissions
-rw-r--r--

7196531: Duplicate error messages on repeating annotations
Reviewed-by: jjg

jjg@1521 1 /*
darcy@1534 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation.
jjg@1521 8 *
jjg@1521 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 13 * accompanied this code).
jjg@1521 14 *
jjg@1521 15 * You should have received a copy of the GNU General Public License version
jjg@1521 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 18 *
jjg@1521 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 20 * or visit www.oracle.com if you need additional information or have any
jjg@1521 21 * questions.
jjg@1521 22 */
jjg@1521 23 import java.io.*;
jjg@1521 24 import java.util.Set;
jjg@1521 25 import java.util.HashSet;
jjg@1521 26
jjg@1521 27 import javax.annotation.processing.*;
jjg@1521 28 import javax.lang.model.SourceVersion;
jjg@1521 29 import javax.lang.model.element.*;
jjg@1521 30 import javax.lang.model.util.ElementFilter;
jjg@1521 31
jjg@1521 32 import com.sun.source.util.JavacTask;
jjg@1521 33 import com.sun.source.util.TaskEvent;
jjg@1521 34 import com.sun.source.util.TaskListener;
jjg@1521 35 import com.sun.source.util.TreePath;
jjg@1521 36 import com.sun.tools.javac.main.JavaCompiler;
jjg@1521 37 import com.sun.tools.javac.main.JavaCompiler.CompileState;
jjg@1521 38 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@1521 39 import com.sun.tools.javac.util.Context;
jjg@1521 40
jjg@1521 41 /*
jjg@1521 42 * @test
jjg@1521 43 * @summary test that type processors are run when -proc:only is passed.
jjg@1521 44 * This class implements the functionality of a type processor, as previously
jjg@1521 45 * embodied by the AbstractTypeProcessor class.
jjg@1521 46 *
jjg@1521 47 * @author Mahmood Ali
jjg@1521 48 * @author Werner Dietl
jjg@1521 49 */
jjg@1521 50 @SupportedAnnotationTypes("*")
jjg@1521 51 public class TypeProcOnly extends AbstractProcessor {
jjg@1521 52 private static final String INDICATOR = "INDICATOR";
jjg@1521 53
jjg@1521 54 private final AttributionTaskListener listener = new AttributionTaskListener();
jjg@1521 55 private final Set<Name> elements = new HashSet<Name>();
jjg@1521 56
jjg@1521 57 @Override
jjg@1521 58 public final void init(ProcessingEnvironment env) {
jjg@1521 59 super.init(env);
jjg@1521 60 JavacTask.instance(env).addTaskListener(listener);
jjg@1521 61 Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
jjg@1521 62 JavaCompiler compiler = JavaCompiler.instance(ctx);
jjg@1521 63 compiler.shouldStopPolicyIfNoError = CompileState.max(
jjg@1521 64 compiler.shouldStopPolicyIfNoError,
jjg@1521 65 CompileState.FLOW);
jjg@1521 66 }
jjg@1521 67
jjg@1521 68 @Override
jjg@1521 69 public final boolean process(Set<? extends TypeElement> annotations,
jjg@1521 70 RoundEnvironment roundEnv) {
jjg@1521 71 for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) {
jjg@1521 72 elements.add(elem.getQualifiedName());
jjg@1521 73 }
jjg@1521 74 return false;
jjg@1521 75 }
jjg@1521 76
jjg@1521 77 @Override
jjg@1521 78 public SourceVersion getSupportedSourceVersion() {
jjg@1521 79 return SourceVersion.latest();
jjg@1521 80 }
jjg@1521 81
jjg@1521 82 private final class AttributionTaskListener implements TaskListener {
jjg@1521 83 @Override
jjg@1521 84 public void started(TaskEvent e) { }
jjg@1521 85
jjg@1521 86 @Override
jjg@1521 87 public void finished(TaskEvent e) {
jjg@1521 88 if (e.getKind() != TaskEvent.Kind.ANALYZE)
jjg@1521 89 return;
jjg@1521 90
jjg@1521 91 if (!elements.remove(e.getTypeElement().getQualifiedName()))
jjg@1521 92 return;
jjg@1521 93
jjg@1521 94 System.out.println(INDICATOR);
jjg@1521 95 }
jjg@1521 96 }
jjg@1521 97
jjg@1521 98
jjg@1521 99 private static File writeTestFile() throws IOException {
jjg@1521 100 File f = new File("Test.java");
jjg@1521 101 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@1521 102 out.println("class Test { }");
jjg@1521 103 out.close();
jjg@1521 104 return f;
jjg@1521 105 }
jjg@1521 106
jjg@1521 107 public static void main(String[] args) throws Exception {
jjg@1521 108 PrintStream prevOut = System.out;
jjg@1521 109
jjg@1521 110 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
jjg@1521 111 PrintStream out = new PrintStream(bytes);
jjg@1521 112 System.setOut(out);
jjg@1521 113
jjg@1521 114 try {
jjg@1521 115 File f = writeTestFile();
jjg@1521 116 com.sun.tools.javac.Main.compile(new String[] {"-proc:only", "-processor", "TypeProcOnly", f.getAbsolutePath()});
jjg@1521 117 } finally {
jjg@1521 118 System.setOut(prevOut);
jjg@1521 119 }
jjg@1521 120
jjg@1521 121 if (bytes.toString().trim().equals(INDICATOR)) {
jjg@1521 122 System.out.println("PASSED");
jjg@1521 123 } else {
jjg@1521 124 throw new Exception("Processor did not run correctly. Output: " + bytes);
jjg@1521 125 }
jjg@1521 126 }
jjg@1521 127 }

mercurial