jjg@1521: /* darcy@1534: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. jjg@1521: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1521: * jjg@1521: * This code is free software; you can redistribute it and/or modify it jjg@1521: * under the terms of the GNU General Public License version 2 only, as jjg@1521: * published by the Free Software Foundation. jjg@1521: * jjg@1521: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1521: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1521: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1521: * version 2 for more details (a copy is included in the LICENSE file that jjg@1521: * accompanied this code). jjg@1521: * jjg@1521: * You should have received a copy of the GNU General Public License version jjg@1521: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1521: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1521: * jjg@1521: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1521: * or visit www.oracle.com if you need additional information or have any jjg@1521: * questions. jjg@1521: */ jjg@1521: import java.util.HashSet; jjg@1521: import java.util.Set; jjg@1521: jjg@1521: import javax.annotation.processing.*; jjg@1521: import javax.lang.model.SourceVersion; jjg@1521: import javax.lang.model.element.*; jjg@1521: import javax.lang.model.util.ElementFilter; jjg@1521: jjg@1521: import com.sun.source.util.JavacTask; jjg@1521: import com.sun.source.util.TaskEvent; jjg@1521: import com.sun.source.util.TaskListener; jjg@1521: import com.sun.source.util.TreePath; jjg@1521: import com.sun.tools.javac.main.JavaCompiler; jjg@1521: import com.sun.tools.javac.main.JavaCompiler.CompileState; jjg@1521: import com.sun.tools.javac.processing.JavacProcessingEnvironment; jjg@1521: import com.sun.tools.javac.util.Context; jjg@1521: jjg@1521: /* jjg@1521: * @test jjg@1521: * @summary test that package annotations are available to type processors. jjg@1521: * This class implements the functionality of a type processor, as previously jjg@1521: * embodied by the AbstractTypeProcessor class. jjg@1521: * jjg@1521: * @author Mahmood Ali jjg@1521: * @author Werner Dietl jjg@1521: * jjg@1521: * @compile PackageProcessor.java jjg@1521: * @compile -cp . -processor PackageProcessor mypackage/Anno.java mypackage/MyClass.java mypackage/package-info.java jjg@1521: */ jjg@1521: jjg@1521: @SupportedAnnotationTypes("*") jjg@1521: public class PackageProcessor extends AbstractProcessor { jjg@1521: jjg@1521: private final AttributionTaskListener listener = new AttributionTaskListener(); jjg@1521: private final Set elements = new HashSet(); jjg@1521: jjg@1521: @Override jjg@1521: public final void init(ProcessingEnvironment env) { jjg@1521: super.init(env); jjg@1521: JavacTask.instance(env).addTaskListener(listener); jjg@1521: Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext(); jjg@1521: JavaCompiler compiler = JavaCompiler.instance(ctx); jjg@1521: compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError, jjg@1521: CompileState.FLOW); jjg@1521: } jjg@1521: jjg@1521: @Override jjg@1521: public final boolean process(Set annotations, jjg@1521: RoundEnvironment roundEnv) { jjg@1521: for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { jjg@1521: elements.add(elem.getQualifiedName()); jjg@1521: } jjg@1521: return false; jjg@1521: } jjg@1521: jjg@1521: @Override jjg@1521: public SourceVersion getSupportedSourceVersion() { jjg@1521: return SourceVersion.latest(); jjg@1521: } jjg@1521: jjg@1521: private final class AttributionTaskListener implements TaskListener { jjg@1521: @Override jjg@1521: public void started(TaskEvent e) { } jjg@1521: jjg@1521: @Override jjg@1521: public void finished(TaskEvent e) { jjg@1521: if (e.getKind() != TaskEvent.Kind.ANALYZE) jjg@1521: return; jjg@1521: jjg@1521: if (!elements.remove(e.getTypeElement().getQualifiedName())) jjg@1521: return; jjg@1521: jjg@1521: if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) { jjg@1521: Element owner = e.getTypeElement().getEnclosingElement(); jjg@1521: if (owner.getKind() != ElementKind.PACKAGE) jjg@1521: throw new RuntimeException("class owner should be a package: " + owner); jjg@1521: if (owner.getAnnotationMirrors().size() != 1) jjg@1521: throw new RuntimeException("the owner package should have one annotation: " + owner); jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: }