Create your own Java annotations

What is an annotation?

Ability to add additional information to classes, methods, packages, etc.

Make your own annotation

import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(TYPE)
public @interface TestAnnotation {
    String testValue();
}

@Retention Specifies the range in which annotation information is retained.

--SOURCE Discarded by the compiler. --CLASS Recorded in class files by the compiler, but not read at run time. (Default) --RUNTIME Recorded in class files by the compiler and read at run time.

@Target Specifies where annotations can be applied.

--TYPE class, interface or enum declaration --FIELD field declaration --METHOD method declaration --Declaration of argument of PARAMETER method --CONSTRUCTOR Constructor declaration --LOCAL_VARIABLE Local variable declaration --ʻANNOTATION_TYPE Annotation declaration --PACKAGE Package Declaration --TYPE_PARAMETERtype argument declaration --Where theTYPE_USE type is used --MODULE` module declaration

@interface Define the annotation.

Add your own annotation

@TestAnnotation(testValue = "test value")
public class AnnotatedClass {
}

This time, we are annotating the class.

Output annotation information

public class GetValue {
    public static void main(String[] args) {
        try {
            Class<?> targetClass = Class.forName("AnnotatedClass");
            var testAnnotation = (TestAnnotation) targetClass.getAnnotation(TestAnnotation.class);
            System.out.println(testAnnotation.testValue());
        } catch (Exception e) {
            //Handling of exceptions
        }
    }
}

When executed, test value is output.

Recommended Posts

Create your own Java annotations
Handle your own annotations in Java
Create your own Solr Function Query
Make your own persistence FW (Java)
Create your own encode for String.getBytes ()
Java: Try implementing your own comma-separated formatter
Understand java interface in your own way
Utilization of Talend component (5) Create your own component
How to create your own annotation in Java and get the value
Create your own Utility with Thymeleaf with Spring Boot
[Java] Create a filter
Make your own pomodoro
Review Java annotations now
Create JSON in Java
[Java] Sort ArrayList with elements of your own class
How to read your own YAML file (*****. Yml) in Java
Make your own Rails validate
Create hyperlinks in Java PowerPoint
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create Azure Functions in Java
Make your own simple server in Java and understand HTTP
Java: Start WAS with Docker and deploy your own application
Make your own Elasticsearch plugin
How to create your own Controller corresponding to / error with Spring Boot
Create an immutable class with JAVA
Make your own sampler with JMeter
Get Java Silver on your commute!
Use LocationAwareLogger for your own Logger
[Java] How to create a folder
[Java & Kotlin] Create multiple selectable RecyclerView
How to create your own headless API using Liferay's REST Builder (Part 3)
How to create your own headless API using Liferay's REST Builder (Part 2)
Create your own dialect to convert line feed code to line feed tag in Thymeleaf3
How to create your own headless API using Liferay's REST Builder (Part 4)
How to create your own headless API using Liferay's REST Builder (Part 1)