如何获取泛型类型的文字类?

时间:2017-06-07 09:49:15

标签: java generics annotations

我有一个需要改编者类的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigurationAnnotation {

  Class<? extends AdaptatorAbstract<?>> adaptator() default AnAdaptator.class;
}

其中一个改编者需要具有如下通用类型:

public class ListEnumAdaptator<T extends Enum<T>> extends AdaptatorAbstract<T> {

  private final Class<T> clazz;

  public ListEnumAdaptator(Class<T> c) {
    this.clazz = c;
  }

  @Override
  public T adaptValue(String k, String v)  {
    return Enum.valueOf(clazz, v);
  }

  public static Class<? extends ListEnumAdaptator<?>> forClass(Class<? extends Enum<?>> c) {
    return new ListEnumAdaptator(c).clazz;
  }
}

我创建静态方法以便用于注释。我以为我能做到:

@ConfigurationAnnotation(adaptator = ListEnumAdaptator.forClass(AnEnum.class))

但是我收到了以下错误:

// The value for annotation attribute ConfigurationAnnotation.adaptator must be a class literal

有没有办法获得genric类的“class literal”?

例如,我想要类型ListEnumAdaptator<AnEnum>的类文字。

编辑:

AnAdaptator定义:

public class AnAdaptator extends AdaptatorAbstract<Object> {

  @Override
  public Object adaptValue(final String k, final String v) throws Exception {
    throw new Exception("No Adaptator is configured for " + k);
  }

}

0 个答案:

没有答案