Another JSR with Potential for Goodness: JSR-303, Bean Validation API
Here's something less depressing (... than the acquisition of Sun by one of worst possible suitors [IMO]) from the Java land: JSR 303, "The Bean Validation API", seems like a rather useful little tool to have.
So what is it? Basically, it is a pluggable annotation-based component for validating data constraints on beans, typically used for things like validating user input like web forms. My personal interest, however, is more related to another obvious use case: that of validating request messages for web services. Either way, writing validation code is brain-numbing dull monkey coding. Writing such validation has been a necessary part of many Java developers daily job. But with this new API (and more importantly the leading implementation by Hibernate team) the programming part can be mostly eliminated; and the rest will be simple matter of applying annotation. At least when defining simple rigid data type constraints (min/max values, lengths, non-null, matches a regexp).
Instead, constraints to validate can be declared by simple standard annotations (and/or custom ones that can be built using guidelines and components from the API), attached to Bean fields and/or access methods. For example:
import javax.validation.constraints.*;
public class MyBean { @NotNull // can't be null (not optional @Size(min=4, max=40) // length, [4, 40] String name; @Max(20) // no more than 20 int retries; @NotNull @Valid // means that instance is recursively validated OtherBean childBean; }
And validation itself is done using something like
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator v = factory.getValidator(); Set<ConstraintViolation<MyBean>> probs = v.validate(myBeanInstance);
which returns set of ConstraintViolations, each of which details field that had the problem (path to field via references using dot notation) and matching localizable problem description.
For further discussion, this article is a good follow up; and JavaDocs should be enough to get you going with the details.