Hi folks, I will try to be quick. Me and my team, we are using Lombok to simplify a lot of code. In special, when using the @Delegate annotation on fields.
We are trying to create some services which delegate directly to Spring Data Repositories. We are using this just for CRUD purposes.
But when we do this, we are getting a compile error because of methods like these ones above:
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
// LKE THIS ONE HERE!
<S extends T> S save(S entity);
// LKE THIS ONE HERE!
<S extends T> Iterable<S> save(Iterable<S> entities);
}
// JpaRespository extends from CrudRepository!!!
public interface UserRepository extends JpaRepository<User, UUID> {
User findById(UUID id);
}
And in our service we do something like this:
@Service
public class UserService {
@Delegate
private UserRepository userRepository;
}
When we try to use the save(user) delegated method on our services, we got a compile error.
[ERROR] UserController.java:[52,20] method save in class br.com.sinax.common.login.service.UserService cannot be applied to given types;
[ERROR] required: java.lang.Iterable<S>
[ERROR] found: br.com.sinax.common.login.domain.User
[ERROR] reason: cannot infer type-variable(s) S
[ERROR] (argument mismatch; br.com.sinax.common.login.domain.User cannot be converted to java.lang.Iterable<S>)
Apparently, when we are using Generics like those ones used above, Lombok is not working accordingly. Because S is not within its bound.
Is there any way to handle cases like this? Any tips?
Thanks, Felipe.