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


i have 2 groovy classes, child1 , child2, derive abstract parent class parent. parent class implements 3 methods, different in parameters (i.e. method names same).

now have instance of child class. task check whether object's class implements (overrides) 1 or more of parent's methods.

i tried groovy's inspector on child class object. gives me list of methods, not sure how read output. in particular not understand whether method looking implement in child class, or in parent class.

can me this, maybe need way of introspection?

hope helps. still feel there groovier way. try using collectinheritedmethods() see if wanted. deliberately wanted return list of matched methods instead of flag because can see methods implemented super class, groovy truth (if(collectinheritedmethods(..))) on list sufficient flagging.

abstract class parent {     void method1() { println "no param" }     void method1( def ) { println "param: $a" }     void method1( list a, map b ) { println "param: $a , $b" } }  class child extends parent {     void method1() { println "a: no param" }     void method1( def ) { println "a: param $a" }     void method1( def a, def b ) { println "a: param $a , $b" } }  list collectinheritedmethods( class parent, class child ) {     list inheritedmethods = []      def parentmethods = parent.declaredmethods.findall { !it.synthetic }     def childmethods = child.declaredmethods.findall { !it.synthetic }      for( chmet in childmethods ) {         for( pamet in parentmethods ) {             if( chmet.name == pamet.name &&                    chmet.parametertypes == pamet.parametertypes &&                       chmet.returntype == pamet.returntype ) {                  inheritedmethods << child.getmethod( chmet.name,                                                      chmet.parametertypes )                                          .togenericstring()                  //if flag required check if method implemented                 //flag = true                 //break             }         }     }      //groovier way     /*inheritedmethods = childmethods.findall { chmet ->          parentmethods.findall { pamet ->              chmet.name == pamet.name &&                  chmet.parametertypes == pamet.parametertypes &&                      chmet.returntype == pamet.returntype          }      }*/      inheritedmethods }  assert collectinheritedmethods( parent, child ) ==         ["public void child.method1()",          "public void child.method1(java.lang.object)"] 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -