You can use the test that you usually use in Java.
def leftShift(other) { if(other instanceof String) { println "$other is a string!" } }
When you call other.getClass() , the result class is an instance of java.lang.Class, which can be compared to String.class. Note another may be null, in which the test "other instanceof String" is false.
UPDATE:
Here is a simple example that creates an instance of Groovy GString that is not an instance of a string:
def x = "It is currently ${ new Date() }" println x.getClass().getName() println x instanceof String println x instanceof CharSequence
Outputs:
It is currently Thu Aug 21 15:42:55 EDT 2014 org.codehaus.groovy.runtime.GStringImpl false true
GStringImpl extends GString, which has methods that make it behave like a String object and implements the CharSequence interface, like the String class. Check if the other CharSequence object is true if the object is an instance of String or GString.
def leftShift(other) { if(other instanceof CharSequence) { println "$other is a string!" } }
source share