Code of the Day: Groovy Print all Methods of an object

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( "Object is null\r\n" );
		return;
    }
	if( !obj.metaClass && obj.getClass() ){
        printAllMethods( obj.getClass() );
		return;
    }
	def str = "class ${obj.getClass().name} functions:\r\n";
	obj.metaClass.methods.name.unique().each{ 
		str += it+"(); "; 
	}
	println "${str}\r\n";
}

Example Code

// Filename: printAllMethodsExample.groovy
void printAllMethods( obj ){
    if( !obj ){
		println( "Object is null\r\n" );
		return;
    }
	if( !obj.metaClass && obj.getClass() ){
        printAllMethods( obj.getClass() );
		return;
    }
	def str = "class ${obj.getClass().name} functions:\r\n";
	obj.metaClass.methods.name.unique().each{ 
		str += it+"(); "; 
	}
	println "${str}\r\n";
}
printAllMethods( null );
printAllMethods( 1 );
printAllMethods( "string" );
printAllMethods( [1:1] );

Output

Object is null
 
class java.lang.Integer functions:
equals(); 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(); 
 
class java.lang.String functions:
equals(); 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(); 
 
class java.lang.Class functions:
equals(); getClass(); hashCode(); notify(); notifyAll(); toString(); wait(); clear(); containsKey(); containsValue(); entrySet(); get(); isEmpty(); keySet(); put(); putAll(); remove(); size(); values(); clone();

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
Twitter

  • guest

    Unknown Unknown

    Hi, i’m newish to groovy. Thanks for the code. What is:
    if( !obj.metaClass && obj.getClass() ) means? I dun really get it. Thanks.

    • Unknown Unknown

      Welcome guest. It’s because hashMaps don’t have a metaClass property. So if the object has a class and doesn’t have a metaClass property then print the methods from the class it comes from. I updated the script to account for null values.

      Output:
      groovy> def x = ""
      groovy> println x.metaClass

      org.codehaus.groovy.runtime.HandleMetaClass@16165c24[groovy.lang.MetaClassImpl@16165c24[class java.lang.String]]

      groovy> def x = [1:1]
      groovy> println x.metaClass

      null

      • guest

        Unknown Unknown

        Thanks much for your answer. Your blog is really great, please post some more groovy goodness… and maybe video? BTW, is the output correct? I think it needs update? Thanks.

      • Unknown Unknown

        Thanks for your feedback, I updated the output and I’ll consider posting more blogs over groovy.

  • Mohd Abdullah

    Unknown Unknown

    What if I need only the methods of a single class and not their parents methods.

  • Andre

    Unknown Unknown

    Saved my day! Many thx.