Wednesday, June 21, 2006
Using Annotations to add Validity Constraints to JavaBeans Properties
I often - and I mean really, really often - find myself having to verify this piece of data or that appears to be acceptable to the application I am writing or maintaining.
There is sort of a 3-way tug-of-war between reusability of the validation rules, precision of the diagnostic messages when data is rejected, and the ease-of-expressing what these constraints are in the program.
Ideally, I like something that is a blend of code and data to do it.
Table-driven schemes are kind of passe. They do not really express all that much and when special cases arise, as they inevitably do, flags start popping up like dandelions in May. Next thing you know, they are everywhere - and you have a mess to clean up.
Objects are good, assuming you are using an OOP program. However, while dynamically constructing the is useful - the program can wind up not exactly sapient about why this thing or that thing is
Java SE 5
That in and of itself is no big deal. You can still do that, with varying degrees of elegance (also known as maintainability) using the other techniques.
What is different is that annotations as visible to the Javadoc compiler, to IDEs, a person reading the source code, QA/test software, and the programs themselves. They all 5 get their information from one source, ultimately.
That tends to be a useful distinction. One that I find really attractive.
Using Annotations to add Validity Constraints to JavaBeans Properties:
This article is a very nice tutorial with some extremely well-written examples. It shows just how you can validate data in an elegant way with Java SE 5.
If that makes for code that is faster to write, easier to maintain, and quickly explained to other staff/manager and users - then I am all for it.
There is sort of a 3-way tug-of-war between reusability of the validation rules, precision of the diagnostic messages when data is rejected, and the ease-of-expressing what these constraints are in the program.
Ideally, I like something that is a blend of code and data to do it.
Table-driven schemes are kind of passe. They do not really express all that much and when special cases arise, as they inevitably do, flags start popping up like dandelions in May. Next thing you know, they are everywhere - and you have a mess to clean up.
Objects are good, assuming you are using an OOP program. However, while dynamically constructing the is useful - the program can wind up not exactly sapient about why this thing or that thing is
goodor
bad. And since that is the point of writing data-input checking logic in the first place - that is not so good either.
Java SE 5
Annotationslook like a good mechanism to do this with. They go into the code, they store metadata. So know you, as a programmer, have a way to express what is acceptable/unnacceptable, why it is acceptable/unnacceptable, and (if you are clever) - how to make it acceptable. You can reuse that information in different ways, in different places.
That in and of itself is no big deal. You can still do that, with varying degrees of elegance (also known as maintainability) using the other techniques.
What is different is that annotations as visible to the Javadoc compiler, to IDEs, a person reading the source code, QA/test software, and the programs themselves. They all 5 get their information from one source, ultimately.
That tends to be a useful distinction. One that I find really attractive.
Using Annotations to add Validity Constraints to JavaBeans Properties:
Currently there is no standard way to add validity constraints to Java classes. This is in contrast to other languages like XML in which (using XML Schema) relatively rich constraints may be specified. With the advent of annotations (JSR 175) in the Java 2 Platform, Standard Edition (J2SE) 5.0 a convenient mechanism now exists to allow such constraints to be specified in the Java meta model. This article explores how annotations might be used for this purpose and discusses why this may be an important capability.[By the way, Java SE 7 adds a facility called
Descriptors. Descriptors add this capability to specify validity constraints on the attributes and operations of MBeans.]
This article is a very nice tutorial with some extremely well-written examples. It shows just how you can validate data in an elegant way with Java SE 5.
If that makes for code that is faster to write, easier to maintain, and quickly explained to other staff/manager and users - then I am all for it.
