我想使用Java註釋驗證我的參數。我現在不用如何使用爲列表編寫自己的註釋。使用Java註釋列表
這裏一個簡單的例子:
class test{
@myAnnotation
List<myObject> myElements =new List<>(); // validated List
}
class myObject{
String name;
}
我的註釋界面:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=myAnnotation_Validator.class)
@Documented
public @interface myAnnotation {
String message() default "List is not valid";
Class <?>[] groups() default {};
Class <?extends Payload>[] payload() default{};
String[] namen();
}
public class myAnnotation_Validator implements ConstraintValidator<myAnnotation, Collection> {
@Override
public void initialize(Kredite_List_Check a) {
// What to do here???
}
@Override
public boolean isValid(Collection t, ConstraintValidatorContext cvc) {
// What to do here???
// How could i get the names from my List "myElements" ?
return false;
}
}
在本實施例我的列表是有效的,如果從我的列表中的每個元素有另一個名稱。我不知道如何在我的Validator類和myObject-elements的名稱中獲得listobject。
UPDATE:
我試圖描述以另一種方式,我的問題:
我的名單是不是有效的,如果從類型「myObject的」兩個元素在我的名單(「myElements」)具有相同的名字!
我怎麼能認識到這與註釋?
是否有任何具體的理由選擇一些裝飾列表的註釋? –
你看過http://stackoverflow.com/questions/7865209/how-to-create-a-constraintvalidator-for-list –