Note that there are some explanatory texts on larger screens.

plurals
  1. POlimit map key and value types - more complicated
    primarykey
    data
    text
    <p>I have a more complicated issue (than question 'Java map with values limited by key's type parameter' question) for mapping key and value type in a Map. Here it is:</p> <pre><code>interface AnnotatedFieldValidator&lt;A extends Annotation&gt; { void validate(Field f, A annotation, Object target); Class&lt;A&gt; getSupportedAnnotationClass(); } </code></pre> <p>Now, I want to store validators in a map, so that I can write the following method:</p> <pre><code>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); } } } } </code></pre> <p>(type parameters are omitted here, since I do not have the solution). I also want to be able to register my validators:</p> <pre><code>public void addValidator(AnnotatedFieldValidator&lt;? extends Annotation&gt; v) { validators.put(v.getSupportedAnnotatedClass(), v); } </code></pre> <p>With this (only) public modifier method, I can ensure the map contains entries for which the key (annotation class) matches the validator's supported annotation class.</p> <p>Here is a try:</p> <p>I declare the validators Map like this:</p> <pre><code>private Map&lt;Class&lt;? extends Annotation&gt;, AnnotatedFieldValidator&lt;? extends Annotation&gt;&gt; validators; </code></pre> <p>I'm aware I cannot properly link the key and value (link is assumed OK due to only access through <code>addValidator()</code>), so I tried a cast:</p> <pre><code>for (Annotation a: field.getAnnotations()) { AnnotatedFieldValidator&lt;? extends Annotation&gt; validator = validators.get(a); if (validator != null) { validator.validate(field, validator.getSupportedAnnotationClass().cast(a), target); } } </code></pre> <p>But this does not work: <code>The method validate(Field, capture#8-of ?, Object) in the type AnnotatedFieldValidator&lt;capture#8-of ?&gt; is not applicable for the arguments (Field, capture#9-of ?, Object)</code>.</p> <p>I can't figure out why this does not work: the <code>AnnotatedFieldValidator</code> has a single type parameter (A), which is used both as the return type of <code>getSupportedAnnotationClass()</code> and as a parameter of <code>validate()</code>; thus, when casting the annotation to supportedAnnotationClass, I should be able to pass it as the parameter to <code>validate()</code>. Why is the result of <code>getSupportedAnnotationClass()</code> considered a different type than the parameter of <code>validate()</code>?</p> <p>I can solve the <code>validate()</code> method by removing wildcards in the validators declaration and <code>validate()</code> method, but then, of course, <code>addValidator()</code> doesn't compile.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload