reflection - 如何使用 Groovy 获取没有继承方法的类的所有方法名称?

标签 reflection groovy

问题

如果没有继承的方法,如何获得类的所有方法的名称?

示例

def methods = MyClass.methods.collect { it.name }
println methods.each { println it }
assert ["method1_static_void", "method2_static_String", "method3_void", "method4_String"].sort() == methods.sort()


class MyClass {
    public static void method1_static_void() {}
    public static String method2_static_String() {}
    public void method3_void() {}
    private String method4_String() {}
}

预期输出
method1_static_void
method2_static_String
method3_void
method4_String

实际输出
setProperty
getProperty
super$1$wait
super$1$wait
super$1$wait
super$1$clone
getMetaClass
invokeMethod
setMetaClass
__$swapInit
method3_void
method1_static_void
method2_static_String
this$2$method4_String
this$dist$invoke$1
this$dist$set$1
this$dist$get$1
super$1$toString
super$1$notify
super$1$notifyAll
super$1$getClass
super$1$equals
super$1$hashCode
super$1$finalize
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
[setProperty, getProperty, super$1$wait, super$1$wait, super$1$wait, super$1$clone, getMetaClass, invokeMethod, setMetaClass, __$swapInit, method3_void, method1_static_void, method2_static_String, this$2$method4_String, this$dist$invoke$1, this$dist$set$1, this$dist$get$1, super$1$toString, super$1$notify, super$1$notifyAll, super$1$getClass, super$1$equals, super$1$hashCode, super$1$finalize, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll]
Assertion failed: 

assert ["method1_static_void", "method2_static_String", "method3_void", "method4_String"] == methods
                                                                                          |  |
                                                                                          |  [setProperty, getProperty, super$1$wait, super$1$wait, super$1$wait, super$1$clone, getMetaClass, invokeMethod, setMetaClass, __$swapInit, method3_void, method1_static_void, method2_static_String, this$2$method4_String, this$dist$invoke$1, this$dist$set$1, this$dist$get$1, super$1$toString, super$1$notify, super$1$notifyAll, super$1$getClass, super$1$equals, super$1$hashCode, super$1$finalize, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll]
                                                                                          false

Google 调查结果
  • List the methods of a groovy class - Groovy 年鉴
  • Discovering Class Members - docs.oracle.com
  • 最佳答案

    代替:

    def methods = MyClass.methods.collect { it.name }
    

    您只需要声明的非合成方法:
    def methods = MyClass.declaredMethods.findAll { !it.synthetic }.name
    

    关于reflection - 如何使用 Groovy 获取没有继承方法的类的所有方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22065290/

    相关文章:

    java - 抽象返回对象列表的方法

    objective-c - 如何获得协议(protocol)定义的函数列表?

    Jenkins Workflow 构建新工作

    grails - 在 grails 插件中将 mime 映射添加到 web.xml 的正确语法

    Groovy 闭包、def 与类型化返回值

    c# - 尝试在动态创建的程序集上绑定(bind)动态方法会导致 RuntimeBinderException

    java - 通过反射修改不可变的scala类字段

    Groovy 相当于 Scala 特征可堆叠修改吗?

    Jenkins 无法识别命令 sh?

    .net - 反射,从好友类获取DataAnnotation属性