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

changeset 1521
71f35e4b93a5
child 1534
bec996065c45
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/TypeProcOnly.java	Wed Jan 23 13:27:24 2013 -0800
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009 Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +import java.io.*;
    1.27 +import java.util.Set;
    1.28 +import java.util.HashSet;
    1.29 +
    1.30 +import javax.annotation.processing.*;
    1.31 +import javax.lang.model.SourceVersion;
    1.32 +import javax.lang.model.element.*;
    1.33 +import javax.lang.model.util.ElementFilter;
    1.34 +
    1.35 +import com.sun.source.util.JavacTask;
    1.36 +import com.sun.source.util.TaskEvent;
    1.37 +import com.sun.source.util.TaskListener;
    1.38 +import com.sun.source.util.TreePath;
    1.39 +import com.sun.tools.javac.main.JavaCompiler;
    1.40 +import com.sun.tools.javac.main.JavaCompiler.CompileState;
    1.41 +import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    1.42 +import com.sun.tools.javac.util.Context;
    1.43 +
    1.44 +/*
    1.45 + * @test
    1.46 + * @summary test that type processors are run when -proc:only is passed.
    1.47 + * This class implements the functionality of a type processor, as previously
    1.48 + * embodied by the AbstractTypeProcessor class.
    1.49 + *
    1.50 + * @author Mahmood Ali
    1.51 + * @author Werner Dietl
    1.52 + */
    1.53 +@SupportedAnnotationTypes("*")
    1.54 +public class TypeProcOnly extends AbstractProcessor {
    1.55 +    private static final String INDICATOR = "INDICATOR";
    1.56 +
    1.57 +    private final AttributionTaskListener listener = new AttributionTaskListener();
    1.58 +    private final Set<Name> elements = new HashSet<Name>();
    1.59 +
    1.60 +    @Override
    1.61 +    public final void init(ProcessingEnvironment env) {
    1.62 +        super.init(env);
    1.63 +        JavacTask.instance(env).addTaskListener(listener);
    1.64 +        Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
    1.65 +        JavaCompiler compiler = JavaCompiler.instance(ctx);
    1.66 +        compiler.shouldStopPolicyIfNoError = CompileState.max(
    1.67 +                compiler.shouldStopPolicyIfNoError,
    1.68 +                CompileState.FLOW);
    1.69 +    }
    1.70 +
    1.71 +    @Override
    1.72 +    public final boolean process(Set<? extends TypeElement> annotations,
    1.73 +            RoundEnvironment roundEnv) {
    1.74 +        for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) {
    1.75 +            elements.add(elem.getQualifiedName());
    1.76 +        }
    1.77 +        return false;
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public SourceVersion getSupportedSourceVersion() {
    1.82 +        return SourceVersion.latest();
    1.83 +    }
    1.84 +
    1.85 +    private final class AttributionTaskListener implements TaskListener {
    1.86 +        @Override
    1.87 +        public void started(TaskEvent e) { }
    1.88 +
    1.89 +        @Override
    1.90 +        public void finished(TaskEvent e) {
    1.91 +            if (e.getKind() != TaskEvent.Kind.ANALYZE)
    1.92 +                return;
    1.93 +
    1.94 +            if (!elements.remove(e.getTypeElement().getQualifiedName()))
    1.95 +                return;
    1.96 +
    1.97 +            System.out.println(INDICATOR);
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +
   1.102 +    private static File writeTestFile() throws IOException {
   1.103 +        File f = new File("Test.java");
   1.104 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
   1.105 +        out.println("class Test { }");
   1.106 +        out.close();
   1.107 +        return f;
   1.108 +    }
   1.109 +
   1.110 +    public static void main(String[] args) throws Exception {
   1.111 +        PrintStream prevOut = System.out;
   1.112 +
   1.113 +        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
   1.114 +        PrintStream out = new PrintStream(bytes);
   1.115 +        System.setOut(out);
   1.116 +
   1.117 +        try {
   1.118 +            File f = writeTestFile();
   1.119 +            com.sun.tools.javac.Main.compile(new String[] {"-proc:only", "-processor", "TypeProcOnly", f.getAbsolutePath()});
   1.120 +        } finally {
   1.121 +            System.setOut(prevOut);
   1.122 +        }
   1.123 +
   1.124 +        if (bytes.toString().trim().equals(INDICATOR)) {
   1.125 +            System.out.println("PASSED");
   1.126 +        } else {
   1.127 +            throw new Exception("Processor did not run correctly. Output: " + bytes);
   1.128 +        }
   1.129 +    }
   1.130 +}

mercurial