Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Static type checking error - No such property: value for class: java.lang.object

Peter Brodt May 14, 2021

I have got this little piece of code and can't get rid of the above error. The script runs all right. But the error message is irritating. Any help is welcome.

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

def customFieldManager = ComponentAccessor.customFieldManager

def bvField = customFieldManager.getCustomFieldObjectsByName("WSJF-Business Value")[0]
def bvValue = issue.getCustomFieldValue(bvField)?.value as Long ?: 0L

def tcField = customFieldManager.getCustomFieldObjectsByName("WSJF-Kundennutzen")[0]
def tcValue = issue.getCustomFieldValue(tcField)?.value as Long ?: 0L

def rrField = customFieldManager.getCustomFieldObjectsByName("WSJF-Dringlichkeit")[0]
def rrValue = issue.getCustomFieldValue(rrField)?.value as Long ?: 0L

def jsField = customFieldManager.getCustomFieldObjectsByName("WSJF-Komplexität")[0]
def jsValue = issue.getCustomFieldValue(jsField)?.value as Long ?: 1L

def wsjf = (rrValue + bvValue + tcValue) / jsValue

def wsjfRounded = Math.round(wsjf * 100) / 100

return wsjfRounded

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 14, 2021

Nic is 100% correct of course.

And I used to not care so much ... but there are places where it becomes annoying to get all those static errors and it's nice to make them go away and it's very satisfying to get that green dot under your script.

So in your case, since you call ?.value on your custom field value, I'll assume that all your fields are based on some form of option (single select for example).

Fields that store values as options are returned as instances of LazyLoadedOption when you can getCustomFieldValue(). And the label of that option is accessed with getValue() or .value.

If that's not the case and those fields are number or text fields, then remove the ?.value and see if that works.

But otherwise, this version of your script is static-type-checking-error-free for me (not tested since I don't actually have those fields):

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger

def customFieldManager = ComponentAccessor.customFieldManager

def bvField = customFieldManager.getCustomFieldObjectsByName("WSJF-Business Value")[0]
def bvValue = (issue.getCustomFieldValue(bvField) as LazyLoadedOption)?.value as Long ?: 0L

def tcField = customFieldManager.getCustomFieldObjectsByName("WSJF-Kundennutzen")[0]
def tcValue = (issue.getCustomFieldValue(tcField) as LazyLoadedOption)?.value as Long ?: 0L

def rrField = customFieldManager.getCustomFieldObjectsByName("WSJF-Dringlichkeit")[0]
def rrValue = (issue.getCustomFieldValue(rrField)as LazyLoadedOption)?.value as Long ?: 0L

def jsField = customFieldManager.getCustomFieldObjectsByName("WSJF-Komplexität")[0]
def jsValue = (issue.getCustomFieldValue(jsField)as LazyLoadedOption)?.value as Long ?: 1L

def wsjf = ((rrValue + bvValue + tcValue) / jsValue) as Double

def wsjfRounded = Math.round(wsjf * 100) / 100

return wsjfRounded
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 14, 2021

Yep, those red dots always make my codey-sense tingle.  Even when I know it's fine and it's always going to work, and my coding is a long way off the best in the world, there's a satisfaction in delivering a script without any :-)

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 14, 2021

This error is really more of a warning about code than it is an error.  I think of it as saying "Oi, developer, your code here allows room for errors that may halt your code, and I, as a dumb compiler, can't understand whether it might or might not"

One reason we need warnings like this is the use of weakly typed languages like groovy over strongly typed ones like Java, and this is what you're running into in this case.

So, somewhere before the line which is showing you the warning, you are creating a variable that will contain an object.

On the line where you're using that variable, the function you are using needs to be given objects of a very specific type.  But when you give it a weakly typed variable, the compiler can not be sure that the input is going to be of the right type.

My guess would be the calculations in your script - arithmetic functions like +, -, / and * obviously expect numbers.  But, the compiler does not know that the value it gets back from "getCustomFieldValue" is going to be a number!  getCustomFieldValue might return a date object, a string, a (jira) option or a couple of other things.

So, you can get rid of the warning by using strict typing - instead of "def jsValue = getcustomfield value" use "long jsValue = getcustomfieldvalue" - that will fix the compiler warning, BUT it also assumes that your customfields are going to contain the right type of data, and your script will fail horribly if they're not (rather than moderately gracefully if you stick with "def")

TAGS
AUG Leaders

Atlassian Community Events