0

My goal is to set a param for a class, but leave all other params default (aka set a named param).

For this class: https://javadoc.jenkins-ci.org/jenkins/slaves/RemotingWorkDirSettings.html

You can construct it either just with defaults, or passing params.

Both are valid constructors:

new RemotingWorkDirSettings()
new RemotingWorkDirSettings​(boolean disabled, String workDirPath, String internalDir, boolean failIfWorkDirIsMissing)

I want to set the param failIfWorkDirIsMissing to true, but leave everything else default.

THIS WORKS: new RemotingWorkDirSettings(false, 'a_string_here', 'b_string_here', true)

THIS DOES NOT: new RemotingWorkDirSettings(failIfWorkDirIsMissing: true)

Throws the exception:

groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: failIfWorkDirIsMissing

Questions:

  1. How is it "readonly" when I am able to set it when I pass all the params? Since it "can" be set, it isn't truly "read only" in an explicit sense.

  2. How can I set this parameter without having to explicitly set all the other params?**

Rino Bino
  • 366
  • 3
  • 15
  • No way. Look at class methods. There is no setFailIfWorkDirIsMissing method - that's why it is readonly and can be set using constructor with positional parameters. – daggett Oct 25 '22 at 19:30
  • Thanks, that answers my second question. I'm still not clear on my first question.... so in general, with Groovy (and Java?), you cannot ever use positional arguments when constructing classes and you must declare **ALL** arguments? I'm sort of confused on why it would work like that. – Rino Bino Oct 25 '22 at 21:14
  • There is no named parameters support in java and you have to specify all parameters required by constructor/method. Groovy for syntax `new A(b:1, c:2)` will try to find `A` with parameter `Map`, then it will try to use default constructor and set corresponding properties... – daggett Oct 26 '22 at 03:30
  • Thanks so much, I appreciate it. If you want to form these comments into an answer I can accept it. – Rino Bino Oct 26 '22 at 18:23

1 Answers1

1

There is no named parameters support in java and you have to specify all parameters required by constructor/method.

For syntax new A(b:1, c:2) groovy will try to find A with parameter Map, then it will try to use default constructor and set corresponding properties...

Look at class methods - there is no setFailIfWorkDirIsMissing method - that's why it is readonly and can be set using constructor with positional parameters.

daggett
  • 26,404
  • 3
  • 40
  • 56