Question
How can you get the names of all the methods of a class without the inherited methods?
Example
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() {}
}
Expected output
method1_static_void
method2_static_String
method3_void
method4_String
Actual output
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 findings
I used to code in Java before I met groovy. Like most of you, groovy attracted me with many enhancements. This was to my surprise to discover that method visibility in groovy is handled different than Java!
To do this, go to the context menu of a table (or several tables) and press Scripted Extension → Generate POJO. groovy. Choose a destination folder, and that's it!
In Groovy, we can add a method named call to a class and then invoke the method without using the name call . We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: () . This can be especially useful in for example a DSL written with Groovy.
A method is in Groovy is defined with a return type or with the def keyword. Methods can receive any number of arguments. It's not necessary that the types are explicitly defined when defining the arguments. Modifiers such as public, private and protected can be added.
Instead of:
def methods = MyClass.methods.collect { it.name }
You just need the declared non-synthetic methods:
def methods = MyClass.declaredMethods.findAll { !it.synthetic }.name
After a change in Groovy 3 (see Simon's comment below) you will need to do:
def methods = MyClass.declaredMethods.findAll {
!it.synthetic && !it.getAnnotation(groovy.transform.Internal)
}.name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With