简体   繁体   English

Java注解:元素声明为方法,但值设置为属性

[英]Java annotation: elements declared as method but value set as attribute

When we create a custom annotation, we declare elements as methods and later set values as if they were attributes. 创建自定义批注时,我们将元素声明为方法,然后将值设置为属性。

For example, here we have declared a custom annotation ComponentType with elements name() and description() that look like methods. 例如,在这里我们声明了一个自定义注释ComponentType ,其元素name()description()看起来像方法。

public @interface ComponentType {
    String name();// declared as method
    String description();
}

When the annotation is used, they look like the below: 使用注释时,它们如下所示:

@ComponentType(name = "userContainer", // value looks like an attribute
               description = "a user container")
public class UserEntity { }

My question is : Why doesn't Java allow to declaring elements as attributes, like this? 我的问题是 :Java为什么不允许这样将元素声明为属性?

public @interface ComponentType {
    String name; // Compilation Error
    String description;
}

If the properties of an annotation weren't defined as abstract methods in an interface, they would have been members. 如果注释的属性未在接口中定义为抽象方法,则它们将成为成员。 Something like: 就像是:

public @interface ComponentType {
    String name;
    String description;
}

However, all the members in an interface are implicitly final (and static ) and the above code does not compile, because name and description aren't initialized. 但是,接口中的所有成员都是隐式的final (和static ),并且上面的代码无法编译,因为namedescription未初始化。

But if they were actually initialized with some values: 但是,如果它们实际上是使用一些值初始化的:

public @interface ComponentType {
    String name = "name";
    String description = "description";
}

then snippets like the following one wouldn't have been possible: 则不可能出现以下片段:

@ComponentType(
  name = "userContainer" //cannot assign a value to a final variable 
, description = "a user container")

My observation is: 我的观察是:

Java consider annotations as special type of interface so: Java将注释视为接口的特殊类型,因此:

  1. Like interface we can declare only final attributes in an annotation: 像接口一样,我们只能在注释中声明最终属性:

     String appName = "test application";//final attribute, never reset value 
  2. Annotation may contains only abstract methods(a method that is declared without an implementation). 注释只能包含抽象方法(声明的方法无需实现)。

     public @interface ComponentType { String name();// declared as abstract method 
  3. When we annotated elements(eg class, method, attribute) by annotation we need to set return value of those abstract methods, which looks like attribute but actually acts as an implementation. 当我们通过注释对元素(例如类,方法,属性)进行注释时,我们需要设置这些抽象方法的返回值,这些返回方法看起来像属性,但实际上是一种实现。

     @ComponentType(name = "userContainer"//set return value of method name() 
  4. We can use the values we set during annotated elements(eg class, method, attribute) by simply calling abstract methods of annotation. 我们可以通过简单地调用批注的抽象方法来使用在批注元素(例如,类,方法,属性)中设置的值。

     Annotation annotation = annotatedClassObject.getAnnotation(ComponentType.class); ComponentType componentType = (ComponentType) annotation; String someName = componentType.name(); //returns value set during annotating 

So like as interface , 就像界面一样

  • Annotation never support to declare any non-final attributes. 注释永远不支持声明任何非最终属性。
  • Annotation may contains some abstract methods and we need to set return value of abstract method during annotated elements(eg class, method, attribute). 注释可能包含一些抽象方法 ,我们需要在被注释的元素(例如类,方法,属性)期间设置抽象方法的返回值。

Expecting More Feedback / Answer 期待更多反馈/答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM