1 /*
2  * Copyright (c) 2009, 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 import java.io.*;
25 import java.util.HashSet;
26 import java.util.Set;
27 
28 import javax.annotation.processing.*;
29 import javax.lang.model.SourceVersion;
30 import javax.lang.model.element.*;
31 import javax.lang.model.util.ElementFilter;
32 
33 import com.sun.source.util.JavacTask;
34 import com.sun.source.util.TaskEvent;
35 import com.sun.source.util.TaskListener;
36 import com.sun.tools.javac.main.JavaCompiler;
37 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
38 import com.sun.tools.javac.util.Context;
39 
40 import static com.sun.tools.javac.comp.CompileStates.CompileState;
41 
42 /*
43  * @test
44  * @summary test that type processors are run when -proc:only is passed.
45  * This class implements the functionality of a type processor, as previously
46  * embodied by the AbstractTypeProcessor class.
47  *
48  * @author Mahmood Ali
49  * @author Werner Dietl
50  * @modules jdk.compiler/com.sun.tools.javac.comp
51  *          jdk.compiler/com.sun.tools.javac.main
52  *          jdk.compiler/com.sun.tools.javac.processing
53  *          jdk.compiler/com.sun.tools.javac.util
54  */
55 @SupportedAnnotationTypes("*")
56 public class TypeProcOnly extends AbstractProcessor {
57     private static final String INDICATOR = "INDICATOR";
58 
59     private final AttributionTaskListener listener = new AttributionTaskListener();
60     private final Set<Name> elements = new HashSet<Name>();
61 
62     @Override
init(ProcessingEnvironment env)63     public final void init(ProcessingEnvironment env) {
64         super.init(env);
65         JavacTask.instance(env).addTaskListener(listener);
66         Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
67         JavaCompiler compiler = JavaCompiler.instance(ctx);
68         compiler.shouldStopPolicyIfNoError = CompileState.max(
69                 compiler.shouldStopPolicyIfNoError,
70                 CompileState.FLOW);
71     }
72 
73     @Override
process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)74     public final boolean process(Set<? extends TypeElement> annotations,
75             RoundEnvironment roundEnv) {
76         for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) {
77             elements.add(elem.getQualifiedName());
78         }
79         return false;
80     }
81 
82     @Override
getSupportedSourceVersion()83     public SourceVersion getSupportedSourceVersion() {
84         return SourceVersion.latest();
85     }
86 
87     private final class AttributionTaskListener implements TaskListener {
88         @Override
started(TaskEvent e)89         public void started(TaskEvent e) { }
90 
91         @Override
finished(TaskEvent e)92         public void finished(TaskEvent e) {
93             if (e.getKind() != TaskEvent.Kind.ANALYZE)
94                 return;
95 
96             if (!elements.remove(e.getTypeElement().getQualifiedName()))
97                 return;
98 
99             System.out.println(INDICATOR);
100         }
101     }
102 
103 
writeTestFile()104     private static File writeTestFile() throws IOException {
105         File f = new File("Test.java");
106         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
107         out.println("class Test { }");
108         out.close();
109         return f;
110     }
111 
main(String[] args)112     public static void main(String[] args) throws Exception {
113         PrintStream prevOut = System.out;
114 
115         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
116         PrintStream out = new PrintStream(bytes);
117         System.setOut(out);
118 
119         try {
120             File f = writeTestFile();
121             com.sun.tools.javac.Main.compile(new String[] {"-XDaccessInternalAPI", "-proc:only", "-processor", "TypeProcOnly", f.getAbsolutePath()});
122         } finally {
123             System.setOut(prevOut);
124         }
125 
126         if (bytes.toString().trim().equals(INDICATOR)) {
127             System.out.println("PASSED");
128         } else {
129             throw new Exception("Processor did not run correctly. Output: " + bytes);
130         }
131     }
132 }
133