Mikesname
Mikesname

Reputation: 8901

Getting info on Groovy functions (name, signature, body code)

I have a Groovy file containing a bunch of simple functions like so:

// useful functions
def myFunc1(String arg) {
    println("Hello " + arg)
}

def myFunc2(String arg) {
    println("Goodbye " + arg)
}

I'd like to obtain from this:

(All as simple strings, I don't need to run anything yet.)

I was about to resort to some Regexing, but since I'm using a JVM language (Scala) I figured I might be able to use some of the Groovy compiler's stuff to do this a "nicer" way.

There seems to be a fair bit of information on loading Groovy code dynamically and running it, but not so much on introspecting the source. Any ideas?

(Failing a "nice" way, I'll also accept some Scala-foo to parse the information in a succinct fashion.)

Upvotes: 4

Views: 2021

Answers (1)

tim_yates
tim_yates

Reputation: 171154

This works, and demonstrates the token types required to find each node of importance in the AST. Hope it makes sense... By using lots of Groovy dynamism, I hope I haven't made it too hard for a port to Scala :-(

import org.codehaus.groovy.antlr.*
import org.codehaus.groovy.antlr.parser.*
import static org.codehaus.groovy.antlr.parser.GroovyTokenTypes.*

def code = '''
// useful functions
def myFunc1(String arg) {
    println("Hello " + arg)
}

def myFunc2(arg, int arg2) {
    println("Goodbye " + arg)
}

public String stringify( int a ) {
  "$a"
}
'''

def lines = code.split( '\n' )

// Generate a GroovyRecognizer, compile an AST and assign it to 'ast'
def ast = new SourceBuffer().with { buff ->
  new UnicodeEscapingReader( new StringReader( code ), buff ).with { read ->
    read.lexer = new GroovyLexer( read )
    GroovyRecognizer.make( read.lexer ).with { parser ->
      parser.sourceBuffer = buff
      parser.compilationUnit()
      parser.AST
    }
  }
}

// Walks the ast looking for types
def findByPath( ast, types, multiple=false ) {
  [types.take( 1 )[ 0 ],types.drop(1)].with { head, tail ->
    if( tail ) {
      findByPath( ast*.childrenOfType( head ).flatten(), tail, multiple )
    }
    else {
      ast*.childrenOfType( head ).with { ret ->
        multiple ? ret[ 0 ] : ret.head()[0]
      }
    }
  }
}

// Walk through the returned ast
while( ast ) {
  def methodModifier = findByPath( ast, [ MODIFIERS   ] ).firstChild?.toStringTree() ?: 'public'
  def returnType     = findByPath( ast, [ TYPE, IDENT ] ) ?: 'Object'
  def methodName     = findByPath( ast, [ IDENT       ] )
  def body           = findByPath( ast, [ SLIST ] )
  def parameters     = findByPath( ast, [ PARAMETERS, PARAMETER_DEF ], true ).collect { param ->
    [ type: findByPath( param, [ TYPE ] ).firstChild?.toStringTree() ?: 'Object',
      name: findByPath( param, [ IDENT ] ) ]
  }

  def (y1,y2,x1,x2) = [ body.line - 1, body.lineLast - 1, body.column - 1, body.columnLast ]
  // Grab the text from the original string
  def snip = [  lines[ y1 ].drop( x1 ),                // First line prefix stripped
               *lines[ (y1+1)..<y2 ],                  // Mid lines
                lines[ y2 ].take( x2 ) ].join( '\n' )  // End line suffix stripped

  println '------------------------------'
  println "modifier: $methodModifier"
  println "returns:  $returnType"
  println "name:     $methodName"
  println "params:   $parameters"
  println "$snip\n"

  // Step to next branch and repeat
  ast = ast.nextSibling
}

It prints out:

------------------------------
modifier: public
returns:  Object
name:     myFunc1
params:   [[type:String, name:arg]]
{
    println("Hello " + arg)
}

------------------------------
modifier: public
returns:  Object
name:     myFunc2
params:   [[type:Object, name:arg], [type:int, name:arg2]]
{
    println("Goodbye " + arg)
}

------------------------------
modifier: public
returns:  String
name:     stringify
params:   [[type:int, name:a]]
{
  "$a"
}

Hope it helps, or points you in the right direction :-)

Upvotes: 6

Related Questions