在Java中强制将通用接口作为注释的值
有没有办法在Java注释中接收泛型类型作为值
// The generics interface:
public interface TypeConverter<S, R> {
public R convert(S sourceType);
}
// The implementation:
public DateConverter extends TypeConverter<String, Date> {
public String convert(Date sourceType) { ... }
}
// Applying the custom converter through an annotation on a field:
...
Converter(DateConverter.class);
public Date dateField;
...
// The issue! Receiving the generic type in an annotation value:
public @interface Converter {
//How to use the generic type as the type of "value"?
Class value() default void.class;
// versus
//Class<? extends TypeConverter> type() default void.class;
}
查看上面注释Converter
上的注释
# 1 楼答案
没有办法做到这一点,但是,有一个解决办法,你可能会感兴趣
如果不希望注释参数是必需的,并且无法传递默认值,则始终可以使用数组
有两个缺点
type
值李>