defining generic classes in scriptrunner

Daniel Garcia August 6, 2024

Does anyone define generic classes in scriptrunner? I remember having problems years ago and was so traumatised by the experience, I haven't used it since. Should I be brave enough to try again?

4 answers

1 accepted

0 votes
Answer accepted
Reece Lander _ScriptRunner - The Adaptavist Group_
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.
August 7, 2024

If you mean generics as in generic types: https://groovy-lang.org/objectorientation.html#generics

I think Groovy in the past had some issues where type checking would get confused with some generics. This works reasonably well now I think, we have varying amounts of generic code in the ScriptRunner codebase (which is written in Groovy).

I think since Groovy 3 (ScriptRunner 7) things should work much better. In the past there were workarounds like making a method explicitly `public` if it was generic.

Reece Lander _ScriptRunner - The Adaptavist Group_
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.
August 7, 2024

When working with generics sometimes it can be a good idea to use `@CompileStatic` which will fail early if Groovy cannot type check properly.

You'd only use generics if you were trying to write typesafe code anyway.

Like Daniel Garcia likes this
0 votes
Daniel Garcia August 7, 2024

A generic class is one that takes another class as a parameter. e.g. java.util.List is a generic interface because you can pass the item type as a parameter.

In the past I've had runaway compilation on production servers when defining generics in scriptrunner. By runaway compilation I mean scriprunner starts to recompile consuming plenty of CPU and load continues to build to 100% and never finishes. The only solution is to restart the jira service. If I recall correctly it was creating multiple versions of the same class then recompiling them again due to infinite loop or recursion.

I think that bug was fixed but a few releases later another bug appeared with slightly different behaviour. After a few iterations of that I stopped creating generics in groovy.

I can use generics from dependencies without a problem. List<String>, Map<Integer,String>, AbstractOrderableField<String> etc. 

 

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 7, 2024

Hi @Daniel Garcia

When you mention Generic Classes, I am assuming (please correct me if I am wrong) an Abstract class where you want to add multiple re-usable methods so they can be invoked Polymorphically or via Inheritance.

If so, you can do it, but when it comes to troubleshooting, it can be a little tedious to find out why it's not working.

It's better to have each method separately in each code so it's easier to identify what is causing the error.

Thank you and Kind regards,
Ram

Daniel Garcia August 7, 2024

Generic classes are those that take other classes as parameters.

0 votes
Radek Dostál
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.
August 7, 2024

Not sure what you mean by "generic classes" and why you would need anything like that, can't say as to how much it takes to traumatise you, but all I can say is that groovy is just a thing on top of java and what can be done in java can generally be done in groovy and vice versa.

Reece Lander _ScriptRunner - The Adaptavist Group_
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.
August 7, 2024

This is generally true but there are exceptions, historically the parser in Groovy was outdated and didn't fully support the syntax of Java in all cases (including getting confused by generics)

Since Groovy 3 (ScriptRunner 7.0.0) the Parrot parser is in use by default and should handle all Java syntax as one would expect.

Daniel Garcia August 7, 2024

Yes, this is why I'm asking. These issues happened quite a while ago so I'm curious to how stable it is now. Are people using generics with success?

Suggest an answer

Log in or Sign up to answer