是否可以将方法参数传递给方法的注释? [英] Is it possible to pass method parameter to annotation on a method?

查看:353
本文介绍了是否可以将方法参数传递给方法的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我们有这种带有注释的方法:

Let's say we have this method with annotation:

@Cached(key="search" + id)
public static List<String> search(String id) {
    //...
}

我想将参数"id"传递给@Cached批注的键.

I want to pass parameter "id" to the @Cached annotation's key.

是否可以在Java中执行此操作?

Is it possible to do this in java?

推荐答案

否.这是不可能的.

Java注释的属性必须是Java语言规范(

Attributes of Java Annotations must be constant expressions as defined by the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28)

我认为您的问题之前已经得到回答: 摆脱";批注属性的值必须是一个常量表达式消息

I think your question has been answered before: Get rid of "The value for annotation attribute must be a constant expression" message

这篇关于是否可以将方法参数传递给方法的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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