Querying methods and properties
In this first recipe about metaprogramming, we begin by looking at the introspection capabilities of Groovy. Introspection is a major feature of the Java language and, by extension, of the Groovy language. Using introspection, we can get the internal information of a class at runtime, including fields, methods, constructors, and so on.
Getting ready
To test out the introspection feature, let's create a couple of Groovy classes:
package org.groovy.cookbook class Book { String title Author author Long year Long pages Long getAmazonSalesPosition() { new Random().nextInt(1) + 1 } void attachReview(String review) { } } class Author { String name String lastName }
How to do it...
The following steps offer an example of the introspection capabilities of the language:
To check the type of a class, you can just execute the following code snippet:
assert 'java.lang.String' == String.name assert 'org.groovy.cookbook.Author' == Author.name
To list...