开发者

How to write annotations using multiple enum types?

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开发者_JS百科 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.


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)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜