在JAVA中的注释内调用方法 [英] Call method inside annotation in JAVA

查看:28
本文介绍了在JAVA中的注释内调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以调用一个在注解中返回字符串的方法吗?
如果是这样,请指导我如何实现这一目标?

Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?

我试过这样,但这对我不起作用.

I tried like this but this doesn't work for me.

@Description(value = Resource.getWord("key"))

推荐答案

注解只需要编译时常量(因为它们可能在编译时使用),因此您不能在定义中进行任何计算,因为它们在编译期间是未知的编译时间.

An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.

允许的常量类型是(取自 java-annotation-成员):

Allowed constant types are (taken from java-annotation-members):

  • 原始
  • 字符串
  • 枚举
  • 另一个注解
  • 上述任何一项的数组

针对您的情况的可能解决方案:

据我了解,您希望本地化 @Description 内容.由于无论如何这只是为了向其他开发人员公开,因此在我看来,您可以安全地使用英语.本地化是针对最终用户的,而不是针对开发人员的.

As I understand you would like to localize the @Description content. As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.

这篇关于在JAVA中的注释内调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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