Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

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:

@Test
public void givenWhenParameterTypeIsDouble_thenReturnTrue() {
    Person personObj = new Person(10.0)
    Assert.assertTrue((personObj.ageAsDouble).getClass() == Double)
}

Lastly, let's apply the .class operator to find the data type:

@Test
public void givenWhenParameterTypeIsString_thenReturnTrue() {
    Person personObj = new Person("10 years")
    Assert.assertTrue(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:

@Test
public void givenGroovyList_WhenFindClassName_thenReturnTrue() {
    def ageList = ['ageAsString','ageAsDouble', 10]
    Assert.assertTrue(ageList.class == ArrayList)
    Assert.assertTrue(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:

@Test
public void givenClassName_WhenParameterIsInteger_thenReturnTrue() {
    Assert.assertTrue(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:

@Test
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
    Person personObj = new Person()
    Assert.assertTrue(personObj instanceof Person)
}

Moreover, we can also use the Groovy membership operator in:

@Test
public void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
    Student studentObj = new Student()
    Assert.assertTrue(studentObj in 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.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!