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();
What REALLY is Data Science? Told by a Data Scientist - By Joma Tech
Writing perfect code is a challenging process. That's where code reviews come in to help…
"The Next Leap: How A.I. will change the 3D industry - Andrew Price - Blender"
"Captain Disillusion: World's Greatest Blenderer - Live at the Blender Conference 2018 - CaptainDisillusion"
My 5 Favorite Linux Shell Tricks for SPEEEEEED (and efficiency) - By tutoriaLinux > What's…
View Comments
Hi, i'm newish to groovy. Thanks for the code. What is:
if( !obj.metaClass && obj.getClass() ) means? I dun really get it. Thanks.
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
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.
Thanks for your feedback, I updated the output and I'll consider posting more blogs over groovy.
What if I need only the methods of a single class and not their parents methods.
Try this. http://stackoverflow.com/questions/22622131/how-to-check-whether-a-groovy-class-overrides-a-parents-method
Saved my day! Many thx.