I am working with javax.validation / Hibernate validation. To validate an annotated (e.g. @NotNull at an attribute) bean, the hibernate validator is used with the javax.validation interfaces:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
public interface Validator {
<T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups);
}
public interface ConstraintViolation<T> {
String getMessage();
Path getPropertyPath();
T getRootBean();
}
The goal is to write a single method, which accepts differently typed sets, like:
Set<ConstraintViolation<TaskList>> violationsTaskList = validator.validate(taskList);
Set<ConstraintViolation<TaskItem>> violationsTaskItem = validator.validate(taskItem);
public void consumeViolations(Set<ConstraintViolation<?> violations) {
// Do something meaningful...
violations.getMessage();
}
Using the wildcard ? is best think I came up with until now but it is rejected by the compiler with the message:
consumeViolations(....<?>) cannot be applied to consumeViolations(....<TaskList>)
I do not want to write this method for every type T just to call getMessage() and pass it to the next method.
Is it possible to write such a method? I only want to access methods of the ConstraintViolation interface, which are not dependent on the type T like String getMessage().