Given the following classes and interfaces
class A{
@NotNull(groups=Section1.class)
private String myString
}
interface All{}
interface Section1 extends All {}
When calling
A a = new A();
validator.validate(a,All.class);
I would expect that it should be invalid since myString is null and it’s notNull group extends All but it does not. Note that i’m using the Hibernate impl (4.0.2.GA) of the validator
Your expectations are backwards from what the specification requires. From the spec (page 27 on the PDF):
In other words, if you validated with
Section1.classand tagged@NotNullwithAll.class, the constraint would be applied. But not the other way around.Think of it as a set:
Allis a common set of constraints, and by extendingAll,Section1becomes a superset ofAll, not a subset. Thus, when you validate usingAll, it applies only those specified byAlland its super interfaces.If you want
Allto be a superset of the constraints found inSection1, you need to flip the inheritance:In this sense, you can say to yourself that
Allinherits all of the constraints ofSection1.This is also the reasonable implementation, as Java makes it extremely difficult to find out who extends a certain interface (after all, the class file might not even be available until it’s referenced), but trivially easy to see the interfaces that a given interface extends.