Wed, 23 Jan 2013 13:27:24 -0800
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
jjg@1521 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2009 Oracle and/or its affiliates. All rights reserved. |
jjg@1521 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1521 | 4 | * |
jjg@1521 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1521 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1521 | 7 | * published by the Free Software Foundation. |
jjg@1521 | 8 | * |
jjg@1521 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1521 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1521 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1521 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1521 | 13 | * accompanied this code). |
jjg@1521 | 14 | * |
jjg@1521 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1521 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1521 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1521 | 18 | * |
jjg@1521 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1521 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1521 | 21 | * questions. |
jjg@1521 | 22 | */ |
jjg@1521 | 23 | import java.util.HashSet; |
jjg@1521 | 24 | import java.util.Set; |
jjg@1521 | 25 | |
jjg@1521 | 26 | import javax.annotation.processing.*; |
jjg@1521 | 27 | import javax.lang.model.SourceVersion; |
jjg@1521 | 28 | import javax.lang.model.element.*; |
jjg@1521 | 29 | import javax.lang.model.util.ElementFilter; |
jjg@1521 | 30 | |
jjg@1521 | 31 | import com.sun.source.util.JavacTask; |
jjg@1521 | 32 | import com.sun.source.util.TaskEvent; |
jjg@1521 | 33 | import com.sun.source.util.TaskListener; |
jjg@1521 | 34 | import com.sun.source.util.TreePath; |
jjg@1521 | 35 | import com.sun.tools.javac.main.JavaCompiler; |
jjg@1521 | 36 | import com.sun.tools.javac.main.JavaCompiler.CompileState; |
jjg@1521 | 37 | import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
jjg@1521 | 38 | import com.sun.tools.javac.util.Context; |
jjg@1521 | 39 | |
jjg@1521 | 40 | /* |
jjg@1521 | 41 | * @test |
jjg@1521 | 42 | * @summary test that package annotations are available to type processors. |
jjg@1521 | 43 | * This class implements the functionality of a type processor, as previously |
jjg@1521 | 44 | * embodied by the AbstractTypeProcessor class. |
jjg@1521 | 45 | * |
jjg@1521 | 46 | * @author Mahmood Ali |
jjg@1521 | 47 | * @author Werner Dietl |
jjg@1521 | 48 | * |
jjg@1521 | 49 | * @compile PackageProcessor.java |
jjg@1521 | 50 | * @compile -cp . -processor PackageProcessor mypackage/Anno.java mypackage/MyClass.java mypackage/package-info.java |
jjg@1521 | 51 | */ |
jjg@1521 | 52 | |
jjg@1521 | 53 | @SupportedAnnotationTypes("*") |
jjg@1521 | 54 | public class PackageProcessor extends AbstractProcessor { |
jjg@1521 | 55 | |
jjg@1521 | 56 | private final AttributionTaskListener listener = new AttributionTaskListener(); |
jjg@1521 | 57 | private final Set<Name> elements = new HashSet<Name>(); |
jjg@1521 | 58 | |
jjg@1521 | 59 | @Override |
jjg@1521 | 60 | public final void init(ProcessingEnvironment env) { |
jjg@1521 | 61 | super.init(env); |
jjg@1521 | 62 | JavacTask.instance(env).addTaskListener(listener); |
jjg@1521 | 63 | Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext(); |
jjg@1521 | 64 | JavaCompiler compiler = JavaCompiler.instance(ctx); |
jjg@1521 | 65 | compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError, |
jjg@1521 | 66 | CompileState.FLOW); |
jjg@1521 | 67 | } |
jjg@1521 | 68 | |
jjg@1521 | 69 | @Override |
jjg@1521 | 70 | public final boolean process(Set<? extends TypeElement> annotations, |
jjg@1521 | 71 | RoundEnvironment roundEnv) { |
jjg@1521 | 72 | for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { |
jjg@1521 | 73 | elements.add(elem.getQualifiedName()); |
jjg@1521 | 74 | } |
jjg@1521 | 75 | return false; |
jjg@1521 | 76 | } |
jjg@1521 | 77 | |
jjg@1521 | 78 | @Override |
jjg@1521 | 79 | public SourceVersion getSupportedSourceVersion() { |
jjg@1521 | 80 | return SourceVersion.latest(); |
jjg@1521 | 81 | } |
jjg@1521 | 82 | |
jjg@1521 | 83 | private final class AttributionTaskListener implements TaskListener { |
jjg@1521 | 84 | @Override |
jjg@1521 | 85 | public void started(TaskEvent e) { } |
jjg@1521 | 86 | |
jjg@1521 | 87 | @Override |
jjg@1521 | 88 | public void finished(TaskEvent e) { |
jjg@1521 | 89 | if (e.getKind() != TaskEvent.Kind.ANALYZE) |
jjg@1521 | 90 | return; |
jjg@1521 | 91 | |
jjg@1521 | 92 | if (!elements.remove(e.getTypeElement().getQualifiedName())) |
jjg@1521 | 93 | return; |
jjg@1521 | 94 | |
jjg@1521 | 95 | if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) { |
jjg@1521 | 96 | Element owner = e.getTypeElement().getEnclosingElement(); |
jjg@1521 | 97 | if (owner.getKind() != ElementKind.PACKAGE) |
jjg@1521 | 98 | throw new RuntimeException("class owner should be a package: " + owner); |
jjg@1521 | 99 | if (owner.getAnnotationMirrors().size() != 1) |
jjg@1521 | 100 | throw new RuntimeException("the owner package should have one annotation: " + owner); |
jjg@1521 | 101 | } |
jjg@1521 | 102 | } |
jjg@1521 | 103 | } |
jjg@1521 | 104 | |
jjg@1521 | 105 | } |