如何使用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调查结果

tim_yates

代替:

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

您只需要声明的非合成方法:

def methods = MyClass.declaredMethods.findAll { !it.synthetic }.name

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Grails [Groovy],如何获取类继承的所有方法的清单?

如何获得带有特定注释,方法名称和包/类名称的所有方法的javadoc?

没有方法名称的方法调用本身?

没有方法名称的Ruby调用

带有方法的Javascript对象返回“没有方法'我的方法名称'”错误

如何不使用dir()获得实例/类的所有属性和方法名称?

从python目录中获取所有模块名称,类名称和方法名称

如何获取脚本中使用的所有变量和方法名称

为什么所有方法都不从类继承?

根据在codeigniter中选择的控制器名称显示所有方法名称

如何使用给定的装饰器获取python类的所有方法

没有方法的类是否有特殊名称?

如何使用dnlib库获取所有方法的说明?

获取所有服务合同方法名称

如何在Ruby中显示没有祖先方法的类的所有方法?

获取所有方法

接受所有方法名称并打印出来的对象

获取类的所有方法(包括继承的Java 8默认方法)的新方法是什么?

在Ruby中使用具有方法名称的字符串调用方法

如何列出一个类的所有字段(没有方法)?

Groovy如何访问Java类的私有方法?

Groovy 脚本:没有方法签名

获取类中所有方法的属性列表

如何获取在类的方法中定义的所有变量的名称

如何使用proguard混淆除公共方法名称和属性以外的所有内容?

Kotlin继承-扩展具有相同方法名称的JVM类和接口

如何导出程序集中所有方法的名称?

如何获得Scala特征中所有方法的名称

我想使用ANTLR4从Java源文件中提取所有方法名称和变量名称