如何创建具有特定注释类的通用类型?

huangapple go评论67阅读模式
英文:

How to make generic type of specifically annotated class?

问题

我需要将一个接口泛型化,该接口是针对带有特定注释的类进行注释的,例如具有以下实体类

@Entity
@Audited
class Record {
   private Long id;
   private String something;
   private String somethingElse;
}

该类使用Hibernate Envers进行审计,我想提供一个服务类,该类获取一些修订数据。类似以下方式:

public interface AuditRecordService<E extends Audited> {
//
}

我知道我可以修改Record类以扩展一个抽象类,类似这样:

@Entity
@Audited
class Record extends RevisionedEntity {
...

这对我有效,但我想避免不必要的扩展,因为它已经使用了@Audited注释。是否可能将此注释重用为泛型类型?

英文:

I need to generify an interface to a class that is annotated to a specific annotation, say having the following entity class

@Entity
@Audited
class Record {
   private Long id;
   private String something;
   private String somethingElse;
}

which is audited with Hibernate Envers, I want to provide a service class that obtains some revision data. With something like

public interface AuditRecordService&lt;E extends Audited&gt; {
//
}

I know I can modify the Record class to extend an abstract class, something like:

@Entity
@Audited
class Record extends RevisionedEntity {
...

This works for me, but I want to avoid unnecessary extension, since it's already annotated with @Audited. Is it possible to reuse this annotation as a generic type?

答案1

得分: 0

Audited 是一个注解,而不是一种类型。注解不能被扩展,因此 AuditRecordService&lt;E extends Audited&gt; 的唯一可能实现将是 AuditRecordService&lt;Audited&gt;,这将毫无用处。

英文:

Audited is an annotation, not a type. Annotations cannot be extended, so the only possible implementation of AuditRecordService&lt;E extends Audited&gt; would be AuditRecordService&lt;Audited&gt;, which would be pretty useless.

huangapple
  • 本文由 发表于 2023年2月14日 04:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定