• Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None

      Trying to make use of extension methods but haven't managed to get it work.
      In example, outside of pipeline this fails:

      String.metaClass.toTitleCase = { -> toLowerCase().tokenize(" ")*.capitalize().join(" ") }

      returning "spread not yet supported", but below code in post stage of pipeline (making html email) works fine:

      entry.User = "${fields[1]}".replace(".", " ").trim().tokenize(" ")*.capitalize().join(" ")

      which may mean that spread actually works.
       
      replacing extension method with: 

      String.metaClass.toTitleCase = {
        def words = phrase.toLowerCase().tokenize(" ")
        words.collect{ -> capitalize() }
        String result = words.join(" ")
        return result;
      }

      gives other error: "No signature of method: java.lang.String.toTitleCase() is applicable for argument types: () values: []"

      I think there is some error around handling extension methods here.

      abayer could you check if its not like the bug https://issues.jenkins-ci.org/browse/JENKINS-47193 where classes outside pipeline were not being parsed? 

       

       
       

          [JENKINS-47450] Extension methods do not work in Pipeline

          It looks like spread not yet supported happens when method is not @NonCPS, with this flag it works fine.

          Unfortunately it looks like @NonCPS cannot be used with extension methods.
           

          Jakub Pawlinski added a comment - It looks like spread not yet supported  happens when method is not @NonCPS, with this flag it works fine. Unfortunately it looks like @NonCPS cannot be used with extension methods.  

          Andrew Bayer added a comment -

          I don't think it's possible to do metaclass method magic in either Pipeline code or any other Groovy code subject to Jenkins script security, fwiw.

          Andrew Bayer added a comment - I don't think it's possible to do metaclass method magic in either Pipeline code or any other Groovy code subject to Jenkins script security, fwiw.

          I understand, guess even using shared library (https://wiki.jenkins.io/display/JENKINS/Pipeline+Shared+Groovy+Libraries+Plugin) will be not sufficient?

           
           

          Jakub Pawlinski added a comment - I understand, guess even using shared library ( https://wiki.jenkins.io/display/JENKINS/Pipeline+Shared+Groovy+Libraries+Plugin ) will be not sufficient?    

            Unassigned Unassigned
            quas Jakub Pawlinski
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: