Need CPS/Groovy understanding - problem with setters/getters

174 views
Skip to first unread message

Michael Lasevich

unread,
Sep 28, 2016, 9:00:40 PM9/28/16
to Jenkins Users
Can anyone who understands Groovy/CPS look at this and see if this is even resolvable/fixable?

Ok, so after much hair loss and much unpredictable behavior - I have discovered an ugly bug (feature?) that Groovy getters/setters that work fine under regular Groovy, fail miserably under Pipelines/Jenkins. I suspect this is due to CPS, although I have not yet confirmed that, and wrapping it in @NonCPS tag seems to have not affected the problem....

Consider this simple Groovy demo bean with a custom getter(same thing happens with setters, btw, I just wanted to keep example simple):

class Bean implements Serializable{

 def name = "unset"


  String getName(){

   if (this.name == "unset"){

     this.name = "is not set"

   }

   return this.name

 }

}


and following pipeline code is using it:

import Bean


b = new Bean()

echo("Bean name: "+ b.name)


The code here works fine under plain Groovy (replace "echo" with "println") - but under pipelines it blows up. Looking under the hood it appears that the 'return this.name' literally throws CPS for a loop, it is being replaced with "return this.getName()" which causes infinite recursion.

Knowing this, the workaround seems to be to change the internal field name to not match the getter/setter pattern - but that causes much ugliness (you are forced to now have both getter/setter for common usage and your getter/setter and your field name do not match)

My suspicion is that this is a bug in CPS, but not clearly understanding the purpose/benefits of CPS - I am unclear if this is even fixable. Best I can figure is that CPS injects a lot of headaches in exchange for ability to serialize your workflow at any point in time - a benefit which I am not sure I care much about, considering bulk of the builds happens in sub-processes outside of CPS control... My point is that I clearly do not understand enough of what is going on or why it is happening under Pipelines but not under Groovy in general.  Can someone who understands this  better see if this is a bug or something inherent to Groovy/CPS setup?

Thanks,

-M


Michael Lasevich

unread,
Sep 29, 2016, 12:01:33 AM9/29/16
to Jenkins Users
Well, apparently it is a known issue for about a year, with little activity :-(


-M
Reply all
Reply to author
Forward
0 new messages