{"id":772,"date":"2011-11-27T16:47:50","date_gmt":"2011-11-27T22:47:50","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=772"},"modified":"2013-02-13T23:13:02","modified_gmt":"2013-02-14T05:13:02","slug":"code-of-the-day-groovy-print-all-methods-of-an-object","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/11\/code-of-the-day-groovy-print-all-methods-of-an-object\/","title":{"rendered":"Code of the Day: Groovy Print all Methods of an object"},"content":{"rendered":"<p><b>Groovy Code<\/b><\/p>\n<pre lang='java'>\r\n\/**\r\n* @function printAllMethods\r\n* @purpose Prints an objects class name and then list the associated class functions.\r\n*\/\r\n\/\/ Filename: printAllMethodsExample.groovy\r\nvoid printAllMethods( obj ){\r\n    if( !obj ){\r\n\t\tprintln( \"Object is null\\r\\n\" );\r\n\t\treturn;\r\n    }\r\n\tif( !obj.metaClass && obj.getClass() ){\r\n        printAllMethods( obj.getClass() );\r\n\t\treturn;\r\n    }\r\n\tdef str = \"class ${obj.getClass().name} functions:\\r\\n\";\r\n\tobj.metaClass.methods.name.unique().each{ \r\n\t\tstr += it+\"(); \"; \r\n\t}\r\n\tprintln \"${str}\\r\\n\";\r\n}\r\n<\/pre>\n<p><b>Example Code<\/b><\/p>\n<pre lang='java'>\r\n\/\/ Filename: printAllMethodsExample.groovy\r\nvoid printAllMethods( obj ){\r\n    if( !obj ){\r\n\t\tprintln( \"Object is null\\r\\n\" );\r\n\t\treturn;\r\n    }\r\n\tif( !obj.metaClass && obj.getClass() ){\r\n        printAllMethods( obj.getClass() );\r\n\t\treturn;\r\n    }\r\n\tdef str = \"class ${obj.getClass().name} functions:\\r\\n\";\r\n\tobj.metaClass.methods.name.unique().each{ \r\n\t\tstr += it+\"(); \"; \r\n\t}\r\n\tprintln \"${str}\\r\\n\";\r\n}\r\nprintAllMethods( null );\r\nprintAllMethods( 1 );\r\nprintAllMethods( \"string\" );\r\nprintAllMethods( [1:1] );\r\n<\/pre>\n<p><b>Output<\/b><\/p>\n<pre lang='java'>\r\nObject is null\r\n\r\nclass java.lang.Integer functions:\r\nequals(); getClass(); hashCode(); notify(); notifyAll(); toString(); wait(); byteValue(); doubleValue(); floatValue(); intValue(); longValue(); shortValue(); bitCount(); compare(); compareTo(); decode(); getInteger(); highestOneBit(); lowestOneBit(); numberOfLeadingZeros(); numberOfTrailingZeros(); parseInt(); reverse(); reverseBytes(); rotateLeft(); rotateRight(); signum(); toBinaryString(); toHexString(); toOctalString(); valueOf(); \r\n\r\nclass java.lang.String functions:\r\nequals(); getClass(); hashCode(); notify(); notifyAll(); toString(); wait(); charAt(); codePointAt(); codePointBefore(); codePointCount(); compareTo(); compareToIgnoreCase(); concat(); contains(); contentEquals(); copyValueOf(); endsWith(); equalsIgnoreCase(); format(); getBytes(); getChars(); indexOf(); intern(); isEmpty(); lastIndexOf(); length(); matches(); offsetByCodePoints(); regionMatches(); replace(); replaceAll(); replaceFirst(); split(); startsWith(); subSequence(); substring(); toCharArray(); toLowerCase(); toUpperCase(); trim(); valueOf(); \r\n\r\nclass java.lang.Class functions:\r\nequals(); getClass(); hashCode(); notify(); notifyAll(); toString(); wait(); clear(); containsKey(); containsValue(); entrySet(); get(); isEmpty(); keySet(); put(); putAll(); remove(); size(); values(); clone(); \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Groovy Code \/** * @function printAllMethods * @purpose Prints an objects class name and then list the associated class functions. *\/ \/\/ Filename: printAllMethodsExample.groovy void printAllMethods( obj ){ if( !obj ){ println( &#8220;Object is null\\r\\n&#8221; ); return; } if( !obj.metaClass &#038;&#038; obj.getClass() ){ printAllMethods( obj.getClass() ); return; } def str = &#8220;class ${obj.getClass().name} functions:\\r\\n&#8221;; obj.metaClass.methods.name.unique().each{ &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/11\/code-of-the-day-groovy-print-all-methods-of-an-object\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Groovy Print all Methods of an object<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[30,180],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-code-of-the-day","tag-groovy"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=772"}],"version-history":[{"count":8,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/772\/revisions\/1186"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}