JSR303 group inheritance

Considering 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 suggest it should be invalid since myString is null and the group notNull extends everything, but it is not. Please note that I am using the Hibernate impl (4.0.2.GA) validator

+3


source to share


1 answer


Your expectations deviate from the requirements of the specification. From the spec (page 27 in PDF):

For a given Z interface, constraints marked as belonging to the Z group (i.e. where the annotation element groups contain the Z interface) or any of the Z superinterfaces (inherited groups) are considered part of the Z group.

In other words, if you confirmed with Section1.class

and are marked @NotNull

with All.class

, this restriction will be applied. But not vice versa.

Think of it as a set: All

- a common set of constraints, and expanding All

, Section1

becoming a superset All

, not a subset. This way, when you check usage All

, it only applies the ones defined All

and its super interfaces.



If you want to All

be a superset of the constraints found in Section1

, you need to reverse inheritance:

interface All extends Section1 /*, Section2, Section3...*/ {}

      

In this sense, you can tell yourself that you All

inherit all restrictions Section1

.

This is also a sensible implementation as Java makes it extremely difficult to figure out who is extending a particular interface (after all, the class file may not even be available until it is referenced), but it is trivially easy to see the interfaces that a given interface is extending.

+5


source







All Articles