Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for annotation type elements?

Tags:

java

How do I read/understand the following statement in Java?

Class<?>[] groups() default {};

Class < ? extends Payload>[] payload() default {};

I think I can understand them individually but I don't know if I get good sense of what it means in its entirety.

Individually:

  1. Class<?> means any class and Class< ? extends Payload> means any class that extends the Payload class
  2. [ ] seems to refer to any array of classes. Is that correct?
  3. groups() and payload() are method names.
  4. default{} Use this when there is no implementation?

I am really not sure how to understand the above statements? Any help would be very much appreciated.

like image 593
Aaron Avatar asked Nov 22 '18 18:11

Aaron


People also ask

What is an annotation type?

The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign ( @ ) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.

What is @interface annotation in Java?

@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }

What is annotation method?

An annotation is a construct associated with Java source code elements such as classes, methods, and variables. Annotations provide information to a program at compile time or at runtime based on which the program can take further action.


1 Answers

default {} --> Use this when there is no implementation?

In an Annotation definition you can specify a default value for a parameter. The {} after default is an array literal for an empty array. You could also set it to some non empty value. This also works for other objects too. eg the Data annotation from lombok where a default string is declared:

public @interface Data {
    String staticConstructor() default "";
}

Class means any class and "Class< ? extends Payload>" means any class that extends the Payload class

Yes

[ ] --> Seems to refer to any array of classes. Is that correct?

Yes

groups() and payload() are method names.

They are the name of the parameter used in annotation declaration, as well as the name of the getter methods for those values. eg you could define:

@Data(staticConstructor = "of") class Foobar {}

And later you could get the value by using the getter method created:

Data dataAnnotation = Foobar.class.getAnnotation(Data.class);
String staticConstructor = dataAnnotation.staticConstructor();

Note that you will not actually be able to retrieve the value for an annotation in runtime unless the annotation definition is also annotated with @Retention(RetentionPolicy.RUNTIME)

like image 168
flakes Avatar answered Nov 14 '22 06:11

flakes