I have a more difficult problem (than the question "Java map with values limited by the key type parameter") for displaying the key and the type of value on the map. Here it is:
interface AnnotatedFieldValidator<A extends Annotation> {
void validate(Field f, A annotation, Object target);
Class<A> getSupportedAnnotationClass();
}
Now I want to store validators on the map so that I can write the following method:
validate(Object o) {
Field[] fields = getAllFields(o.getClass());
for (Field field: fields) {
for (Annotation a: field.getAnnotations()) {
AnnotatedFieldValidator validator = validators.get(a);
if (validator != null) {
validator.validate(field, a, target);
}
}
}
}
(type parameters are omitted here, since I have no solution). I also want to be able to register my validators:
public void addValidator(AnnotatedFieldValidator<? extends Annotation> v) {
validators.put(v.getSupportedAnnotatedClass(), v);
}
Using this (only) public modifier method, I can guarantee that the map contains entries for which the key (annotation class) matches the annotation class supported by the validator.
Here is an attempt:
I declare Map validators as follows:
private Map<Class<? extends Annotation>, AnnotatedFieldValidator<? extends Annotation>> validators;
, ( - addValidator()
), :
for (Annotation a: field.getAnnotations()) {
AnnotatedFieldValidator<? extends Annotation> validator = validators.get(a);
if (validator != null) {
validator.validate(field, validator.getSupportedAnnotationClass().cast(a), target);
}
}
: The method validate(Field, capture#8-of ?, Object) in the type AnnotatedFieldValidator<capture#8-of ?> is not applicable for the arguments (Field, capture#9-of ?, Object)
.
, : AnnotatedFieldValidator
(A), getSupportedAnnotationClass()
, validate()
; , AnnotationClass validate()
. getSupportedAnnotationClass()
, validate()
?
validate()
, validate()
, , , addValidator()
.