A common pattern in Java, whenever there is a need retrieve data from a database or deserialize JSON is to pass the target object class as a parameter.
A good example is Jackson's ObjectMapper#readValue
method with following signature:
public <T> T readValue(String content, Class<T> valueType) {
...
}
Passing class as a parameter looks repetitive. Since <T>
defines the type, why do we need to pass Class<T>
? Why it can't be simplified to:
public <T> T readValue(String content) {
...
}
And most importantly, how is Mockito able to find out the type of mock in such code?
Book book = mock();
Person person = mock();
Type Erasure
Reified Generics is a type system that makes generics type information available in runtime. Java does not support reification for many reasons explained in depth in essay Background: How We Got the Generics We Have written by Brian Goetz. One of them is backward compatibility. Generics were introduced in Java 1.5 on the 30th of September 2005 - almost 9 years after Java 1.0 release. Brian writes:
Migration compatibility. There was no known translation scheme at the time that would have allowed a migration to reified generics to be source- and binary-compatible, creating flag days and invalidating developer’s considerable investment in their existing code.
Lack of reified generics, or type erasure, in practice means that List<String> list = new ArrayList<String>()
in runtime is an equivalent of List list = new ArrayList()
- generics information is lost and the information about the type must be passed with extra Class
parameter. Such pattern is heavily used not only in Jackson but also Spring's JdbcTemplate
and pretty much every library that deserializes or converts data into Java objects.
class JdbcTemplate {
// ...
<T> T queryForObject(String sql, Class<T> requiredType) {
// ...
}
}
Array type hack
Back to Mockito example. In the past, to create mocks we had to pass Class
to the Mockito#mock
method - similar to previous Jackson and JdbcTemplate
examples:
Book book = mock(Book.class);
Person person = mock(Person.class);
This has changed in Mockito 4.9.0 released in November 2022 where the extra Class
parameter is not needed any longer:
Book book = mock();
Person person = mock();
Considering what we've just discussed type erasure and lack of reified generics - this looks like magic. How can Mockito know the type if generics type information is erased?
Lets take a look at the mock
method signature
<T> T mock(T... reified)
It takes an array reified
of type T
. When mock is created with mock()
, parameter value is turned into an empty array. Generics type gets erased but arrays type are reified - type of array is available in runtime and can be retrieved in following way:
static <T> Class<T> getClassOf(T[] array) {
return (Class<T>) array.getClass().getComponentType();
}
It is a hack, it makes mock
method API less obvious, but at the same time it is brilliant and very convenient for users.
INFO
This hack was discovered and described by Tagir Valeev and mock()
method in Mockito was directly inspired by his tweet.
While I don't think such hack should become a norm, for certain use cases it may be worth it! Perhaps one day we will get reified generics in Java and such hacks won't be even discussed.