changeset 1875:1908e86ee49a

7162089: Add support for repeating annotations to javax.annotation.processing Reviewed-by: abuckley, jjg, jfranck
author darcy
date Mon, 01 Jul 2013 11:58:45 -0700
parents f559ef7568ce
children 27a2e8c78bd0
files src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java src/share/classes/javax/annotation/processing/AbstractProcessor.java src/share/classes/javax/annotation/processing/Processor.java test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java test/tools/javac/processing/environment/round/TpAnno.java test/tools/javac/processing/environment/round/TypeParameterAnnotations.java
diffstat 7 files changed, 149 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Jul 01 14:57:03 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Jul 01 11:58:45 2013 -0700
@@ -36,10 +36,7 @@
 
 import javax.annotation.processing.*;
 import javax.lang.model.SourceVersion;
-import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.PackageElement;
-import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.*;
 import javax.lang.model.util.*;
 import javax.tools.DiagnosticListener;
 import javax.tools.JavaFileManager;
@@ -762,12 +759,30 @@
         }
 
         @Override
-        public Set<TypeElement> scan(Element e, Set<TypeElement> p) {
+        public Set<TypeElement> visitType(TypeElement e, Set<TypeElement> p) {
+            // Type parameters are not considered to be enclosed by a type
+            scan(e.getTypeParameters(), p);
+            return scan(e.getEnclosedElements(), p);
+        }
+
+        @Override
+        public Set<TypeElement> visitExecutable(ExecutableElement e, Set<TypeElement> p) {
+            // Type parameters are not considered to be enclosed by an executable
+            scan(e.getTypeParameters(), p);
+            return scan(e.getEnclosedElements(), p);
+        }
+
+        void addAnnotations(Element e, Set<TypeElement> p) {
             for (AnnotationMirror annotationMirror :
                      elements.getAllAnnotationMirrors(e) ) {
                 Element e2 = annotationMirror.getAnnotationType().asElement();
                 p.add((TypeElement) e2);
             }
+        }
+
+        @Override
+        public Set<TypeElement> scan(Element e, Set<TypeElement> p) {
+            addAnnotations(e, p);
             return super.scan(e, p);
         }
     }
--- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Mon Jul 01 14:57:03 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Mon Jul 01 11:58:45 2013 -0700
@@ -147,6 +147,20 @@
         }
 
         @Override
+        public Set<Element> visitType(TypeElement e, DeclaredType p) {
+            // Type parameters are not considered to be enclosed by a type
+            scan(e.getTypeParameters(), p);
+            return scan(e.getEnclosedElements(), p);
+        }
+
+        @Override
+        public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) {
+            // Type parameters are not considered to be enclosed by an executable
+            scan(e.getTypeParameters(), p);
+            return scan(e.getEnclosedElements(), p);
+        }
+
+        @Override
         public Set<Element> scan(Element e, DeclaredType p) {
             java.util.List<? extends AnnotationMirror> annotationMirrors =
                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
@@ -157,7 +171,6 @@
             e.accept(this, p);
             return annotatedElements;
         }
-
     }
 
     /**
--- a/src/share/classes/javax/annotation/processing/AbstractProcessor.java	Mon Jul 01 14:57:03 2013 +0100
+++ b/src/share/classes/javax/annotation/processing/AbstractProcessor.java	Mon Jul 01 11:58:45 2013 -0700
@@ -38,7 +38,7 @@
  * superclass for most concrete annotation processors.  This class
  * examines annotation values to compute the {@linkplain
  * #getSupportedOptions options}, {@linkplain
- * #getSupportedAnnotationTypes annotations}, and {@linkplain
+ * #getSupportedAnnotationTypes annotation types}, and {@linkplain
  * #getSupportedSourceVersion source version} supported by its
  * subtypes.
  *
--- a/src/share/classes/javax/annotation/processing/Processor.java	Mon Jul 01 14:57:03 2013 +0100
+++ b/src/share/classes/javax/annotation/processing/Processor.java	Mon Jul 01 11:58:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,8 @@
 package javax.annotation.processing;
 
 import java.util.Set;
+import javax.lang.model.util.Elements;
+import javax.lang.model.AnnotatedConstruct;
 import javax.lang.model.element.*;
 import javax.lang.model.SourceVersion;
 
@@ -88,23 +90,52 @@
  * configuration mechanisms, such as command line options; for
  * details, refer to the particular tool's documentation.  Which
  * processors the tool asks to {@linkplain #process run} is a function
- * of what annotations are present on the {@linkplain
+ * of the types of the annotations <em>{@linkplain AnnotatedConstruct present}</em>
+ * on the {@linkplain
  * RoundEnvironment#getRootElements root elements}, what {@linkplain
  * #getSupportedAnnotationTypes annotation types a processor
- * processes}, and whether or not a processor {@linkplain #process
- * claims the annotations it processes}.  A processor will be asked to
+ * supports}, and whether or not a processor {@linkplain #process
+ * claims the annotation types it processes}.  A processor will be asked to
  * process a subset of the annotation types it supports, possibly an
  * empty set.
  *
- * For a given round, the tool computes the set of annotation types on
- * the root elements.  If there is at least one annotation type
- * present, as processors claim annotation types, they are removed
- * from the set of unmatched annotations.  When the set is empty or no
- * more processors are available, the round has run to completion.  If
+ * For a given round, the tool computes the set of annotation types
+ * that are present on the elements enclosed within the root elements.
+ * If there is at least one annotation type present, then as
+ * processors claim annotation types, they are removed from the set of
+ * unmatched annotation types.  When the set is empty or no more
+ * processors are available, the round has run to completion.  If
  * there are no annotation types present, annotation processing still
  * occurs but only <i>universal processors</i> which support
- * processing {@code "*"} can claim the (empty) set of annotation
- * types.
+ * processing all annotation types, {@code "*"}, can claim the (empty)
+ * set of annotation types.
+ *
+ * <p>An annotation type is considered present if there is at least
+ * one annotation of that type present on an element enclosed within
+ * the root elements of a round. For this purpose, a type parameter is
+ * considered to be enclosed by its {@linkplain
+ * TypeParameterElement#getGenericElement generic
+ * element}. Annotations on {@linkplain
+ * java.lang.annotation.ElementType#TYPE_USE type uses}, as opposed to
+ * annotations on elements, are ignored when computing whether or not
+ * an annotation type is present.
+ *
+ * <p>An annotation is present if it meets the definition of being
+ * present given in {@link AnnotatedConstruct}. In brief, an
+ * annotation is considered present for the purposes of discovery if
+ * it is directly present or present via inheritance. An annotation is
+ * <em>not</em> considered present by virtue of being wrapped by a
+ * container annotation. Operationally, this is equivalent to an
+ * annotation being present on an element if and only if it would be
+ * included in the results of {@link
+ * Elements#getAllAnnotationMirrors(Element)} called on that element. Since
+ * annotations inside container annotations are not considered
+ * present, to properly process {@linkplain
+ * java.lang.annotation.Repeatable repeatable annotation types},
+ * processors are advised to include both the repeatable annotation
+ * type and its containing annotation type in the set of {@linkplain
+ * #getSupportedAnnotationTypes() supported annotation types} of a
+ * processor.
  *
  * <p>Note that if a processor supports {@code "*"} and returns {@code
  * true}, all annotations are claimed.  Therefore, a universal
@@ -257,10 +288,10 @@
     /**
      * Processes a set of annotation types on type elements
      * originating from the prior round and returns whether or not
-     * these annotations are claimed by this processor.  If {@code
-     * true} is returned, the annotations are claimed and subsequent
+     * these annotation types are claimed by this processor.  If {@code
+     * true} is returned, the annotation types are claimed and subsequent
      * processors will not be asked to process them; if {@code false}
-     * is returned, the annotations are unclaimed and subsequent
+     * is returned, the annotation types are unclaimed and subsequent
      * processors may be asked to process them.  A processor may
      * always return the same boolean value or may vary the result
      * based on chosen criteria.
@@ -271,7 +302,7 @@
      *
      * @param annotations the annotation types requested to be processed
      * @param roundEnv  environment for information about the current and prior round
-     * @return whether or not the set of annotations are claimed by this processor
+     * @return whether or not the set of annotation types are claimed by this processor
      */
     boolean process(Set<? extends TypeElement> annotations,
                     RoundEnvironment roundEnv);
--- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Mon Jul 01 14:57:03 2013 +0100
+++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Mon Jul 01 11:58:45 2013 -0700
@@ -30,11 +30,13 @@
  * @build   JavacTestingAbstractProcessor
  * @compile TestElementsAnnotatedWith.java
  * @compile InheritedAnnotation.java
+ * @compile TpAnno.java
  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
+ * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
  * @compile Foo.java
  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
  */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/processing/environment/round/TpAnno.java	Mon Jul 01 11:58:45 2013 -0700
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.annotation.*;
+import static java.lang.annotation.RetentionPolicy.*;
+
+@Retention(RUNTIME)
+@Target(ElementType.TYPE_PARAMETER)
+public @interface TpAnno {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java	Mon Jul 01 11:58:45 2013 -0700
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * Class to hold annotations for ElementsAnnotatedWithTest.
+ */
+
+@AnnotatedElementInfo(annotationName="TpAnno",
+                      expectedSize=4,
+                      names={"T", "A", "B", "C"})
+public class TypeParameterAnnotations<@TpAnno T>  {
+    private <@TpAnno A> TypeParameterAnnotations(A a) {;}
+
+    public <@TpAnno B> void foo(B b) {return;}
+
+    public static <@TpAnno C> void bar(C b) {return;}
+}