1. Introduction
1.绪论
In this quick tutorial, we’re going to explore different ways to find the data type in Groovy.
在这个快速教程中,我们将探讨在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
2.原始类型
Groovy supports the same number of primitive types as Java. We can find the data type of primitives in three ways.
Groovy支持与Java相同数量的原始类型。我们可以通过三种方式找到基元的数据类型。
To begin, let’s imagine we have multiple representations of a person’s age.
首先,让我们想象一下,我们对一个人的年龄有多种表述。
First of all, let’s start with the instanceof operator:
首先,让我们从instanceof操作符开始。
@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.
instanceof是一个二进制运算符,我们可以用它来检查一个对象是否是一个特定类型的实例。如果该对象是该特定类型的实例,它将返回true ,否则返回false 。
Also, Groovy 3 adds the new !instanceof operator. It returns true if the object is not an instance of a type and false otherwise.
另外,Groovy 3增加了新的!instanceof操作符。如果对象不是一个类型的实例,它返回true ,否则返回false 。
Then, we can also use the getClass() method from the Object class. It returns the runtime class of an instance:
然后,我们也可以使用Object类中的getClass()方法。它返回一个实例的运行时类。
@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:
最后,让我们应用.class操作符来查找数据类型。
@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
3.收藏
Groovy provides support for various collection types.
Groovy提供了对各种集合类型的支持。
Let’s define a simple list in Groovy:
让我们用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:
但是在地图上,不能应用.class操作符。
@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.
在上面的代码片段中,ageMap.class将尝试从给定的地图中检索键类的值。对于地图,建议应用getClass() 而不是.class。
4. Objects & Class Variables
4.对象和类变量
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:
为了了解类变量是如何工作的,让我们假设我们有一个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.
请记住,getDeclaredField()返回某个类别的所有字段。
We can find the type of any object using instanceof, getClass() and .class operators:
我们可以使用instanceof, getClass() 和 .class 操作符找到任何对象的类型。
@Test
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
Person personObj = new Person()
Assert.assertTrue(personObj instanceof Person)
}
Moreover, we can also use the Groovy membership operator in:
此外,我们还可以使用Groovy成员操作符in。
@Test
public void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
Student studentObj = new Student()
Assert.assertTrue(studentObj in Person)
}
5. Conclusion
5.总结
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.
在这篇短文中,我们看到了如何在Groovy中找到数据类型。相比之下, getClass()方法比.class操作符更安全。我们还讨论了in操作符和instanceof操作符的工作。此外,我们还学习了如何获得一个类的所有字段并应用.type操作符。
As always, all the code snippets can be found over on GitHub.
一如既往,所有的代码片段都可以在GitHub上找到。