test/tools/javac/annotations/typeAnnotations/packageanno/PackageProcessor.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
child 1534
bec996065c45
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

     1 /*
     2  * Copyright (c) 2009 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 import java.util.HashSet;
    24 import java.util.Set;
    26 import javax.annotation.processing.*;
    27 import javax.lang.model.SourceVersion;
    28 import javax.lang.model.element.*;
    29 import javax.lang.model.util.ElementFilter;
    31 import com.sun.source.util.JavacTask;
    32 import com.sun.source.util.TaskEvent;
    33 import com.sun.source.util.TaskListener;
    34 import com.sun.source.util.TreePath;
    35 import com.sun.tools.javac.main.JavaCompiler;
    36 import com.sun.tools.javac.main.JavaCompiler.CompileState;
    37 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    38 import com.sun.tools.javac.util.Context;
    40 /*
    41  * @test
    42  * @summary test that package annotations are available to type processors.
    43  * This class implements the functionality of a type processor, as previously
    44  * embodied by the AbstractTypeProcessor class.
    45  *
    46  * @author Mahmood Ali
    47  * @author Werner Dietl
    48  *
    49  * @compile PackageProcessor.java
    50  * @compile -cp . -processor PackageProcessor mypackage/Anno.java mypackage/MyClass.java mypackage/package-info.java
    51  */
    53 @SupportedAnnotationTypes("*")
    54 public class PackageProcessor extends AbstractProcessor {
    56     private final AttributionTaskListener listener = new AttributionTaskListener();
    57     private final Set<Name> elements = new HashSet<Name>();
    59     @Override
    60     public final void init(ProcessingEnvironment env) {
    61         super.init(env);
    62         JavacTask.instance(env).addTaskListener(listener);
    63         Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
    64         JavaCompiler compiler = JavaCompiler.instance(ctx);
    65         compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError,
    66                 CompileState.FLOW);
    67     }
    69     @Override
    70     public final boolean process(Set<? extends TypeElement> annotations,
    71             RoundEnvironment roundEnv) {
    72         for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) {
    73             elements.add(elem.getQualifiedName());
    74         }
    75         return false;
    76     }
    78     @Override
    79     public SourceVersion getSupportedSourceVersion() {
    80         return SourceVersion.latest();
    81     }
    83     private final class AttributionTaskListener implements TaskListener {
    84         @Override
    85         public void started(TaskEvent e) { }
    87         @Override
    88         public void finished(TaskEvent e) {
    89             if (e.getKind() != TaskEvent.Kind.ANALYZE)
    90                 return;
    92             if (!elements.remove(e.getTypeElement().getQualifiedName()))
    93                 return;
    95             if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) {
    96                 Element owner = e.getTypeElement().getEnclosingElement();
    97                 if (owner.getKind() != ElementKind.PACKAGE)
    98                     throw new RuntimeException("class owner should be a package: " + owner);
    99                 if (owner.getAnnotationMirrors().size() != 1)
   100                     throw new RuntimeException("the owner package should have one annotation: " + owner);
   101             }
   102         }
   103     }
   105 }

mercurial