力注释类包含注释字段 [英] Force annotated Class to contain annotated Field

查看:157
本文介绍了力注释类包含注释字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能迫使(在编译时)的注解类有一个带注释字段?

我有这样的注解:

  @Target(值= ElementType.TYPE)
@interface MyClass的{}@Target(值= ElementType.FIELD)
@interface MyField的{}

现在我想,像这样一类编译失败:

  @MyClass
一流的客户{}

而这应该工作:

  @MyClass
一流的客户{
   @MyField
   字符串文本;
}


解决方案

是的,使用的容易(注释处理工具)。

但对于这样的简单任务,这将是一个矫枉过正。

您可以进行一些监听器被触发应用程序启动和检查这一要求。

最后,您可以通过假定合理的默认值避免这种情况 - 即如果类注释,然后再考虑注释,以及各个领域,为默认值

Is it possible to force (at compile-time) an annotated Class to have a an annotated Field?

I have this annotations:

@Target(value = ElementType.TYPE)
@interface MyClass {}

@Target(value = ElementType.FIELD)
@interface MyField {}

and now I would like that the compilation of a Class like this fails:

@MyClass
class Customer {

}

whereas this should work:

@MyClass
class Customer {
   @MyField
   String text;
}

解决方案

Yes, using apt (annotation processing tool).

But for such simply tasks it will be an overkill.

You can make some listener that is triggered on application startup and checks this requirement.

Finally, you can avoid this by assuming reasonable defaults - i.e. if the class is annotated, then consider all fields annotated as well, with the default values.

这篇关于力注释类包含注释字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆