Validation via Annotation
I've been beating around the bush with posting some of my ideas for validation via annoations. The implementation is extremely simple and offers a protocol independent way of validating input on setter calls. If the validation stuff doesn't interest you, the method of plugging annotations might be applicable to other things.
Notice that this annotation is meant for annotating other annotations (!) and has a single 'class' value which will be used to handle validation.
This is where the validation logic goes. The ValidationHandler is generic and is defined for a specific settings type-- which will be an annotation in our example. To give you some insight at this point, a 'settings' annotation would have minimum and maximum lengths for a String on some bean property's setter, such as 'setLogin(String login)'. Now for an implementation:
The above example would be used to validate something like an email address or some other user input. The ValidateExpr annotation is annotated with the Validate annotation with the class value of its handler-- ValidateExprHandler.class. Again, the annotation defines what class type is going to handle it. Annotating your object becomes quite simple once you've written a validate annotation and it's handler (two type declarations and two methods).
The final piece to the puzzle is the code to introspect and evaluate the handlers. For brevity's sake, I'm just including the tricky part of it all.
You may be thinking that there's too much reflection going on, but annotations are statically typed, so you wouldn't have to think twice about caching all the validators for an object.
I was thinking this same kind of annotation could be used for converters in the view, where again, we define a single Converter annotation that has a ConversionHandler type with two methods: stringToObject(T settings, String str) and objectToString(T settings, Object obj).
The reason why I like this way of providing behavior is that by annotating, we have not changed the behavior of the object itself. In addition, you have type safety and IDE support and the implementation is protocol generic so it can be used within JSF, Struts, or before persisting.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Validate {
Class<? extends ValidationHandler> value();
}
Notice that this annotation is meant for annotating other annotations (!) and has a single 'class' value which will be used to handle validation.
public interface ValidationHandler<T> {
public void validate(T settings, Object value) throws ValidationException;
public Class<T> getSettingsType();
}
This is where the validation logic goes. The ValidationHandler is generic and is defined for a specific settings type-- which will be an annotation in our example. To give you some insight at this point, a 'settings' annotation would have minimum and maximum lengths for a String on some bean property's setter, such as 'setLogin(String login)'. Now for an implementation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Validate(ValidateExprHandler.class)
public @interface ValidateExpr {
String value();
}
public class ValidateExprHandler
implements ValidationHandler<ValidateExpr> {
public void validate(ValidateExpr validateExpr, Object value)
throws ValidationException {
String i = (value != null) ? value.toString() : "";
Pattern p = Pattern.compile(validateExpr.value());
Matcher m = p.matcher(i);
if (!m.matches()) {
throw new ValidationException(i
+ " does not match the pattern "
+ validateExpr.value());
}
}
public ClassgetSettingsType() {
return ValidateExpr.class;
}
}
The above example would be used to validate something like an email address or some other user input. The ValidateExpr annotation is annotated with the Validate annotation with the class value of its handler-- ValidateExprHandler.class. Again, the annotation defines what class type is going to handle it. Annotating your object becomes quite simple once you've written a validate annotation and it's handler (two type declarations and two methods).
@ValidateRequired
@ValidateEmail
public void setEmail(String email) {
this.email = email;
}
@ValidateRequired
public String getPassword() {
return password;
}
The final piece to the puzzle is the code to introspect and evaluate the handlers. For brevity's sake, I'm just including the tricky part of it all.
public static void validateProperty(PropertyDescriptor p, Object value)
throws ValidationException {
Method m = p.getWriteMethod();
if (m != null) {
Annotation[] a = m.getAnnotations();
Validate v = null;
Annotation s = null;
ValidationHandler h = null;
for (int i = 0; i < a.length; i++) {
v = a[i].annotationType().getAnnotation(Validate.class);
if (v != null) {
try {
h = v.value().newInstance();
s = m.getAnnotation(h.getSettingsType());
h.validate(s, value);
} catch (InstantiationException ie) {
} catch (IllegalAccessException iae) {
}
}
}
}
}
You may be thinking that there's too much reflection going on, but annotations are statically typed, so you wouldn't have to think twice about caching all the validators for an object.
I was thinking this same kind of annotation could be used for converters in the view, where again, we define a single Converter annotation that has a ConversionHandler type with two methods: stringToObject(T settings, String str) and objectToString(T settings, Object obj).
The reason why I like this way of providing behavior is that by annotating, we have not changed the behavior of the object itself. In addition, you have type safety and IDE support and the implementation is protocol generic so it can be used within JSF, Struts, or before persisting.

1 Comments:
情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情趣,情趣,情趣,情趣,A片,視訊聊天室,聊天室,視訊,視訊聊天室,080苗栗人聊天室,上班族聊天室,成人聊天室,中部人聊天室,一夜情聊天室,情色聊天室,視訊交友網a片,a片
免費A片,AV女優,美女視訊,情色交友,免費AV,色情網站,辣妹視訊,美女交友,色情影片,成人影片,成人網站,A片,H漫,18成人,成人圖片,成人漫畫,情色網,日本A片,免費A片下載,性愛
A片,色情,成人,做愛,情色文學,A片下載,色情遊戲,色情影片,色情聊天室,情色電影,免費視訊,免費視訊聊天,免費視訊聊天室,一葉情貼圖片區,情色,情色視訊,免費成人影片,視訊交友,視訊聊天,視訊聊天室,言情小說,愛情小說,AIO,AV片,A漫,avdvd,聊天室,自拍,情色論壇,視訊美女,AV成人網,色情A片,SEX,成人論壇
情趣用品,A片,免費A片,AV女優,美女視訊,情色交友,色情網站,免費AV,辣妹視訊,美女交友,色情影片,成人網站,H漫,18成人,成人圖片,成人漫畫,成人影片,情色網
情趣用品,A片,免費A片,日本A片,A片下載,線上A片,成人電影,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人文章,成人影城,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,臺灣情色網,色情,情色電影,色情遊戲,嘟嘟情人色網,麗的色遊戲,情色論壇,色情網站,一葉情貼圖片區,做愛,性愛,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,美女交友,做愛影片
av,情趣用品,a片,成人電影,微風成人,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,愛情公寓,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,aio,av女優,AV,免費A片,日本a片,美女視訊,辣妹視訊,聊天室,美女交友,成人光碟
情趣用品.A片,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,色情遊戲,色情網站,聊天室,ut聊天室,豆豆聊天室,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,免費A片,日本a片,a片下載,線上a片,av女優,av,成人電影,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,成人網站,自拍,尋夢園聊天室
By
sexy, at 7:16 AM
Post a Comment
<< Home