无法使用java.lang.annotation.Annotation实现泛型:不兼容的类型

时间:2014-07-09 11:55:13

标签: java generics compiler-errors annotations

我希望根据提供的检索值的位置一般检索注释。无论是来自田野还是吸气者/安装者。因此,提供者需要返回Field注释中的信息。还有其他注释计划,因此实现需要是通用的。

该方法正是java.lang.Class类中的方法。

import java.lang.annotation.Annotation;

public interface Setter<Instance, Type>
{
    <A extends Annotation> A getAnnotation(Class<A> annotationClass);
}

但是,当我使用这个方法时..

final Field annotation = setter.getAnnotation(Field.class);

我得到一个并发症错误..

incompatible types
found   : java.lang.annotation.Annotation
required: Field

这几乎没有意义,因为FieldAnnotation

在实现泛型时是否需要特别考虑注释?

为什么这不起作用?它与Java API完全相同。

仅供参考,以下是Field注释。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Field
{
    /**
     * The effective path of the local or result set value to load into the field.
     */
    String value() default "";
}  

0 个答案:

没有答案