Is it possible to pass method parameter to annotation on a method?

℡╲_俬逩灬. 提交于 2020-01-24 11:16:05

问题


Let's say we have this method with annotation:

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

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

Is it possible to do this in java?


回答1:


No. It is not possible.

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



来源:https://stackoverflow.com/questions/25785823/is-it-possible-to-pass-method-parameter-to-annotation-on-a-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!