I will introduce a sample program that creates your own annotation in Java and gets the value of the annotation.
C:\>java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
C:\>
This is a sample of self-made annotation. Here, the members have "name" and "value".
MyAnnotation.java
package test01;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) // (1) @Control how much annotation information is retained in Retantion
@Target({ // (2) @Target Controls what type of element the annotation can be added to
ElementType.TYPE,
ElementType.FIELD,
ElementType.CONSTRUCTOR,
ElementType.METHOD
})
public @interface MyAnnotation { // (3)Create MyAnnotation
String name();
int value() default 0;
}
Specify to what stage the annotation information is retained by @ Retention
in (1).
The following can be mainly set for @Retention
.
value | Description |
---|---|
RetentionPolicy.SOURCE | Annotations are retained only at the source level and are ignored by the compiler. |
RetentionPolicy.CLASS | Annotations are retained by the compiler at compile time, but are ignored by the Java Virtual Machine (JVM). |
RetentionPolicy.RUNTIME | Annotations are retained by the JVM and can be used in the runtime environment. |
Here, specify RetentionPolicy.RUNTIME
.
Specify the location where the annotation can be applied with @ Target
in (2).
The following can be mainly set for @ Target
.
value | Description |
---|---|
ElementType.TYPE | Make it applicable to classes, interfaces, annotations, and enum types. |
ElementType.FIELD | Make it applicable to the field. |
ElementType.CONSTRUCTOR | Make it applicable to the constructor. |
ElementType.METHOD | Make it applicable to the method. |
Here, specify ʻElementType.TYPE, ʻElementType.FIELD
, ʻElementType.CONSTRUCTOR, and ʻElementType.METHOD
in @ Target
.
In (3), create an annotation with the name MyAnnotation
. Set the members to name
and value
.
Add your own MyAnnotation
to the classes, fields, constructors and methods of the Sample
class.
Sample.java
package test01;
@MyAnnotation(name="class", value=100) // (1)Grant MyAnnotation to class
public class Sample {
@MyAnnotation(name="field", value=200) // (2)Grant MyAnnotation to field
private String name;
@MyAnnotation(name="constructor", value=300) // (3)Add MyAnnotation to constructor
public Sample() {}
@MyAnnotation(name="method", value=400) // (4)Give method MyAnnotation
public void execute() {
System.out.println("execute");
}
}
(1) Add your own annotation (MyAnnotation
) to the class.
(2) Add your own annotation (MyAnnotation
) to the field.
(3) Add your own annotation (MyAnnotation
) to the constructor.
(4) Add your own annotation (MyAnnotation
) to the method.
Main.java
package test01;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, SecurityException {
// (1)Get the annotation (MyAnnotation) attached to the class
Class<?> clazz = Class.forName("test01.Sample");
MyAnnotation annoClass = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class);
System.out.println("class annotation : name=" + annoClass.name() + ", value=" + annoClass.value());
// (2)Get the annotation (MyAnnotation) attached to the field
Field field = clazz.getDeclaredField("name");
MyAnnotation annoField = (MyAnnotation) field.getAnnotation(MyAnnotation.class);
System.out.println("field annotation : name=" + annoField.name() + ", value=" + annoField.value());
// (3)Get the annotation (MyAnnotation) attached to the constructor
Constructor<?> cons = clazz.getConstructor();
MyAnnotation annoCons = (MyAnnotation) cons.getAnnotation(MyAnnotation.class);
System.out.println("constructor annotation : name=" + annoCons.name() + ", value=" + annoCons.value());
// (4)Get the annotation (MyAnnotation) attached to the method
Method method = clazz.getMethod("execute");
MyAnnotation annoMethod = (MyAnnotation) method.getAnnotation(MyAnnotation.class);
System.out.println("method annotation : name=" + annoMethod.name() + ", value=" + annoMethod.value());
}
}
(1) Acquires and displays the contents of the annotation (MyAnnotation
) attached to the Sample
class.
(2) Acquires and displays the contents of the annotation (MyAnnotation
) attached to the field name
of the Sample
class.
(3) Acquires and displays the contents of the annotation (MyAnnotation
) attached to the constructor of the Sample
class.
(4) Acquires and displays the contents of the annotation (MyAnnotation
) attached to the ʻexecute method of the
Sample` class.
Lesson: Annotations (The Java™ Tutorials < Learning the Java Language)
that's all
Recommended Posts