Reputation: 5076
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
Upvotes: 3
Views: 2114
Reputation: 81074
Your expectations are backwards from what the specification requires. From the spec (page 27 on the PDF):
For a given interface Z, constraints marked as belonging to the group Z (i.e. where the annotation element groups contains the interface Z) or any of the super interfaces of Z (inherited groups) are considered part of the group Z.
In other words, if you validated with Section1.class
and tagged @NotNull
with All.class
, the constraint would be applied. But not the other way around.
Think of it as a set: All
is a common set of constraints, and by extending All
, Section1
becomes a superset of All
, not a subset. Thus, when you validate using All
, it applies only those specified by All
and its super interfaces.
If you want All
to be a superset of the constraints found in Section1
, you need to flip the inheritance:
interface All extends Section1 /*, Section2, Section3...*/ {}
In this sense, you can say to yourself that All
inherits all of the constraints of Section1
.
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.
Upvotes: 6