javac8 annotation processors don't visit parameter annotations

Liam Miller-Cushon cushon at google.com
Wed Mar 5 22:06:58 UTC 2014


Here's the (trivial) patch, with a test case that demonstrates the problem.


On Wed, Mar 5, 2014 at 1:49 PM, Liam Miller-Cushon <cushon at google.com>wrote:

> There appears to be a javac8 bug that prevents annotation processors from
> visiting annotations on parameters. When repeated annotations were
> implemented [1][2] the annotation scanners in JavacProcessingEnvironment
> and JavacRoundEnvironment were modified and the following override was
> added:
>
> @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);
> }
>
> MethodSymbols don't have any enclosed elements, but they do have
> parameters (which currently aren't being visited). I think that line was
> meant to match the base implementation in ElementScanner:
>
> return scan(e.getParameters(), p);
>
>
> [1] https://bugs.openjdk.java.net/browse/JDK-7162089
> [2] http://hg.openjdk.java.net/jdk8/jdk8/langtools/rev/1908e86ee49a
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140305/497bd0c7/attachment.html 
-------------- next part --------------
# HG changeset patch
# User cushon <cushon at google.com>
# Date 1394056943 28800
# Node ID ee6cd27af37336f07958ef4463d8827a7b9fe88a
# Parent  927c17e4f6d158e70f43e41dd98f64b8a9e70aeb
Visit parameter annotations during annotation procressing.

diff -r 927c17e4f6d1 -r ee6cd27af373 src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
--- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Feb 24 13:06:07 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Wed Mar 05 14:02:23 2014 -0800
@@ -768,7 +768,7 @@
         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);
+            return scan(e.getParameters(), p);
         }
 
         void addAnnotations(Element e, Set<TypeElement> p) {
diff -r 927c17e4f6d1 -r ee6cd27af373 src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java
--- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Mon Feb 24 13:06:07 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Wed Mar 05 14:02:23 2014 -0800
@@ -144,7 +144,7 @@
         public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) {
             // Type parameters are not considered to be enclosed by an executable
             scan(e.getTypeParameters(), p);
-            return scan(e.getEnclosedElements(), p);
+            return scan(e.getParameters(), p);
         }
 
         @Override
diff -r 927c17e4f6d1 -r ee6cd27af373 test/tools/javac/processing/environment/round/Anno.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/processing/environment/round/Anno.java	Wed Mar 05 14:02:23 2014 -0800
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014, 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.*;
+
+ at Retention(RUNTIME)
+public @interface Anno {}
diff -r 927c17e4f6d1 -r ee6cd27af373 test/tools/javac/processing/environment/round/ParameterAnnotations.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/processing/environment/round/ParameterAnnotations.java	Wed Mar 05 14:02:23 2014 -0800
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014, 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.
+ */
+
+ at AnnotatedElementInfo(annotationName="Anno",
+                      expectedSize=1,
+                      names={"annotatedParameter"})
+public class ParameterAnnotations {
+    private void foo(@Anno Object annotatedParameter) {}
+}
diff -r 927c17e4f6d1 -r ee6cd27af373 test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java
--- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Mon Feb 24 13:06:07 2014 -0800
+++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Wed Mar 05 14:02:23 2014 -0800
@@ -31,12 +31,14 @@
  * @compile TestElementsAnnotatedWith.java
  * @compile InheritedAnnotation.java
  * @compile TpAnno.java
+ * @compile Anno.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 -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java
  * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
  * @compile Foo.java
  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo


More information about the compiler-dev mailing list