Home > Java > javaTutorial > body text

How to apply custom annotations in Java code?

王林
Release: 2024-05-02 09:09:02
Original
321 people have browsed it

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.

How to apply custom annotations in Java code?

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";
}
Copy after login
  • @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;
}
Copy after login

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"
Copy after login

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 {
}
Copy after login

Apply annotations and extend annotations

public class MyClass {
    @Loggable
    public void myMethod() {
        // ...
    }
}
Copy after login

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");
        }
    }
}
Copy after login

When run, the program prints the following output:

Method is annotated with @Loggable
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!