Let’s say I have classes A and B. A and B have properties/fields ‘a’ and ‘b’ respectively of the same class C which in turn has some property/field ‘c’. Now the class A is valid only if a.c < N and B is valid only if b.c < M
As you can see we cannot put validation directly to the class C. Also there could be arbitrary large number of classes that use class C and each could have its own restrictions on value c.
I do not see how this could be done gracefully unless some kind of expression (ideally the unified expression language, JSR-245) is introduced. Something like this:
class A { @NotNull @Max(value=N, expression=”c”) C a; }
class B { @NotNull @Max(value=M, expression=”c”) C b; }
class C { private int c; public int getC() { return c; } }
Obviously expression could be arbitrary deep, could only be applied to Object targets, and a target object would become a root context for expression evaluation So something like this becomes possible:
class X { @NotNull @Max(value=100, expression="z.map['some-key']") Y y; }
class Y { @NotNull private Z z; public Z getZ() { return z; } }
class Z { @NotNull private Map<String,Integer> map; public Map<String,Integer> getMap() { return map; } }
Any thoughts?
|