Search code examples
groovyintrospection

How to check whether a Groovy class overrides a parent's method


I have two Groovy classes, Child1 and Child2, which derive from an abstract parent class Parent. The parent class implements three methods, only different in their parameters (i.e. their method names are the same).

Now I have an instance of the child class. The task is to check whether that object's class implements (overrides) one or more of the parent's methods.

I tried with Groovy's Inspector on the child class object. This gives me a list of all methods, but I am not sure how to read the output. In particular I do not understand whether the method I am looking for is implement in the child class, or only in the parent class.

Can anybody help me with this, do I maybe need another way of introspection?


Solution

  • So I ended up with this solution, which does exactly what I wanted:

    def pattern = ~/^public void ... $/
    for(method in obj.metaClass.methods.findAll { it ==~ pattern })
    {
        /* Do some stuff... */
    }