What is the Custom Annotation?
An annotation is a form of metadata that provides additional information about a program. It can influence how the program is compiled, run, or processed by tools and frameworks.
(metadata: Describe or provide info about
program elements like classes, methods, variables..)
Literally, a custom annotation
refers to creating your own annotaion in Java!
How can we make Custom Anotation?
1 - Defining
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String value() default "default value";
int count() default 1;
}
I'll explain KeyComponent like @Retention, @Target later
2 - Using
Applying the Annotation that you made.
public class MyService {
@MyAnnotation(value = "Hello! 안녕예진!", count = 3) <- !!
public void myMethod() {
System.out.println("Excuting myMethod");
}
}
Accessing Annotation Values.
public class AnnotationProcessor {
public static void main(String[] args) throws Exception {
Method method = MyService.class.getMethod("myMethod");
if (method.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class)
//Print annotation values
System.out.println("value: " + annotation.value());
System.out.println("Count: " + annotation.count());
}
}
}
Key Components of Annotations
@Retention
specifies how long the annotation is retained!
RetentionPolicy.SOURCE
:
Discarded during compliation.RetentionPolicy.CLASS
:
Retained in the class file but not available at runtime.RetentionPolicy.RUNTIME
:
Available at runtime via reflection.
@Target
specifies where the annotation can be applied.
ElementType.TYPE
: Classes, interfaces, or enums.ElementType.METHOD
: Methods.ElementType.FIELD
: Fileds.
Others include PARAMETER, CONSTRUCTOR, etc.
Attributes
Attributes in annotation are defined like methods inside the @interface.
All attributes in a custom annotation require a value unless a default is provided using default keyword.
All attributes must have a return type (String,int ..)
All attributes can't have parameters.
When I can use Custom Anotation.
There is a lot. I'll show you one.
1 - Defining
@Constraint(validateBy = NameValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ValidName {
String message() dafault "Not found username";
Class<?>[] groups() default {};
Class<? extends Payload>[] pay() default {};
}
@Constraint is an annotation used in the Bean Validation API. It is applied to define custom validation annotations.
(validateBy = NameValidator.class) means that
the NameValidator class contains the logic to validate the annotated element.
2 - Using
Applying the Annotation that you made.
public class NameValidator implements ConstraintValidator<ValidName, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return value != ull && value.matches("^[가-힣a-zA-Z]+$"); //permit only Kor or Eng
}
}
public class UserRequest{
@ValidName
private String name;
}
By using annotations, it became possible to user only Korean and English for names.
Like these, It must improve your code readability and be reusable logic!
Top comments (0)