如何使用多种枚举类型编写注释?

发布于 2024-08-18 18:05:23 字数 766 浏览 1 评论 0原文

我正在尝试编写一些注释来帮助将测试用例方法与各种元数据关联起来。到目前为止,我的注释包括 BugFix、UseCase 和 Requirement。我的最终目标是编写一个注释处理器来显示哪些测试用例与各个错误修复、用例和需求相关。

当我刚刚为自己的项目实现此功能时,我为每个测试关联类别创建了特定于我的项目的枚举。例如,我有 ProjectUseCase 枚举,我可以将其作为值传递给测试方法上的 UseCase 注释。这使得在一个地方添加额外的用例或修改用例变得很容易。

现在,我想让我工作中的其他开发人员可以使用此代码。当然,他们将致力于其他具有不同用例、要求和错误修复的项目。

以下是我遇到的问题:

  1. 枚举无法扩展,因此我无法拥有其他人可以为自己的项目扩展的基本用例枚举。
  2. 注释属性的值必须是常量。这阻止我在枚举上使用接口标记 (TestAssociation),并使用 TestAssociation 接口作为注释的值。另外,这会阻止我在注释中使用 String 值,并在使用注释时传入枚举名称,例如:@UseCase(ProjectUseCase.GENERAL.name())。

据我所知,这让我只能使用原始字符串作为值,这剥夺了我的类型安全性和快速重构的能力。使用常量字符串类代替枚举似乎是处理此问题的最佳方法,因为每个开发人员都可以使用自己的类。

我唯一能想到的另一件事是引用 Enum 类(或常量字符串类),并且不将该类包含在 jar 中,将其留给用户实现。

有什么建议或解决方法吗?

我还想知道是否有任何项目已经提供类似的功能。

I'm attempting to write some annotations to help associatate test case methods with various metadata. My annotations so far include BugFix, UseCase, and Requirement. My end goal is to write an annotations processor to display which test cases are associated with individual bug fixes, use cases, and requirements.

When I was just implementing this for my own project, I created enums specific for my project for each test association category. For example, I had ProjectUseCase enums, which I could pass as the value to the UseCase annotation on my test methods. This made it easy to add additional use cases or modify use cases in a single place.

Now, I'd like to make this code available for other developers at my work. Of course, they will be working on other projects, with different use cases, requirements, and bug fixes.

Here are the problems I'm running into:

  1. Enum cannot be extended, so I cannot have a base UseCase enum which others can extend for their own projects.
  2. Values for annotation properties have to be constants. This prevents me from using an interface marker (TestAssociation) on my enums, and using the TestAssociation interface for the values of my annotation. Also, this prevents me from using String values in my annotation, and passing in the enum name when I use the annotation, such as: @UseCase(ProjectUseCase.GENERAL.name()).

From what I can tell, this leaves me with just using raw Strings for the values, which deprives me of type safety and the ability to quickly refactor. Using classes of constant Strings in place of the enums seems to be the best way to handle this, as every developer can use their own.

The only other thing I can think of is to reference an Enum class (or a class of constant Strings), and not include the class in the jar, leaving it for the users to implement.

Any suggestions or workarounds?

I'd also like to know if there are any projects out there already providing similar functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-08-25 18:05:23

是的。

解决这个问题的唯一好方法是允许任意用户定义的注释,这样如果我的枚举是 FooEnum,我就可以定义使用它的 FooAnnotation。您的框架可以通过查看本身是否使用@UseCase(或您喜欢的任何内容)进行元注释来将FooAnnotation识别为它正在寻找的注释!

Yup.

The only good way out of this is to allow for arbitrary user-defined annotations, so that if my enum is FooEnum, I can define FooAnnotation that uses it. Your framework can recognize FooAnnotation as being the annotation it's looking for by seeing whether it itself is meta-annotated with @UseCase (or whatever you please)!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文