Groovy – Java类的反射 – 方法和参数

Groovy – Java类的反射 – 方法和参数,第1张

概述你将如何在Groovy中进行这个反射任务: (1)为Groovy函数提供类类型 (2)循环遍历此类的所有方法         (a)从方法中打印出每个参数名称和类型         (b)打印出退货类型 我认为你能做的最好的事情是写下这样的东西: def dumpOut( clz ) { clz.metaClass.methods.each { method -> println "$ 你将如何在Groovy中进行这个反射任务:

(1)为Groovy函数提供类类型
(2)循环遍历此类的所有方法
        (a)从方法中打印出每个参数名称和类型
        (b)打印出退货类型

解决方法 我认为你能做的最好的事情是写下这样的东西:

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

哪个会打印出来:

boolean equals( @[email protected] )java.lang.class getClass(  )int hashCode(  )voID notify(  )voID notifyAll(  )@[email protected] 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( @[email protected] )int compareto( @[email protected] )int comparetoIgnoreCase( @[email protected] )@[email protected] concat( @[email protected] )boolean contains( @[email protected] )boolean contentEquals( @[email protected] )boolean contentEquals( @[email protected] )@[email protected] copyValueOf( [C )@[email protected] copyValueOf( [C,int,int )boolean endsWith( @[email protected] )boolean equals( @[email protected] )boolean equalsIgnoreCase( @[email protected] )@[email protected] format( @[email protected],[L@[email protected]; )@[email protected] format( java.util.Locale,@[email protected],[L@[email protected]; )[B getBytes(  )[B getBytes( @[email protected] )[B getBytes( java.nio.charset.Charset )voID getBytes( int,[B,int )voID getChars( int,[C,int )int hashCode(  )int indexOf( int )int indexOf( @[email protected] )int indexOf( int,int )int indexOf( @[email protected],int )@[email protected] intern(  )boolean isEmpty(  )int lastIndexOf( int )int lastIndexOf( @[email protected] )int lastIndexOf( int,int )int lastIndexOf( @[email protected],int )int length(  )boolean matches( @[email protected] )int offsetByCodePoints( int,int )boolean regionMatches( int,int )boolean regionMatches( boolean,int )@[email protected] replace( char,char )@[email protected] replace( @[email protected],@[email protected] )@[email protected] replaceAll( @[email protected],@[email protected] )@[email protected] replaceFirst( @[email protected],@[email protected] )[L@[email protected]; split( @[email protected] )[L@[email protected]; split( @[email protected],int )boolean startsWith( @[email protected] )boolean startsWith( @[email protected],int )@[email protected] subSequence( int,int )@[email protected] substring( int )@[email protected] substring( int,int )[C tochararray(  )@[email protected] tolowerCase(  )@[email protected] tolowerCase( java.util.Locale )@[email protected] toString(  )@[email protected] toupperCase(  )@[email protected] toupperCase( java.util.Locale )@[email protected] trim(  )@[email protected] valueOf( [C )@[email protected] valueOf( boolean )@[email protected] valueOf( char )@[email protected] valueOf( double )@[email protected] valueOf( float )@[email protected] valueOf( int )@[email protected] valueOf( @[email protected] )@[email protected] valueOf( long )@[email protected] valueOf( [C,int )

我认为在不 *** 作字节码的情况下参数名称是不可能的(假设该类是使用这种调试信息完整编译的)

总结

以上是内存溢出为你收集整理的Groovy – Java类的反射 – 方法和参数全部内容,希望文章能够帮你解决Groovy – Java类的反射 – 方法和参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1228173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存