preface

I believe that many Java users are familiar with annotations, and the application of annotations in various popular frameworks in the Java ecosystem is dizzying. However, many beginners are not familiar with annotations. The purpose of this article is to use popular language to help those who are not familiar with annotations. Let the concept of annotations form an initial impression in your mind for a better understanding in subsequent study.

What are annotations

In short, annotations can be understood as comments, but they are fundamentally different from comments, which are often used to remind the programmer what the code is for and why it was written. And the role of the annotation is more of a compiler is a reminder that tells the compiler that the place is identified, then the compiler to carry on the corresponding processing, such as we often see @ Override is to tell the compiler that the method is to rewrite the superclass method, the compiler can be processed, such as found that do not match the method signature warning and so on. Of course, annotations are not limited to this, we can customize our own annotations, through the annotations we define, to affect the execution logic of the program. Since the annotation’s identity can be retained at runtime, meaning that the annotation’s information remains on the modified object while the program is running, the code can dynamically retrieve this information while the program is running for subsequent processing.

2. Custom annotations

Now that we’ve outlined the use of annotations, let’s use a simple example to see how to customize a simple annotation.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ChangeYourMind {
    boolean isChange(a) default false;
}

Copy the code

@Retention(retentionPolicy.runtime) This is a meta-annotation provided by Java that is intended for the compiler to read, indicating that my custom annotations will be retained at RUNTIME.

@target (ElementType.METHOD) this means I can only put this annotation on methods.

It defines an isChange() property in the annotation, and some of you might be wondering, isn’t that a method? Although this looks like a method, it actually represents a property, and we can call the method directly to get the value of the property.

Now that our custom annotations are defined, it’s time to use them.

As we said, the annotations we define are modifications to methods, so let’s define a test class to see how our custom annotations, ChangeYourMind, affect the method’s execution. This annotation is an amazing name, and when added, it will change our original execution logic.

public class Main {
    @ChangeYourMind(isChange = true)
    public static void  testMethod1(a) throws NoSuchMethodException {
        Method method = Main.class.getMethod("testMethod1");
        ChangeYourMind annotation = method.getAnnotation(ChangeYourMind.class);
        if(annotation ! =null) {
            boolean change = annotation.isChange();
            if (change) {
                System.out.println("I change my mind");
            } else {
                System.out.println("I do what i want"); }}else {
            System.out.println("I don't know what to do!"); }}@ChangeYourMind(isChange = false)
    public static void  testMethod2(a) throws NoSuchMethodException {
        Method method = Main.class.getMethod("testMethod2");
        ChangeYourMind annotation = method.getAnnotation(ChangeYourMind.class);
        if(annotation ! =null) {
            boolean change = annotation.isChange();
            if (change) {
                System.out.println("I change my mind");
            } else {
                System.out.println("I do what i want"); }}else {
            System.out.println("I don't know what to do!"); }}public static void  testMethod3(a) throws NoSuchMethodException {
        Method method = Main.class.getMethod("testMethod3");
        ChangeYourMind annotation = method.getAnnotation(ChangeYourMind.class);
        if(annotation ! =null) {
            boolean change = annotation.isChange();
            if (change) {
                System.out.println("I change my mind");
            } else {
                System.out.println("I do what i want"); }}else {
            System.out.println("I don't know what to do!"); }}public static void main(String[] args) throws NoSuchMethodException { testMethod1(); testMethod2(); testMethod3(); }}Copy the code

The output of the program is as follows

I change my mind
I do what i want
I don't know what to do!
Copy the code

In this class we get annotations by reflection and influence the logic of our program based on whether the annotations exist and whether isChange is true. With annotations, we decide whether isChange is true to “change our mind”. Without annotations, our application does not know what to do.

Third, summary

This article uses a small example to introduce the general use of custom annotations. Many middleware, such as SOME ORM frameworks, some authorization frameworks, etc., are used in this way to obtain attributes defined in annotations to simplify development. The implementation of these frameworks may be far more complex than this, but the general framework is still the same, these content needs us to continue to learn, we refueling together!

This article is part of the “Gold Nuggets For Free!” Event, click to view details of the event