I'm trying to store a collection of generic objects within an Exception and am having troubles figuring out the generics. Specifically, I'm using Hibernate Validator and would like to save the collected list of violations within an exception for processing in another layer of the application. Here's an example:
Set<ConstraintViolation<User>> violations = validator.validate(user);
if (violations.size() > 0) {
throw new ValidationException("User details are invalid", violations);
}
In Eclipse, the throws line is showing the constructor undefined and is suggesting I change the constructor signature to ValidationException(String, Set<ConstraintViolation<User>>. Here's ValidationException:
public class ValidationException extends Exception {
private Set<ConstraintViolation<?>> violations;
public ValidationException() {
}
public ValidationException(String msg) {
super(msg);
}
public ValidationException(String msg, Throwable cause) {
super(msg, cause);
}
public ValidationException(String msg, Set<ConstraintViolation<?>> violations) {
super(msg);
this.violations = violations;
}
public Set<ConstraintViolation<?>> getViolations() {
return violations;
}
}
However, I want to keep ValidationException generic so that I can use it for more than just User validations. I've tried Set<ConstraintViolation<? extends Object>> as well, but get the same results.
Is there a way to accomplish what I'm trying to do?