Custom Annotation Guide To create custom annotations in Java, use the @interface keyword. Use custom annotations to specify the retention time and application location of the annotation through @Retention and @Target. Use reflection to retrieve the annotation value, obtain the field's annotation through getDeclaredField, and obtain the annotation object using the getAnnotation method. In practice, custom annotations can be used to mark methods that require logging, and the annotations can be checked at runtime through reflection.
Apply custom annotations in Java code
Introduction
Customization Annotations are a powerful tool for adding metadata in Java code. They allow you to add additional information to different parts of your program for later processing or analysis. This article will guide you on how to create, use, and process custom annotations in Java code.
Create a custom annotation
To create a custom annotation, you need to use the @interface
keyword. The following is an example of creating a custom annotation named @MyAnnotation
:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MyAnnotation { String value() default "default"; }
@Retention(RetentionPolicy.RUNTIME)
: Specifies that the annotation should be used at runtime Available. This means that the annotation can be accessed in reflection. @Target(ElementType.FIELD)
: Specifies that the annotation can only be applied to fields. Using custom annotations
To use a custom annotation, add it on the field to which you want to append metadata. Here's how to use the @MyAnnotation
annotation field:
public class MyClass { @MyAnnotation("custom value") private String myField; }
Handling custom annotations
You can use reflection to handle custom annotations. The following is how to retrieve the annotation value:
Class myClass = MyClass.class; Field myField = myClass.getDeclaredField("myField"); MyAnnotation annotation = myField.getAnnotation(MyAnnotation.class); String value = annotation.value(); System.out.println(value); // 输出:"custom value"
Practical case
The following is a practical case showing how to use custom annotations to mark methods that require logging:
Create custom annotations
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Loggable { }
Apply annotations and extend annotations
public class MyClass { @Loggable public void myMethod() { // ... } }
Process annotations
import java.lang.reflect.Method; public class AnnotationProcessor { public static void main(String[] args) throws Exception { Class myClass = MyClass.class; Method myMethod = myClass.getDeclaredMethod("myMethod"); Loggable annotation = myMethod.getAnnotation(Loggable.class); if (annotation != null) { System.out.println("Method is annotated with @Loggable"); } } }
When run, the program prints the following output:
Method is annotated with @Loggable
The above is the detailed content of How to apply custom annotations in Java code?. For more information, please follow other related articles on the PHP Chinese website!