Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10635

Method references not working for record components in dynamic code

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.3
    • None
    • None

    Description

      The following example currently fails.

      record Bar(String name) { }
      
      def bars = [new Bar(name: 'A'), new Bar(name: 'B')]
      assert bars.stream().map(Bar::name).toList() == ['A', 'B']
      

      The error message is:

      java.lang.IllegalArgumentException: Can not set final java.lang.String field Bar.name to java.lang.Class
      

      This is because the metaclass is picking the private field as a property ahead of the same-named accessor method.

      Workaround is to use @CompileStatic.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              paulk Paul King
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: