6

I'm coding a jenkins pipeline and I need to convert a String parameter in a Long value.

I have used Long.valueOf, Long.parseLong. I get this error message :

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Class.parseLong() is applicable for argument types: (java.lang.String) values: [8899986991733205013]

or this :

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Class.valueOf() is applicable for argument types: (java.lang.String) values: [8899986991733205013]

my code :

    def min = Long.valueOf(params.paymentid) + Integer.valueOf(params.begin)
    def max = Long.valueOf(params.paymentid) + Integer.valueOf(params.end)

Any idea ? Thanks.

Franck Cussac
  • 310
  • 1
  • 3
  • 14
  • 1
    Long.parseLong(String ) expects a string argument. What is the type of params.begin, params.end and params.paymentid -- all String ?? – CodeMonkey Dec 15 '17 at 17:48
  • 1
    You have `Integer` instead of `Long` in your sample code, but in your question you refer to "Long.valueOf" and "Long.parseLong"... – bdkosher Dec 15 '17 at 18:06
  • Yes i tried different things and I pasted one of my try, but Long.valuOf tells me this : org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod java.lang.Long valueOf java.lang.String – Franck Cussac Dec 18 '17 at 09:25
  • @JasonM1 param.begin, end and paymentid are all String – Franck Cussac Dec 18 '17 at 09:56

3 Answers3

14

you should use the Long class:

def min = Long.valueOf('1')
vmartin
  • 493
  • 4
  • 15
  • 1
    Sorry my paste is bad, but I tried it and the error was : org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod java.lang.Long valueOf java.lang.String – Franck Cussac Dec 18 '17 at 09:55
8

The following conversion will also work.

Long paymentId = params.paymentid as Long
println paymentId

You can put this block inside try..catch block to handle any type casting exception if the string is invalid.

Nitin Dhomse
  • 2,524
  • 1
  • 12
  • 24
5

I tried this solution in admin console :

print(params.paymendid.toLong())

it worked. On my pipeline it didn't work, i got this error :

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods toLong java.lang.String

but this solution : https://stackoverflow.com/a/39412951/8357778 works.

I should disable sandbox.

Franck Cussac
  • 310
  • 1
  • 3
  • 14