Mon, 01 Jul 2013 11:58:45 -0700
7162089: Add support for repeating annotations to javax.annotation.processing
Reviewed-by: abuckley, jjg, jfranck
1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Mon Jul 01 14:57:03 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Mon Jul 01 11:58:45 2013 -0700 1.3 @@ -36,10 +36,7 @@ 1.4 1.5 import javax.annotation.processing.*; 1.6 import javax.lang.model.SourceVersion; 1.7 -import javax.lang.model.element.AnnotationMirror; 1.8 -import javax.lang.model.element.Element; 1.9 -import javax.lang.model.element.PackageElement; 1.10 -import javax.lang.model.element.TypeElement; 1.11 +import javax.lang.model.element.*; 1.12 import javax.lang.model.util.*; 1.13 import javax.tools.DiagnosticListener; 1.14 import javax.tools.JavaFileManager; 1.15 @@ -762,12 +759,30 @@ 1.16 } 1.17 1.18 @Override 1.19 - public Set<TypeElement> scan(Element e, Set<TypeElement> p) { 1.20 + public Set<TypeElement> visitType(TypeElement e, Set<TypeElement> p) { 1.21 + // Type parameters are not considered to be enclosed by a type 1.22 + scan(e.getTypeParameters(), p); 1.23 + return scan(e.getEnclosedElements(), p); 1.24 + } 1.25 + 1.26 + @Override 1.27 + public Set<TypeElement> visitExecutable(ExecutableElement e, Set<TypeElement> p) { 1.28 + // Type parameters are not considered to be enclosed by an executable 1.29 + scan(e.getTypeParameters(), p); 1.30 + return scan(e.getEnclosedElements(), p); 1.31 + } 1.32 + 1.33 + void addAnnotations(Element e, Set<TypeElement> p) { 1.34 for (AnnotationMirror annotationMirror : 1.35 elements.getAllAnnotationMirrors(e) ) { 1.36 Element e2 = annotationMirror.getAnnotationType().asElement(); 1.37 p.add((TypeElement) e2); 1.38 } 1.39 + } 1.40 + 1.41 + @Override 1.42 + public Set<TypeElement> scan(Element e, Set<TypeElement> p) { 1.43 + addAnnotations(e, p); 1.44 return super.scan(e, p); 1.45 } 1.46 }
2.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Mon Jul 01 14:57:03 2013 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Mon Jul 01 11:58:45 2013 -0700 2.3 @@ -147,6 +147,20 @@ 2.4 } 2.5 2.6 @Override 2.7 + public Set<Element> visitType(TypeElement e, DeclaredType p) { 2.8 + // Type parameters are not considered to be enclosed by a type 2.9 + scan(e.getTypeParameters(), p); 2.10 + return scan(e.getEnclosedElements(), p); 2.11 + } 2.12 + 2.13 + @Override 2.14 + public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) { 2.15 + // Type parameters are not considered to be enclosed by an executable 2.16 + scan(e.getTypeParameters(), p); 2.17 + return scan(e.getEnclosedElements(), p); 2.18 + } 2.19 + 2.20 + @Override 2.21 public Set<Element> scan(Element e, DeclaredType p) { 2.22 java.util.List<? extends AnnotationMirror> annotationMirrors = 2.23 processingEnv.getElementUtils().getAllAnnotationMirrors(e); 2.24 @@ -157,7 +171,6 @@ 2.25 e.accept(this, p); 2.26 return annotatedElements; 2.27 } 2.28 - 2.29 } 2.30 2.31 /**
3.1 --- a/src/share/classes/javax/annotation/processing/AbstractProcessor.java Mon Jul 01 14:57:03 2013 +0100 3.2 +++ b/src/share/classes/javax/annotation/processing/AbstractProcessor.java Mon Jul 01 11:58:45 2013 -0700 3.3 @@ -38,7 +38,7 @@ 3.4 * superclass for most concrete annotation processors. This class 3.5 * examines annotation values to compute the {@linkplain 3.6 * #getSupportedOptions options}, {@linkplain 3.7 - * #getSupportedAnnotationTypes annotations}, and {@linkplain 3.8 + * #getSupportedAnnotationTypes annotation types}, and {@linkplain 3.9 * #getSupportedSourceVersion source version} supported by its 3.10 * subtypes. 3.11 *
4.1 --- a/src/share/classes/javax/annotation/processing/Processor.java Mon Jul 01 14:57:03 2013 +0100 4.2 +++ b/src/share/classes/javax/annotation/processing/Processor.java Mon Jul 01 11:58:45 2013 -0700 4.3 @@ -1,5 +1,5 @@ 4.4 /* 4.5 - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 4.6 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 4.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.8 * 4.9 * This code is free software; you can redistribute it and/or modify it 4.10 @@ -26,6 +26,8 @@ 4.11 package javax.annotation.processing; 4.12 4.13 import java.util.Set; 4.14 +import javax.lang.model.util.Elements; 4.15 +import javax.lang.model.AnnotatedConstruct; 4.16 import javax.lang.model.element.*; 4.17 import javax.lang.model.SourceVersion; 4.18 4.19 @@ -88,23 +90,52 @@ 4.20 * configuration mechanisms, such as command line options; for 4.21 * details, refer to the particular tool's documentation. Which 4.22 * processors the tool asks to {@linkplain #process run} is a function 4.23 - * of what annotations are present on the {@linkplain 4.24 + * of the types of the annotations <em>{@linkplain AnnotatedConstruct present}</em> 4.25 + * on the {@linkplain 4.26 * RoundEnvironment#getRootElements root elements}, what {@linkplain 4.27 * #getSupportedAnnotationTypes annotation types a processor 4.28 - * processes}, and whether or not a processor {@linkplain #process 4.29 - * claims the annotations it processes}. A processor will be asked to 4.30 + * supports}, and whether or not a processor {@linkplain #process 4.31 + * claims the annotation types it processes}. A processor will be asked to 4.32 * process a subset of the annotation types it supports, possibly an 4.33 * empty set. 4.34 * 4.35 - * For a given round, the tool computes the set of annotation types on 4.36 - * the root elements. If there is at least one annotation type 4.37 - * present, as processors claim annotation types, they are removed 4.38 - * from the set of unmatched annotations. When the set is empty or no 4.39 - * more processors are available, the round has run to completion. If 4.40 + * For a given round, the tool computes the set of annotation types 4.41 + * that are present on the elements enclosed within the root elements. 4.42 + * If there is at least one annotation type present, then as 4.43 + * processors claim annotation types, they are removed from the set of 4.44 + * unmatched annotation types. When the set is empty or no more 4.45 + * processors are available, the round has run to completion. If 4.46 * there are no annotation types present, annotation processing still 4.47 * occurs but only <i>universal processors</i> which support 4.48 - * processing {@code "*"} can claim the (empty) set of annotation 4.49 - * types. 4.50 + * processing all annotation types, {@code "*"}, can claim the (empty) 4.51 + * set of annotation types. 4.52 + * 4.53 + * <p>An annotation type is considered present if there is at least 4.54 + * one annotation of that type present on an element enclosed within 4.55 + * the root elements of a round. For this purpose, a type parameter is 4.56 + * considered to be enclosed by its {@linkplain 4.57 + * TypeParameterElement#getGenericElement generic 4.58 + * element}. Annotations on {@linkplain 4.59 + * java.lang.annotation.ElementType#TYPE_USE type uses}, as opposed to 4.60 + * annotations on elements, are ignored when computing whether or not 4.61 + * an annotation type is present. 4.62 + * 4.63 + * <p>An annotation is present if it meets the definition of being 4.64 + * present given in {@link AnnotatedConstruct}. In brief, an 4.65 + * annotation is considered present for the purposes of discovery if 4.66 + * it is directly present or present via inheritance. An annotation is 4.67 + * <em>not</em> considered present by virtue of being wrapped by a 4.68 + * container annotation. Operationally, this is equivalent to an 4.69 + * annotation being present on an element if and only if it would be 4.70 + * included in the results of {@link 4.71 + * Elements#getAllAnnotationMirrors(Element)} called on that element. Since 4.72 + * annotations inside container annotations are not considered 4.73 + * present, to properly process {@linkplain 4.74 + * java.lang.annotation.Repeatable repeatable annotation types}, 4.75 + * processors are advised to include both the repeatable annotation 4.76 + * type and its containing annotation type in the set of {@linkplain 4.77 + * #getSupportedAnnotationTypes() supported annotation types} of a 4.78 + * processor. 4.79 * 4.80 * <p>Note that if a processor supports {@code "*"} and returns {@code 4.81 * true}, all annotations are claimed. Therefore, a universal 4.82 @@ -257,10 +288,10 @@ 4.83 /** 4.84 * Processes a set of annotation types on type elements 4.85 * originating from the prior round and returns whether or not 4.86 - * these annotations are claimed by this processor. If {@code 4.87 - * true} is returned, the annotations are claimed and subsequent 4.88 + * these annotation types are claimed by this processor. If {@code 4.89 + * true} is returned, the annotation types are claimed and subsequent 4.90 * processors will not be asked to process them; if {@code false} 4.91 - * is returned, the annotations are unclaimed and subsequent 4.92 + * is returned, the annotation types are unclaimed and subsequent 4.93 * processors may be asked to process them. A processor may 4.94 * always return the same boolean value or may vary the result 4.95 * based on chosen criteria. 4.96 @@ -271,7 +302,7 @@ 4.97 * 4.98 * @param annotations the annotation types requested to be processed 4.99 * @param roundEnv environment for information about the current and prior round 4.100 - * @return whether or not the set of annotations are claimed by this processor 4.101 + * @return whether or not the set of annotation types are claimed by this processor 4.102 */ 4.103 boolean process(Set<? extends TypeElement> annotations, 4.104 RoundEnvironment roundEnv);
5.1 --- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Mon Jul 01 14:57:03 2013 +0100 5.2 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Mon Jul 01 11:58:45 2013 -0700 5.3 @@ -30,11 +30,13 @@ 5.4 * @build JavacTestingAbstractProcessor 5.5 * @compile TestElementsAnnotatedWith.java 5.6 * @compile InheritedAnnotation.java 5.7 + * @compile TpAnno.java 5.8 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 5.9 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 5.10 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 5.11 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 5.12 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 5.13 + * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 5.14 * @compile Foo.java 5.15 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 5.16 */
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/processing/environment/round/TpAnno.java Mon Jul 01 11:58:45 2013 -0700 6.3 @@ -0,0 +1,29 @@ 6.4 +/* 6.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +import java.lang.annotation.*; 6.28 +import static java.lang.annotation.RetentionPolicy.*; 6.29 + 6.30 +@Retention(RUNTIME) 6.31 +@Target(ElementType.TYPE_PARAMETER) 6.32 +public @interface TpAnno {}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java Mon Jul 01 11:58:45 2013 -0700 7.3 @@ -0,0 +1,37 @@ 7.4 +/* 7.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/** 7.28 + * Class to hold annotations for ElementsAnnotatedWithTest. 7.29 + */ 7.30 + 7.31 +@AnnotatedElementInfo(annotationName="TpAnno", 7.32 + expectedSize=4, 7.33 + names={"T", "A", "B", "C"}) 7.34 +public class TypeParameterAnnotations<@TpAnno T> { 7.35 + private <@TpAnno A> TypeParameterAnnotations(A a) {;} 7.36 + 7.37 + public <@TpAnno B> void foo(B b) {return;} 7.38 + 7.39 + public static <@TpAnno C> void bar(C b) {return;} 7.40 +}