Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScripRunner: Static Type Checking fails

Frank Mathis July 9, 2019

Hello guys,

I try to clone an issue and remove a specific label from it (named CCB). Following code is working fine (this is just a snippet as in the original code I will really create the cloned issue)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.label.Label

// get Issue
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("SWCM-46")

// Clone Issue
def issueFactory = ComponentAccessor.getIssueFactory()
def newIssue = issueFactory.cloneIssue(issue)

// get Labels from Issue
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
List <String> labels = labelManager.getLabels(issue.id).collect{ it.getLabel() }

labels -= 'CCB'

newIssue.setLabels(labels.toSet())

return null

 Now my problem concerns static type checking:

09-07-_2019_10-13-57.png

As I can see labels is a collection and is converted to a Set by calling toSet() - so everything should fine as the argument of method setLabels() must be a Set according to documentation: http://docs.groovy-lang.org/2.4.15/html/groovy-jdk/java/util/Collection.html#toSet()

So why is type checking failing here?

 

Kind Regards

Frank

 

1 answer

0 votes
Ben Poulson
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.
July 10, 2019

I believe this is a quirk of the scriptrunner linter. It treats any object declared as a def as a standard object (of class java.lang.object). When you declare newIssue on this line

def newIssue = issueFactory.cloneIssue(issue)

the linter always treats newIssue as a generic object, regardless of the actual type (which is why the script works despite this- from a programming perspective your code is correct, its just a problem with the console). If you want the warning to go away, you should just be able to declare it as an IssueImpl (since issueImpl implements the MutableIssue interface), you just need to add this import:

import com.atlassian.jira.issue.IssueImpl

Personally, this bug (amongst other problems with the console) is why I don't write my scripts in jira itself- I use a 3rd party text editor (you can use any you want, but my preference is Sublime Text) to actually write the scripts, and then just copy and paste them into jira when I'm ready to test them.

Frank Mathis July 10, 2019

Hi Ben

Thanks for your answer I definitely will try out Sublime Text :-)

I tried using IssueImpl:

import com.atlassian.jira.issue.IssueImpl

IssueImpl newIssue = issueFactory.cloneIssue(issue)

This leads to warning:

11-07-_2019_07-02-04.png

Meanwhile I got rid of the warning by using as operator for argument of setLabels(...):

newIssue.setLabels(labels.toSet() as Set)

 

Kind Regards

Frank

Ben Poulson
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.
July 12, 2019

Not sure if you still have the first, warning, but if you do, you should be able to fix it by casting the cloned issue to issueimpl

 

IssueImpl newIssue = (IssueImpl)issueFactory.cloneIssue(issue)
Frank Mathis July 14, 2019

That helps for getting rid of warning about mismatch between MutableIssue and IssueImp but the first warning still appears...

Suggest an answer

Log in or Sign up to answer