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

查看:1152
本文介绍了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"))

推荐答案

注释仅采用编译时常数(因为它们可能在编译时使用),因此您无法在定义内进行任何计算,因为它们不会在编译时就知道

A 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 would not been known during 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 save to simply use English, in my opinion. Localization is for the end user, not the developer.

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

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