Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy convert String to Long

Tags:

jenkins

groovy

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.

like image 593
Franck Cussac Avatar asked Dec 15 '17 16:12

Franck Cussac


3 Answers

you should use the Long class:

def min = Long.valueOf('1')
like image 179
vmartin Avatar answered Oct 17 '22 00:10

vmartin


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.

like image 45
Franck Cussac Avatar answered Oct 17 '22 01:10

Franck Cussac


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.

like image 6
Nitin Dhomse Avatar answered Oct 17 '22 00:10

Nitin Dhomse