No such field found: field java.lang.String sinput error when accessing cppcheck plugin classes

Viewed 1050

]I am a junior dev trying to lear about Jenkins, I have been learning on my own for a couple of months. Currently I have a pipeline (just for learning purposes) which runs static analysis on a folder, and then publish it, I have been able to send a report through email using jelly templates, from there I realized it is posbile to instantiate the classes of a plugin to use its methods so I went to the cppcheck javadoc and did some trial and error so I can get some values of my report and then do something else with them, so I had something like this in my pipeline:

pipeline {
    agent any

    stages {
        stage('analysis') {
            steps {
                script{
                    bat'cppcheck "E:/My_project/Source/"  --xml --xml-version=2 . 2> cppcheck.xml'
                }

            }
        }
        
        stage('Test'){
            steps {
                script {
                    publishCppcheck pattern:'cppcheck.xml'
                    
                    for (action in currentBuild.rawBuild.getActions()) {
                        def name = action.getClass().getName()
                        if (name == 'org.jenkinsci.plugins.cppcheck.CppcheckBuildAction') {
                            def cppcheckaction = action
                            def totalErrors = cppcheckaction.getResult().report.getNumberTotal()
                            println totalErrors
                            def warnings = cppcheckaction.getResult().statistics.getNumberWarningSeverity()
                            println warnings
                        }
                    }
                }   
            }
        }
   
    }

}

which output is:

[Pipeline] echo
102
[Pipeline] echo
4

My logic (wrongly) tells me that if I can access to the report and statistics classes like that and uses their methods getNumberTotal() and getNumberWarningSeverity() respectively, therefore I should be able to also access the DiffState class in the same way and use the valueOf() method to get an enum of the new errors. But adding this to my pipeline:

def nueva = cppcheckaction.getResult().diffState.valueOf(NEW)
println nueva

Gives me an error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field org.jenkinsci.plugins.cppcheck.CppcheckBuildAction diffState
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:409)
...

I can see in the javadoc there is a diffState class with a valueOf() method, but I cannot access to it is therre any other way to get the new errors between the last build and the current one? enter image description here

1 Answers

I see 2 issues that could be causing this:

  1. CppcheckResult doesn't have a member variable diffState so you can't access it obviously

If you check the javadoc of CppcheckResult the class does have:

private CppcheckReport report;
public CppcheckStatistics getReport()

and

private CppcheckStatistics statistics;
public CppcheckStatistics getStatistics()

there is no member (and getter method) for diffState so maybe try to call:

/**
 * Get differences between current and previous statistics.
 * 
 * @return the differences
 */
public CppcheckStatistics getDiff(){

my suggestion: cppcheckaction.getResult().getDiff().valueOf(NEW). Furthermore CppcheckWorkspaceFile does have a method getDiffState().


  1. Please have a look at the script approval of your Jenkins (see here).

The syntax error might appear because Jenkins (Groovy Sandbox) blocks the execution of an (for the Jenkins) "unknown" and potential dangerous method.

Jenkins settings - Script Approval - Approve your blocked method

Related