开发者

Groovy - reflection on a Java class - methods and parameters

开发者 https://www.devze.com 2023-01-17 14:21 出处:网络
How would you go about this reflection task in Groovy: (1) provide a class type to the Groovy funct开发者_如何学Goion

How would you go about this reflection task in Groovy:

(1) provide a class type to the Groovy funct开发者_如何学Goion

(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


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)


Maybe this got easier since the question was originally answered.

someThing.metaClass.methods.each {println it}

For example:

groovy:000> Object.metaClass.methods.each {println it}
public boolean java.lang.Object.equals(java.lang.Object)
public final native java.lang.Class java.lang.Object.getClass()
public native int java.lang.Object.hashCode()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public java.lang.String java.lang.Object.toString()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
===> [public boolean java.lang.Object.equals(java.lang.Object), public final native java.lang.Class java.lang.Object.getClass(), public native int java.lang.Object.hashCode(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll(), public java.lang.String java.lang.Object.toString(), public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException]
0

精彩评论

暂无评论...
验证码 换一张
取 消