Utilize original marker annotation

Purpose

--Create your own marker annotation. --Add a condition depending on the presence or absence of annotations given to class, interface, and method.

How to Use

1. Create your own marker annotation

Reference article: Slightly special Java annotation

Specify the target of annotation: Target

--Target class / interface and method. Select ** TYPE, METHOD ** from java.lang.annotation.ElementType To do. -When selecting multiple ElementType **, use the arc {} and separate them with commas ,.

SampleAnnotation.java


/**
 *Annotations that can be added to class / interface and method
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@interface SampleAnnotation {

}

Specify when to load annotations: Retention

--Specify to be loaded into the VM at run time. Select ** RUNTIME ** from java.lang.annotation.RetentionPolicy.

SampleAnnotation.java


/**
 *Annotation representing VM loading at runtime
 */
@Retention(RetentionPolicy.RUNTIME)
@interface SampleAnnotation {
	
}

2. Add conditions depending on whether or not annotation is added

Marker annotation to be used (annotation created in 1.)

[Usage example] The class interface or method to which this annotation @IgnoreSession is attached is ** Annotation for a marker indicating that it is not a process that handles a session **.

IgnoreSession.java


/**
 *Annotation indicating that the process does not handle sessions
 *Granted to processes that do not handle sessions
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreSession {

}

Determination process

Describe the process to determine whether or not this annotation @ IgnoreSession has been added.

Verification of class / interface

bean: represents the substance of class / interface

  1. Normal case

    
    //Verification for class / interface
    if (targetClass.isAnnotationPresent(IgnoreSession.class)
    
    	//Processing when class / interface does not handle session
    }
    
  2. When using fw such as spring In the springAOP process, the entity of the bean to be verified may be the Proxy class. (2) Before "Verify class / interface", (1) "Verify whether it is a Proxy class for bean" is required.

Reference article: I was addicted to metaprogramming using beans with Spring AOP applied.

```java:Verify class / interface

//(1) Verify whether it is a Proxy class for the bean
Class<?> targetClass = null;
if (AopUtils.isAopProxy(bean)) {
   	targetClass = AopUtils.getTargetClass(bean);
} else {
   	targetClass = bean.getClass();
}

//② Verification for class / interface
if (targetClass.isAnnotationPresent(IgnoreSession.class)

	//Processing when class / interface does not handle session
}
```

method validation

method: Represents the substance of method

Validate method



//Verification for method
if (method.isAnnotationPresent(IgnoreSession.class)) {

    //Processing when the method does not handle sessions
}

Reference summary

-Basic knowledge about Java annotation -Slightly special Java annotation -Matters that I was addicted to in metaprogramming using beans to which Spring AOP was applied

Recommended Posts

Utilize original marker annotation
[Java] Creation of original annotation
Add processing to original annotation to Jackson