jjg@1521: /* vromero@1690: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1521: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1521: * jjg@1521: * This code is free software; you can redistribute it and/or modify it jjg@1521: * under the terms of the GNU General Public License version 2 only, as jjg@1521: * published by the Free Software Foundation. jjg@1521: * jjg@1521: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1521: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1521: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1521: * version 2 for more details (a copy is included in the LICENSE file that jjg@1521: * accompanied this code). jjg@1521: * jjg@1521: * You should have received a copy of the GNU General Public License version jjg@1521: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1521: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1521: * jjg@1521: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1521: * or visit www.oracle.com if you need additional information or have any jjg@1521: * questions. jjg@1521: */ jjg@1521: import java.io.*; jjg@1521: import java.util.Set; jjg@1521: import java.util.HashSet; jjg@1521: jjg@1521: import javax.annotation.processing.*; jjg@1521: import javax.lang.model.SourceVersion; jjg@1521: import javax.lang.model.element.*; jjg@1521: import javax.lang.model.util.ElementFilter; jjg@1521: jjg@1521: import com.sun.source.util.JavacTask; jjg@1521: import com.sun.source.util.TaskEvent; jjg@1521: import com.sun.source.util.TaskListener; jjg@1521: import com.sun.tools.javac.main.JavaCompiler; jjg@1521: import com.sun.tools.javac.processing.JavacProcessingEnvironment; jjg@1521: import com.sun.tools.javac.util.Context; jjg@1521: vromero@1690: import static com.sun.tools.javac.comp.CompileStates.CompileState; vromero@1690: jjg@1521: /* jjg@1521: * @test jjg@1521: * @summary test that type processors are run when -proc:only is passed. jjg@1521: * This class implements the functionality of a type processor, as previously jjg@1521: * embodied by the AbstractTypeProcessor class. jjg@1521: * jjg@1521: * @author Mahmood Ali jjg@1521: * @author Werner Dietl jjg@1521: */ jjg@1521: @SupportedAnnotationTypes("*") jjg@1521: public class TypeProcOnly extends AbstractProcessor { jjg@1521: private static final String INDICATOR = "INDICATOR"; jjg@1521: jjg@1521: private final AttributionTaskListener listener = new AttributionTaskListener(); jjg@1521: private final Set elements = new HashSet(); jjg@1521: jjg@1521: @Override jjg@1521: public final void init(ProcessingEnvironment env) { jjg@1521: super.init(env); jjg@1521: JavacTask.instance(env).addTaskListener(listener); jjg@1521: Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext(); jjg@1521: JavaCompiler compiler = JavaCompiler.instance(ctx); jjg@1521: compiler.shouldStopPolicyIfNoError = CompileState.max( jjg@1521: compiler.shouldStopPolicyIfNoError, jjg@1521: CompileState.FLOW); jjg@1521: } jjg@1521: jjg@1521: @Override jjg@1521: public final boolean process(Set annotations, jjg@1521: RoundEnvironment roundEnv) { jjg@1521: for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { jjg@1521: elements.add(elem.getQualifiedName()); jjg@1521: } jjg@1521: return false; jjg@1521: } jjg@1521: jjg@1521: @Override jjg@1521: public SourceVersion getSupportedSourceVersion() { jjg@1521: return SourceVersion.latest(); jjg@1521: } jjg@1521: jjg@1521: private final class AttributionTaskListener implements TaskListener { jjg@1521: @Override jjg@1521: public void started(TaskEvent e) { } jjg@1521: jjg@1521: @Override jjg@1521: public void finished(TaskEvent e) { jjg@1521: if (e.getKind() != TaskEvent.Kind.ANALYZE) jjg@1521: return; jjg@1521: jjg@1521: if (!elements.remove(e.getTypeElement().getQualifiedName())) jjg@1521: return; jjg@1521: jjg@1521: System.out.println(INDICATOR); jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: jjg@1521: private static File writeTestFile() throws IOException { jjg@1521: File f = new File("Test.java"); jjg@1521: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@1521: out.println("class Test { }"); jjg@1521: out.close(); jjg@1521: return f; jjg@1521: } jjg@1521: jjg@1521: public static void main(String[] args) throws Exception { jjg@1521: PrintStream prevOut = System.out; jjg@1521: jjg@1521: ByteArrayOutputStream bytes = new ByteArrayOutputStream(); jjg@1521: PrintStream out = new PrintStream(bytes); jjg@1521: System.setOut(out); jjg@1521: jjg@1521: try { jjg@1521: File f = writeTestFile(); jjg@1521: com.sun.tools.javac.Main.compile(new String[] {"-proc:only", "-processor", "TypeProcOnly", f.getAbsolutePath()}); jjg@1521: } finally { jjg@1521: System.setOut(prevOut); jjg@1521: } jjg@1521: jjg@1521: if (bytes.toString().trim().equals(INDICATOR)) { jjg@1521: System.out.println("PASSED"); jjg@1521: } else { jjg@1521: throw new Exception("Processor did not run correctly. Output: " + bytes); jjg@1521: } jjg@1521: } jjg@1521: }