Jenkins groovy pipeline简单继承故障

4
我正在尝试在 Jenkins 流水线的 Groovy 脚本中的子类中调用父类的受保护方法。Jenkins 崩溃并显示:"groovy.lang.MissingPropertyException: No such property: _parentValue for class: Child"。
但是,如果我在 Intellij IDEA 中运行完全相同的代码,它可以正常工作。我不知道为什么在 Jenkins 中无法工作。有人可以帮忙吗?
代码:
public class Parent
{
    private int _parentValue

    public Parent()
    {
        _parentValue = 0
    }

    protected void Increment()
    {
        _parentValue = _parentValue + 1
    }
}

public class Child extends Parent
{
    public void IncrementFromChild()
    {
        // call parent method
        Increment()
    }
}

// Instantiate and call child method
def child = new Child()
child.IncrementFromChild()

堆栈跟踪:
groovy.lang.MissingPropertyException: No such property: _parentValue for  class: Child
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at Parent.Increment(WorkflowScript:12)
at Child.IncrementFromChild(WorkflowScript:20)
at WorkflowScript.run(WorkflowScript:25)
...
1个回答

2
似乎 CPS 转换会破坏共享库中的继承关系,这确实令人非常遗憾。请参阅 Andrew Bayer 的最后一条评论这里
我不确定,但 @NonCPS 注释可能在某些情况下有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接