How can I do Java annotation like @name(“Luke”) with no attribute inside parenthesis?

前端 未结 3 1165
醉梦人生
醉梦人生 2020-12-29 01:57

How I can do custom Java annotation with no attribute name inside parentheses?

I don\'t want this: @annotation_name(att=valor). I just want like in Serv

3条回答
  •  醉话见心
    2020-12-29 02:29

    Quoting Annotations official documentation:

    If there is just one element named value, then the name may be omitted, as in:

    @SuppressWarnings("unchecked")
    void myMethod() { }
    

    This is how this annotation is defined:

    public @interface SuppressWarnings {
      String[] value();
    }
    

    As you can see the documentation isn't entirely right, other attributes are also allowed ("just one element"), see WebServlet - but the one named value is treated differently.

提交回复
热议问题