Understanding Java Annotations: Types, Benefits, Custom Creation, and Runtime Parsing
This article explains the purpose and advantages of Java annotations, categorizes built‑in, third‑party and custom annotations, describes meta‑annotations, demonstrates how to define and apply custom annotations, and shows how to parse them at runtime using reflection, complete with code examples.
Java annotations are extensively used in modern development to help developers read framework code, replace verbose configuration with concise metadata, and make programs more readable; mastering them, especially custom annotations, can significantly improve a developer's credibility.
Common built‑in annotations such as @Override , @Deprecated and @SuppressWarnings are introduced, with examples showing how they affect compilation and IDE warnings.
JDK annotations are divided into three categories (source, class, runtime) and illustrated with a diagram (omitted). Sample code defines an interface people and a class Child that implements it, demonstrating the use of @Override and @Deprecated on methods.
Third‑party annotations are mentioned briefly, accompanied by an image (omitted) that lists popular libraries.
Classification of annotations is presented:
By lifecycle: source, class, runtime.
By origin: JDK, third‑party, custom.
Meta‑annotations that describe other annotations.
Creating a custom annotation requires four meta‑annotations ( @Target , @Retention , @Inherited , @Documented ) and a definition using @interface . The article shows the syntax and constraints for members, default values, and marker annotations.
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String desc();
String author();
int age() default 18;
}Usage of the custom annotation is demonstrated on a method:
@Description(desc="i am Color", author="boy", age=18)
public String Color() {
return "red";
}The article then explains how to parse annotations at runtime using reflection. It shows loading a class, checking for the presence of Description , retrieving its values, and printing them.
public class ParseAnn {
public static void main(String[] args) {
try {
Class c = Class.forName("com.test.Child");
boolean isExist = c.isAnnotationPresent(Description.class);
if (isExist) {
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.value());
}
Method[] ms = c.getMethods();
for (Method m : ms) {
if (m.isAnnotationPresent(Description.class)) {
Description d1 = m.getAnnotation(Description.class);
System.out.println(d1.value());
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}The output confirms successful parsing of both class‑level and method‑level annotations. The article concludes by encouraging readers to experiment with changing the @Retention policy (SOURCE, CLASS, RUNTIME) to observe different behaviors.
Conclusion : By understanding built‑in, third‑party, and custom annotations, as well as meta‑annotations and reflection‑based parsing, developers can write cleaner, more maintainable Java code and leverage annotations for advanced runtime logic.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.