4

I have an annotation which adds some methods and default constructor to annotated class. I have managed to create a gdsl, to enable autocompletion in idea for methods, but I'm stuck with constructor and documentation is very poor.

Does anyone have any ideas, how to do this?

Maybe I could find a solution, in existing gdsl, but I can't remember any Transformation, related to constructors. Maybe you can remind me of any of them.

def objectContext = context(ctype: "java.lang.Object")

contributor(objectContext) {
  if (hasAnnotation("com.xseagullx.SomeAnnotation")) {
    // Here I want to add constructor's declaration(with empty arg's)
    // …
    // And then my methods.
    method name: 'someMethod', type: 'void', params: [:]
  }
}

EDITED: OK, if it's as @jasp say, and there is no DSL construct for declaring Constructors, I'm still asking for a good documentation sources, other than JB's confluence page. Tutorials and other sources. I'm familiar with embedded dsl's for groovy, grails and gradle.

Need smth. more structured, if it's possible.

Seagull
  • 13,484
  • 2
  • 33
  • 45

2 Answers2

3

All function invocations inside of GroovyDSL are just calls to wrappers around internal IDEA's Program Structure Interface (PCI). However it doesn't cover all of PCI's abilities, including default constructors functionality I believe. One of an evidence for that is singletonTransform.gdsl, which is bundled into IDEA from 9 version and describes @Singleton AST transformation. Here is it's code:

contributor(context()) {
  if (classType?.hasAnnotation("groovy.lang.Singleton")) {
    property name: "instance",
             type: classType?.getQualifiedName() ?: "java.lang.Object",
             isStatic: true
  }
}

As you can see it doesn't change a constructor and it's visibility, so IDEA will autocomplete this invalid code:

@Singleton class Foo {}
def foo = new Foo()

Futhermore GDSL that describes the semantics of GroovyDSL (which is actually the part of /plugins/groovy/resources/standardDsls/metaDsl.gdsl of IDEA sources) doesn't provide any ability for describing of constructors.

In this case I suggest you use newify transformation which allows you to describe targetClass.name method returning created instance.

jasp
  • 291
  • 1
  • 7
1

I know this is a bit old, but I found myself looking for something similar.

The DSL you are looking for is method params: [:], constructor: true although I don't understand why you'd need it; if a class doesn't declare any constructors doesn't IDEA always suggest the default one?

Poundex
  • 410
  • 4
  • 5
  • Yes, if the class _doesn't declare any constructors_… But not in my case, as class can have constructors, and default one is added by annotation. Anyway, thanks for the answer! It's good, that this thing is possible. – Seagull Feb 09 '15 at 00:19
  • 1
    Ah, I see - fair enough. As for documentation, the answer seems to be that there isn't any (http://stackoverflow.com/questions/3584883/where-is-the-jetbrains-intellij-openapi-documentation) but I've found that the OpenAPI library itself (http://www.mvnrepository.com/artifact/com.intellij/openapi/7.0.3) is usually good enough (if you have this as a dependency in your project, you'll also get pretty decent code completion in your GDSL files). – Poundex Feb 09 '15 at 12:18