1. Introduction
In this quick tutorial, we’re going to explore different ways to find the data type in Groovy.
Actually, it’s different depending on what we’re doing:
- First, we’ll look at what to do for primitives
- Then, we’ll see how collections bring some unique challenges
- And finally, we’ll look at objects and class variables
2. Primitive Types
Groovy supports the same number of primitive types as Java. We can find the data type of primitives in three ways.
To begin, let’s imagine we have multiple representations of a person’s age.
First of all, let’s start with the instanceof operator:
@Test
public void givenWhenParameterTypeIsInteger_thenReturnTrue() {
Person personObj = new Person(10)
Assert.assertTrue(personObj.ageAsInt instanceof Integer);
}
instanceof is a binary operator that we can use to check if an object is an instance of a given type. It returns true if the object is an instance of that particular type and false otherwise.
Also, Groovy 3 adds the new !instanceof operator. It returns true if the object is not an instance of a type and false otherwise.
Then, we can also use the getClass() method from the Object class. It returns the runtime class of an instance:
def "givenWhenParameterTypeIsDouble_thenReturnTrue"() {
given:
Person personObj = new Person(10.0)
expect:
personObj.ageAsDouble.class == Double
}
Lastly, let’s apply the .class operator to find the data type:
def "givenWhenParameterTypeIsString_thenReturnTrue"() {
given:
Person personObj = new Person("10 years")
expect:
personObj.ageAsString.class == String
}
Similarly, we can find the data type of any primitive type.
3. Collections
Groovy provides support for various collection types.
Let’s define a simple list in Groovy:
def "givenGroovyList_WhenFindClassName_thenReturnTrue"() {
given:
def ageList = ['ageAsString', 'ageAsDouble', 10]
expect:
ageList.class == ArrayList
ageList.getClass() == ArrayList
}
But on maps, the .class operator cannot be applied:
@Test
public void givenGrooyMap_WhenFindClassName_thenReturnTrue() {
def ageMap = [ageAsString: '10 years', ageAsDouble: 10.0]
Assert.assertFalse(ageMap.class == LinkedHashMap)
}
In the above code snippet, ageMap.class will try to retrieve the value of the key class from the given map. For maps, it is recommended to apply getClass() than .class.
4. Objects & Class Variables
In the above sections, we used various strategies to find the data type of primitives and collections.
To see how class variables work, let’s suppose we have a class Person:
def "givenClassName_WhenParameterIsInteger_thenReturnTrue"() {
expect:
Person.class.getDeclaredField('ageAsInt').type == int.class
}
Remember that the getDeclaredField() returns all the fields of a certain class.
We can find the type of any object using instanceof, getClass() and .class operators:
def "givenWhenObjectIsInstanceOfType_thenReturnTrue"() {
given:
Person personObj = new Person()
expect:
personObj.class == Person
}
Moreover, we can also use the Groovy membership operator in:
def "givenWhenInstanceIsOfSubtype_thenReturnTrue"() {
given:
Student studentObj = new Student()
expect:
studentObj.class.superclass == Person
}
5. Conclusion
In this short article, we saw how to find the data type in Groovy. By comparison, the getClass() method is safer than .class operator. We also discussed the working of in operator along with instanceof operator. Additionally, we learned how to get all the fields of a class and apply the .type operator.
As always, all the code snippets can be found over on GitHub.