Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all method names of a class without inherited methods with Groovy?

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

  • List the methods of a groovy class - Groovy Almanac
  • Discovering Class Members - docs.oracle.com
like image 411
Lernkurve Avatar asked Feb 27 '14 10:02

Lernkurve


People also ask

Does Groovy have private methods?

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!

How do you make a POJO on Groovy?

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!

How do you call a method in Groovy class?

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.

Is method in 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.


1 Answers

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

Groovy 3+

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
like image 199
tim_yates Avatar answered Nov 15 '22 10:11

tim_yates