IntelliJ GroovyDSL for static method

349 views Asked by At

I'm using GroovyDSL for IntelliJ, and I'd like to describe a static method, that returns instance of same class. It's a method like:

MyEntity x = MyEntity.get(1)

As I understand, I should use context with ctype for java.lang.Class. But I don't know how to specify return type, currently i'm specifying it just as a java.lang.Object:

def domainCtx = context(
        ctype: 'java.lang.Class'
)
contributor([domainCtx]) {
    method name: 'get',
           params: [id: 'long'],
           type: 'java.lang.Object'
}

Question: How I can set type as a actual classname? not 'Object', but 'MyEntity'

PS is there any documentation about GroovyDSL, a JavaDoc describing contributor?

1

There are 1 answers

0
Max Medvedev On BEST ANSWER

use something like this

private String extractParameter(def type) {
  def parameters = type.parameters
  if (!parameters || parameters.length != 1) return 'java.lang.Object'
  return parameters[0].canonicalText
}

contributor(ctype:'java.lang.Class') {
    method(type:extractParameter(psiType), name: 'create')
}

'psiType' property has 'com.intellij.psi.PsiClassType' type in your case. It has 'getParameters()' method which returns generic parameters or the type. 'getCanonicalText()' return canonical presentation of type (qualified class name with generics).

In some cases java.lang.Class can have '? extends MyEntity' or even '?' parameter. So you can add some code which handles these cases.