PackageProcessor.java revision 3170:dc017a37aac5
1119420Sobrien/*
2178466Smarius * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
353790Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
453790Sobrien *
586266Sgroudier * This code is free software; you can redistribute it and/or modify it
653790Sobrien * under the terms of the GNU General Public License version 2 only, as
753790Sobrien * published by the Free Software Foundation.
859743Sgroudier *
959743Sgroudier * This code is distributed in the hope that it will be useful, but WITHOUT
1053790Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11178466Smarius * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1253790Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1353790Sobrien * accompanied this code).
1453790Sobrien *
15178466Smarius * You should have received a copy of the GNU General Public License version
1653790Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1753790Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1853790Sobrien *
1953790Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2053790Sobrien * or visit www.oracle.com if you need additional information or have any
2153790Sobrien * questions.
2253790Sobrien */
23178466Smarius
24178466Smariusimport java.util.HashSet;
2553790Sobrienimport java.util.Set;
2653790Sobrien
2753790Sobrienimport javax.annotation.processing.*;
2853790Sobrienimport javax.lang.model.SourceVersion;
2953790Sobrienimport javax.lang.model.element.*;
3053790Sobrienimport javax.lang.model.util.ElementFilter;
3153790Sobrien
3253790Sobrienimport com.sun.source.util.JavacTask;
3353790Sobrienimport com.sun.source.util.TaskEvent;
3453790Sobrienimport com.sun.source.util.TaskListener;
3553790Sobrienimport com.sun.tools.javac.main.JavaCompiler;
3653790Sobrienimport com.sun.tools.javac.processing.JavacProcessingEnvironment;
3753790Sobrienimport com.sun.tools.javac.util.Context;
3853790Sobrien
3953790Sobrienimport static com.sun.tools.javac.comp.CompileStates.CompileState;
4053790Sobrien
4153790Sobrien/*
4253790Sobrien * @test
4353790Sobrien * @summary test that package annotations are available to type processors.
4453790Sobrien * This class implements the functionality of a type processor, as previously
4553790Sobrien * embodied by the AbstractTypeProcessor class.
4653790Sobrien *
4753790Sobrien * @author Mahmood Ali
4853790Sobrien * @author Werner Dietl
4953790Sobrien *
5053790Sobrien * @modules jdk.compiler/com.sun.tools.javac.comp
5153790Sobrien *          jdk.compiler/com.sun.tools.javac.main
5253790Sobrien *          jdk.compiler/com.sun.tools.javac.processing
5353790Sobrien *          jdk.compiler/com.sun.tools.javac.util
5453790Sobrien * @compile PackageProcessor.java
5553790Sobrien * @compile -cp . -processor PackageProcessor mypackage/Anno.java mypackage/MyClass.java mypackage/package-info.java
5653790Sobrien */
5753790Sobrien
58119420Sobrien@SupportedAnnotationTypes("*")
59119420Sobrienpublic class PackageProcessor extends AbstractProcessor {
6055258Sobrien
6165404Sgroudier    private final AttributionTaskListener listener = new AttributionTaskListener();
6253790Sobrien    private final Set<Name> elements = new HashSet<Name>();
6360134Sgroudier
6460134Sgroudier    @Override
6561429Sgroudier    public final void init(ProcessingEnvironment env) {
6653790Sobrien        super.init(env);
6753790Sobrien        JavacTask.instance(env).addTaskListener(listener);
6861429Sgroudier        Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
6961429Sgroudier        JavaCompiler compiler = JavaCompiler.instance(ctx);
7061429Sgroudier        compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError,
7161429Sgroudier                CompileState.FLOW);
7261429Sgroudier    }
7353790Sobrien
7453790Sobrien    @Override
7595533Smike    public final boolean process(Set<? extends TypeElement> annotations,
7653790Sobrien            RoundEnvironment roundEnv) {
77117126Sscottl        for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) {
78117126Sscottl            elements.add(elem.getQualifiedName());
7953790Sobrien        }
8053790Sobrien        return false;
8153790Sobrien    }
8253790Sobrien
8353790Sobrien    @Override
84119287Simp    public SourceVersion getSupportedSourceVersion() {
85119287Simp        return SourceVersion.latest();
8653790Sobrien    }
8753790Sobrien
8853790Sobrien    private final class AttributionTaskListener implements TaskListener {
89245275Simp        @Override
90207285Smarius        public void started(TaskEvent e) { }
91207285Smarius
92207285Smarius        @Override
93207285Smarius        public void finished(TaskEvent e) {
94207285Smarius            if (e.getKind() != TaskEvent.Kind.ANALYZE)
95207285Smarius                return;
9653790Sobrien
9753790Sobrien            if (!elements.remove(e.getTypeElement().getQualifiedName()))
9853790Sobrien                return;
9953790Sobrien
10053790Sobrien            if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) {
10153790Sobrien                Element owner = e.getTypeElement().getEnclosingElement();
10253790Sobrien                if (owner.getKind() != ElementKind.PACKAGE)
10353790Sobrien                    throw new RuntimeException("class owner should be a package: " + owner);
10453790Sobrien                if (owner.getAnnotationMirrors().size() != 1)
10553790Sobrien                    throw new RuntimeException("the owner package should have one annotation: " + owner);
10653790Sobrien            }
10753790Sobrien        }
10853790Sobrien    }
10953790Sobrien
11053790Sobrien}
11153790Sobrien