-If @interface is added, it will be defined as an annotation.
--Declaration of parameters when using annotations
TestAnnotation.java
public @interface TestAnnotation {
String param1();
String param2() default "defaultValue";
}
UserAnnotation.java
@TestAnnotation (param1="30", param2="100")
public UserAnnotation() {}
--Refer to using reflection.
Reference class
Class<?> c = UserAnnotation.class;
for (Annotation a : c.getDeclaredAnnotations()) {
System.out.println(a);
}
Console output
@com.example.demo.com.annotation.TestAnnotation(param2=100, param1=30)
TestAnnotation
@Documented
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation{}
--Annotation attached to an annotation.
--Of these, the one prepared on the java side is called the standard meta annotation.
ex. Documented, Target, Retention, Ingerited
@Documented --Whether to include annotation description in Javadoc.
@Target --Define where to place annotations.
type | Description |
---|---|
TYPE | Class interface, enum, annotation |
FIELD | field |
METHOD | Method |
ANNOTATION_TYPE | Annotation type declaration |
@Retantion ――How far will the annotation information be used?
type | Description |
---|---|
SOURCE | It is retained only on the source and deleted when the class file is created. |
CLASS | It can be retained in a class file, but it cannot be referenced when the application is executed. |
RUNTIME | Can be used even when the application is executed. This is necessary if you want to refer to annotations in reflection. |
@Inherited --Add annotation information to the subclass that declares the class.
@Documented behavior https://qiita.com/opengl-8080/items/1cc996d9e8bb5c811567
What is @meta-annotation? https://www.techscore.com/tech/Java/JavaSE/JavaLanguage/7-3/
Recommended Posts