Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8244068

Constructor.getParameterAnnotations() ignores annotations with ElementType.FIELD

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      % java -version
      openjdk version "14.0.1" 2020-04-14
      OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
      OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)


      % sw_vers
      ProductName: Mac OS X
      ProductVersion: 10.15.4
      BuildVersion: 19E287

      A DESCRIPTION OF THE PROBLEM :
      Constructor.getAnnotationParameters() being used for a Record completly ignores and does not return annotations with @Target(ElementType.FIELD).


      That lead to potential problems with using a 3rd party libraries, like Jackson. As an example due to this issue record components annotated with @JsonIgnore(https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonIgnore.html) can't be properly ignored.


      REGRESSION : Last worked in version 14ea


      ---------- BEGIN SOURCE ----------
      public class RecordAnnotationsTest {

          @Target(ElementType.PARAMETER)
          @Retention(RetentionPolicy.RUNTIME)
          public @interface A {
          }

          @Target(ElementType.FIELD)
          @Retention(RetentionPolicy.RUNTIME)
          public @interface B {
          }

          public static class Foo {
              public Foo( @A String message ) {
              }
          }

          public record Bar (@A String message) {}

          public record Baz (@B String message) {}

          @Test
          public void ensureThatGetParameterAnnotationsWorksForRecordProperly () {
              Constructor<?>[] csFoo = Foo.class.getConstructors();
              Constructor<?> cFoo = csFoo[0];
              Annotation[][] paFoo = cFoo.getParameterAnnotations();

              Constructor<?>[] csBar = Bar.class.getConstructors();
              Constructor<?> cBar = csBar[0];
              Annotation[][] paBar = cBar.getParameterAnnotations();


              Assert.assertEquals( paFoo[0].length, paBar[0].length);

              Constructor<?>[] csBaz = Baz.class.getConstructors();
              Constructor<?> cBaz = csBaz[0];
              Annotation[][] paBaz = cBaz.getParameterAnnotations();

              Assert.assertEquals("ElementType.FIELD should be considered for records", paBar[0].length, paBaz[0].length);

          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Considering the follwing two facts:

      (1) Record components in the same time are:
          - constructor parameters, in accordance to the declaration;
              - fields, by the nature and observable behavior.
      (2) Since the code from provided test case is legitimate and it does not produce compilation errors, compiler allows usage of @Target(ElementType.FIELD) and @Target(ElementType.PARAMETER) for record components.

       As workaround it should be enough to return annotations with @Target(ElementType.FIELD).


      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              vromero Vicente Arturo Romero Zaldivar
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: