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

@NamedParam arguments don't get their default value when only named parameters are used in the method call

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.7
    • 2.5.16, 3.0.10, 4.0.0
    • None

    Description

      See the code below.

      @NamedParam only method signatures with default values for not required ones should be possible, with corresponding named parameters only method calls.

      import groovy.transform.NamedParam
      import groovy.transform.NamedVariant
      import static groovy.test.GroovyAssert.shouldFail
          
      class NamedParamTestClass {
      
          Map getArgs(def arg1, def arg2) {
              Map passedArgs = [:]
              passedArgs << ["arg1":arg1, "arg2":arg2]
              passedArgs
          }
           
          @NamedVariant
          Map onlyNamedArgs(@NamedParam(required = true) String arg1, @NamedParam(required = false) String arg2 = "expected default value") {
              getArgs(arg1, arg2)
          }
          
          @NamedVariant
          Map primitiveTypeNotRequired(@NamedParam(required = false) String arg1, @NamedParam(required = false) int arg2 = 1) {
              getArgs(arg1, arg2)
          }
      }
      
      def test = new NamedParamTestClass()
      Map result
      
      result = test.onlyNamedArgs("a positional value for @NamedParam arg1")
      assert result.getAt("arg2") == "expected default value"
      
      result = test.onlyNamedArgs(arg1:"a named parameter value for @NamedParam arg1")
      shouldFail(AssertionError) {
          assert result.getAt("arg2") == "expected default value" :  "arg2 was not given its default value"
      }
      assert result.getAt("arg2") == null
      
      shouldFail(MissingMethodException) {
          result = test.primitiveTypeNotRequired(arg1:'1') // called with named arg
      }
      

      Last test shows also that MissingMethodException occurs when a primitive type is used with @NamedParam.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              math-g Mathieu Grelier
              Votes:
              5 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 10m
                  1h 10m