Diego
Diego

Reputation: 4433

Telling if a method was declared using "a string"

For DSL purposes I want to detect methods defined like:

  def "methodName"() {}

or

  def "This is another method name"() {}

Is there any way to do that using reflection?

Upvotes: 4

Views: 125

Answers (1)

melix
melix

Reputation: 1530

This information is not available at runtime. Even with an AST transformation, you would not be able to figure out that the method was defined using a string.

For this, you would have to write your own AntlrParserPlugin and use it using a custom CompilerConfiguration. Then in methodDef from AntlrParserPlugin, when the name of the method is parsed, you could check that the character before the name is a double quote. If a double quote is found, then add a custom annotation to the generated MethodNode, so that the information is available at runtime...

Upvotes: 1

Related Questions