1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/typeAnnotations/packageanno/PackageProcessor.java Wed Jan 23 13:27:24 2013 -0800 1.3 @@ -0,0 +1,105 @@ 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.util.HashSet; 1.27 +import java.util.Set; 1.28 + 1.29 +import javax.annotation.processing.*; 1.30 +import javax.lang.model.SourceVersion; 1.31 +import javax.lang.model.element.*; 1.32 +import javax.lang.model.util.ElementFilter; 1.33 + 1.34 +import com.sun.source.util.JavacTask; 1.35 +import com.sun.source.util.TaskEvent; 1.36 +import com.sun.source.util.TaskListener; 1.37 +import com.sun.source.util.TreePath; 1.38 +import com.sun.tools.javac.main.JavaCompiler; 1.39 +import com.sun.tools.javac.main.JavaCompiler.CompileState; 1.40 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 1.41 +import com.sun.tools.javac.util.Context; 1.42 + 1.43 +/* 1.44 + * @test 1.45 + * @summary test that package annotations are available to type processors. 1.46 + * This class implements the functionality of a type processor, as previously 1.47 + * embodied by the AbstractTypeProcessor class. 1.48 + * 1.49 + * @author Mahmood Ali 1.50 + * @author Werner Dietl 1.51 + * 1.52 + * @compile PackageProcessor.java 1.53 + * @compile -cp . -processor PackageProcessor mypackage/Anno.java mypackage/MyClass.java mypackage/package-info.java 1.54 + */ 1.55 + 1.56 +@SupportedAnnotationTypes("*") 1.57 +public class PackageProcessor extends AbstractProcessor { 1.58 + 1.59 + private final AttributionTaskListener listener = new AttributionTaskListener(); 1.60 + private final Set<Name> elements = new HashSet<Name>(); 1.61 + 1.62 + @Override 1.63 + public final void init(ProcessingEnvironment env) { 1.64 + super.init(env); 1.65 + JavacTask.instance(env).addTaskListener(listener); 1.66 + Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext(); 1.67 + JavaCompiler compiler = JavaCompiler.instance(ctx); 1.68 + compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError, 1.69 + CompileState.FLOW); 1.70 + } 1.71 + 1.72 + @Override 1.73 + public final boolean process(Set<? extends TypeElement> annotations, 1.74 + RoundEnvironment roundEnv) { 1.75 + for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { 1.76 + elements.add(elem.getQualifiedName()); 1.77 + } 1.78 + return false; 1.79 + } 1.80 + 1.81 + @Override 1.82 + public SourceVersion getSupportedSourceVersion() { 1.83 + return SourceVersion.latest(); 1.84 + } 1.85 + 1.86 + private final class AttributionTaskListener implements TaskListener { 1.87 + @Override 1.88 + public void started(TaskEvent e) { } 1.89 + 1.90 + @Override 1.91 + public void finished(TaskEvent e) { 1.92 + if (e.getKind() != TaskEvent.Kind.ANALYZE) 1.93 + return; 1.94 + 1.95 + if (!elements.remove(e.getTypeElement().getQualifiedName())) 1.96 + return; 1.97 + 1.98 + if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) { 1.99 + Element owner = e.getTypeElement().getEnclosingElement(); 1.100 + if (owner.getKind() != ElementKind.PACKAGE) 1.101 + throw new RuntimeException("class owner should be a package: " + owner); 1.102 + if (owner.getAnnotationMirrors().size() != 1) 1.103 + throw new RuntimeException("the owner package should have one annotation: " + owner); 1.104 + } 1.105 + } 1.106 + } 1.107 + 1.108 +}