Lesson 23Annotations and Reflection

In general, metadata is data about your data. In the context of DBMSes, metadata can be information describing the way you store data, such as the table and field names or primary keys. Program code metadata is data about your code. Any Java class has its metadata embedded, and you can write a program that “asks” another class, “What methods do you have?” or similar questions about class fields, constructors, and ancestors.

You can use annotations to include metadata about your code. There are a number of predefined annotations (for example, @Override and @SuppressWarning). The Java annotation model enables you to add custom metadata anywhere in your code. You can apply custom annotations to a class, a method, or a variable—just specify allowed targets when the annotation is being defined. Java annotations start with the @ sign and may optionally have one or more parameters. Some of the annotations are built into Java SE and are used by the javac compiler, but most of them are consumed by some kind of processing program or tool.

The subject of Java reflection doesn’t require the use of annotations; reflection is used widely in various areas of Java development. But because this subject has to be covered before you can proceed with annotation processing, I decided to cover both topics in the same lesson.

Javadoc Annotations

If you’ve ever looked at the source code of any Java class, you can easily identify Javadoc-style comments for classes, ...

Get Java Programming 24-Hour Trainer, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.