I have an interface:
public interface IValidator<E extends IEntity>
{
void validate(final E entity);
}
I would like to create a ValidatorService class, something like this:
public class ValidatorService
{
private List<IValidator<IEntity>> validators;
public void validate(IEntity e)
{
for(IValidator<IEntity> validator : this.validators)
{
//if( instanceof )???
validator.validate(e);
}
}
}
How can I make sure that validators are applied only to their corresponding classes (please see '???' in code)? I.e.: if we have entity of type EntityA, then only validator IValidator will be invoked? I think I lack some knowledge of generics.. Thanks
Due to type erasure, this is not possible.
Instead, you can add a Class<E> getEntityClass()
method to the interface.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments