Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.5.7
-
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
- is related to
-
GROOVY-10176 @NamedVariant with primitives can't find method
- Closed