defining generic classes in scriptrunner
Asked by Daniel Garcia:

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?


Answers:
author: Reece Lander _ScriptRunner - The Adaptavist Group_
body: 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.

comments: author: Reece Lander _ScriptRunner - The Adaptavist Group_
body: 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.


author: Daniel Garcia
body: 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.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Daniel Garcia](/t5/user/viewprofilepage/user-id/1845595)

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

comments: author: Daniel Garcia
body: Generic classes are those that take other classes as parameters.


author: Radek Dost?l
body: 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.

comments: author: Reece Lander _ScriptRunner - The Adaptavist Group_
body: 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.


author: Daniel Garcia
body: 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?


Tags: addon-com.onresolve.jira.groovy.groovyrunner

data-center

jira-data-center

scriptrunner