[PATCH] 8140633: Method.getAnnotatedReturnType() returns empty annotations if return type is an array

Joel Borggrén-Franck joel.borggren.franck at gmail.com
Thu Jan 28 19:17:34 UTC 2016


Hi,

Thanks for the patch, but this isn't a bug.

If you look in the Java Virtual Machine Specification 4.7.20.2. The
type_path structure [1] you can see that you haven't annotated the
array but the component type String. You can expand your test slightly
to see this:

public class AnnotationTest {

  @Target({ ElementType.TYPE_PARAMETER, ElementType.TYPE_USE })
  @Retention(RetentionPolicy.RUNTIME)
  public @interface MyAnnotation {
    String value();
  }

  public interface ToImplement {
    public @MyAnnotation("sayHello2") String
@MyAnnotation("sayHello1") [] sayHello2();
  }

  public static void main(String[] args) throws Exception {
    for (Method m : ToImplement.class.getMethods()) {
      AnnotatedType returnType = m.getAnnotatedReturnType();
      System.out.println(Arrays.asList(returnType.getAnnotations()));
      AnnotatedType t2 =
((AnnotatedArrayType)returnType).getAnnotatedGenericComponentType();
      System.out.println(Arrays.asList(t2.getAnnotations()));
      }
    }
  }


I've closed the bug.

[1] https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20.2

cheers
/Joel

On Tue, Jan 12, 2016 at 3:11 PM, Sergey Ustimenko <merkel05 at gmail.com> wrote:
> Hello,
>
> I've recently was working on the bugfix for
> https://bugs.openjdk.java.net/browse/JDK-8140633 and as a result i got that
> small fix. Also I've adopted presented in bugtracker test and done jtreg
> test.
>
> Here is patch. This is my first contribution try, so im probably missing
> something.
>
> # HG changeset patch
> # User fdesu
> # Date 1452597274 -10800
> #      Tue Jan 12 14:14:34 2016 +0300
> # Node ID c7a11642c1a9bb38d558e8b237afce223c586d14
> # Parent  d09282af3b521f751a4b4d5056134877f4cd1b0e
> 8140633: Bugfix for Method.getAnnotatedReturnType()
>
> diff --git
> a/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
> b/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
> ---
> a/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
> +++
> b/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
> @@ -90,7 +90,7 @@
>
>      public static LocationInfo nestingForType(Type type, LocationInfo
> addTo) {
>          if (isArray(type))
> -            return addTo;
> +            return addTo.pushArray();
>          if (type instanceof Class) {
>              Class<?> clz = (Class)type;
>              if (clz.getEnclosingClass() == null)
> diff --git
> a/test/java/lang/reflect/Method/MethodAnnotatedReturnTypeTest.java
> b/test/java/lang/reflect/Method/MethodAnnotatedReturnTypeTest.java
> new file mode 100644
> --- /dev/null
> +++ b/test/java/lang/reflect/Method/MethodAnnotatedReturnTypeTest.java
> @@ -0,0 +1,58 @@
> +/*
> + * Copyright (c) 2012, 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 java.lang.reflect.AnnotatedType;
> +import java.lang.reflect.Method;
> +import java.util.List;
> +
> +/*
> + * @test
> + * @bug 8140633
> + * @summary Ensure that Method.getAnnotatedReturnType() not returns empty
> annotations when
> + * type is an array
> + * @author Sergey Ustimenko
> + */
> +public class MethodAnnotatedReturnTypeTest {
> +
> +    @Target({ ElementType.TYPE_PARAMETER, ElementType.TYPE_USE })
> +    @Retention(RetentionPolicy.RUNTIME)
> +    public @interface MyAnnotation {
> +        String value();
> +    }
> +
> +    public interface ToImplement {
> +        @MyAnnotation("sayHello") String sayHello();
> +        @MyAnnotation("sayHello1") List<String> sayHello1();
> +        @MyAnnotation("sayHello2") String[] sayHello2();
> +    }
> +
> +    public static void main(String[] args) {
> +        for (Method m : ToImplement.class.getMethods()) {
> +            AnnotatedType returnType = m.getAnnotatedReturnType();
> +            if(returnType.getAnnotations().length == 0)
> +                throw new
> RuntimeException("Method.getAnnotatedReturnType() returned an empty
> array!");
> +        }
> +    }
> +
> +}
> \ No newline at end of file



More information about the core-libs-dev mailing list