Nested Type Errors

Nested Type Errors

Dependency Expecting a Child

If we try to use the repository for saving the data we end with a similar error:

public Iterable<ModelObject> save(final Iterable<ModelObject> data) {
   // ERROR: Iterable<ModelObject> is not Iterable<ModelObjectEntity>
   return repository.create(data);
}

The difference here is that the type is inside the Iterable, used as a generic. Now we have to worry not only about the type we use, but also about the type inside the argument.

Method Receiving a Child

Another version of the same problem is using as argument a collection of child classes. These will extend the interface we want, but won't match the expected generic template.

Collection<ModelObjectEntity> data;

data = new ArrayList<>();

// ERROR: Iterable<ModelObjectEntity> is not Iterable<ModelObject>
service.save(data);

Solving Nested Type Errors

Removing Types

The easiest way, which is not recommended, is removing the nested type:

In short, this is already done by Java (type erasure), as generics are checked only when compiling. But we lose the most important feature of generics, ensuring that we are working with the data we want.

Transforming Types

Again, we can keep the interfaces by transforming the type. But in this case we will have to transform all the objects we have received:

Using Wildcard

Another option is making use of the tools offered by generics:

This works perfectly with the input:

But requires the output to use the wildcard:

And won't work with the inner dependency:

Adding a Type

Again, the best solution is adding a type to the interface:

And matching it with the type the dependencies want:

Last updated

Was this helpful?