Image

Imagehedin wrote in Imageru_java

getAnnotation поставило в тупик.

Товарищи ктониубдь моджет обьяснить почему

Method m = ...
PresentableParam annotation = m.getAnnotation(PresentableParam.class);

Все в порядке а

Class clazz = ...
Tag tag = (Tag) clazz.getAnnotation(Tag.class);

требуется каст. И не компилится без него.

притом что сигнатуры методов одинаковые.
Class 
    public < A extends Annotation> A getAnnotation(Class< A> annotationClass) {
        if (annotationClass == null)
            throw new NullPointerException();

        initAnnotationsIfNecessary();
        return (A) annotations.get(annotationClass);
    }

Method 
    public < T extends Annotation> T getAnnotation(Class< T> annotationClass) {
        if (annotationClass == null)
            throw new NullPointerException();

        return (T) declaredAnnotations().get(annotationClass);
    }