Reputation: 8629
When I'm calling a function in Groovy, how do I determine the class of the caller?
For example, I want to know what the class of foo
is, inside the printFoo()
function:
foo.printFoo()
def printFoo() {
print(this.class)
}
And this should print out the class of foo
Upvotes: 2
Views: 7519
Reputation: 8333
I don't know of any Groovy-specific way, but you can get the class name with object.getClass().name
. Is that what you are looking for?
If you want to check if an object implements a particular interface or extends a particular class (e.g. Date) use:
object instanceof Date
or to check if the class of an object is exactly a particular class (not a subclass of it), use:
object.getClass() == Date
There's also the in
operator: object in Date
Upvotes: 3
Reputation: 27255
The language and the runtime don't provide a reliable mechanism to do what you are asking about. You can do some pokey jiggery inspecting stack frames but that is really detailed low level stuff and won't really be reliable for a number of reasons. The short answer is, the language just doesn't support it.
Upvotes: 1