views:

49

answers:

1

How would you go about this reflection task in Groovy:

(1) provide a class type to the Groovy function
(2) loop over all the methods of this class
(a) print out each parameter name and type from the method
(b) print out the return type

+1  A: 

I think the best you can do is to write something like this:

def dumpOut( clz ) {
  clz.metaClass.methods.each { method ->
    println "${method.returnType.name} ${method.name}( ${method.parameterTypes*.name.join( ', ' )} )"
  }
}

dumpOut String.class 

Which will print out:

boolean equals( java.lang.Object )
java.lang.Class getClass(  )
int hashCode(  )
void notify(  )
void notifyAll(  )
java.lang.String toString(  )
void wait(  )
void wait( long )
void wait( long, int )
char charAt( int )
int codePointAt( int )
int codePointBefore( int )
int codePointCount( int, int )
int compareTo( java.lang.Object )
int compareTo( java.lang.String )
int compareToIgnoreCase( java.lang.String )
java.lang.String concat( java.lang.String )
boolean contains( java.lang.CharSequence )
boolean contentEquals( java.lang.CharSequence )
boolean contentEquals( java.lang.StringBuffer )
java.lang.String copyValueOf( [C )
java.lang.String copyValueOf( [C, int, int )
boolean endsWith( java.lang.String )
boolean equals( java.lang.Object )
boolean equalsIgnoreCase( java.lang.String )
java.lang.String format( java.lang.String, [Ljava.lang.Object; )
java.lang.String format( java.util.Locale, java.lang.String, [Ljava.lang.Object; )
[B getBytes(  )
[B getBytes( java.lang.String )
[B getBytes( java.nio.charset.Charset )
void getBytes( int, int, [B, int )
void getChars( int, int, [C, int )
int hashCode(  )
int indexOf( int )
int indexOf( java.lang.String )
int indexOf( int, int )
int indexOf( java.lang.String, int )
java.lang.String intern(  )
boolean isEmpty(  )
int lastIndexOf( int )
int lastIndexOf( java.lang.String )
int lastIndexOf( int, int )
int lastIndexOf( java.lang.String, int )
int length(  )
boolean matches( java.lang.String )
int offsetByCodePoints( int, int )
boolean regionMatches( int, java.lang.String, int, int )
boolean regionMatches( boolean, int, java.lang.String, int, int )
java.lang.String replace( char, char )
java.lang.String replace( java.lang.CharSequence, java.lang.CharSequence )
java.lang.String replaceAll( java.lang.String, java.lang.String )
java.lang.String replaceFirst( java.lang.String, java.lang.String )
[Ljava.lang.String; split( java.lang.String )
[Ljava.lang.String; split( java.lang.String, int )
boolean startsWith( java.lang.String )
boolean startsWith( java.lang.String, int )
java.lang.CharSequence subSequence( int, int )
java.lang.String substring( int )
java.lang.String substring( int, int )
[C toCharArray(  )
java.lang.String toLowerCase(  )
java.lang.String toLowerCase( java.util.Locale )
java.lang.String toString(  )
java.lang.String toUpperCase(  )
java.lang.String toUpperCase( java.util.Locale )
java.lang.String trim(  )
java.lang.String valueOf( [C )
java.lang.String valueOf( boolean )
java.lang.String valueOf( char )
java.lang.String valueOf( double )
java.lang.String valueOf( float )
java.lang.String valueOf( int )
java.lang.String valueOf( java.lang.Object )
java.lang.String valueOf( long )
java.lang.String valueOf( [C, int, int )

I think parameter names are not possible without manipulating the bytecode (and assuming the class was compiled with this sort of debug information intact)

tim_yates