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

Compile error using NamedParam with value

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.0
    • Fix Version/s: 2.5.1
    • Component/s: None
    • Labels:
      None

      Description

      NamedVariantASTTransformation has bug checking for presence of value member.

                  for (Parameter fromParam : fromParams) {
                      if (AnnotatedNodeUtils.hasAnnotation(fromParam, NAMED_PARAM_TYPE)) {
                          AnnotationNode namedParam = fromParam.getAnnotations(NAMED_PARAM_TYPE).get(0);
                          boolean required = memberHasValue(namedParam, "required", true);
                          if (getMemberValue(namedParam, "name") == null) {
                              namedParam.addMember("value", constX(fromParam.getName()));
                          }
                          String name = getMemberStringValue(namedParam, "value");
      

      Check here is for name and if not found, fill in value from name of annotated param. There are 2 problems with this:
      1. NamedParam has no name member
      2. NamedParam indicates value as required (no default value provided in declaration)

      Example that produces error:

      import groovy.transform.*
      
      @ToString(includeNames=true)
      class Color {
        Integer r, g, b
      }
      
      @NamedVariant
      String m(@NamedDelegate Color color, @NamedParam(value='a', required=true) int alpha) {
        return [color, alpha].join(' ')
      }
      
      print m(r:1, g:2, b:3, a: 0)
      

      Compiling this script results in this error because the @NamedParam annotation already has a value member when this line from the transform is executed: namedParam.addMember("value", constX(fromParam.getName()));.

      1. ERROR in Script.groovy (at line 1)\r\n
      	import groovy.transform.*\r\n
      	^\n
      Groovy:Groovy compiler error: exception in phase 'semantic analysis' in source unit 'Script.groovy' Annotation member value has already been added @ line 1, column 0.
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                emilles Eric Milles
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: