Why Is the Java Annotation Ignored in Code?
Java annotations, added in Java 1.5, can add extra information about a class, method or field. This information can be used in various ways, such as detailing how a program should execute, or what methods should be avoided by other developers. Depending on how the annotations were originally coded, this information can be available only in the original source code, when working with the compiled class directly or at runtime via reflection.
-
What Are Annotations?
-
Simply put, an annotation is a means of adding additional information -- also known as metadata -- to your program about the annotated item. This information can be as simple as a note detailing authorship or something as complex as associating a field with a database column. Annotations can be handled in one of three ways: by staying solely in the original source code, where they're generally only visible to other developers; by being compiled into the bytecode, where they are not available for runtime processing, but are available to programs accessing the class files; and being compiled so the information is loaded at runtime so the running program can access it.
Why Require Annotation Details at Runtime?
-
Because a developer can indicate additional information about an item, having this information available at runtime can allow a program to act differently for that item without necessarily requiring that information to compile (or even function). For example, consider an annotation that associates a field with a database column; perhaps it would look something like this:
@DBField("FIRSTNAME")
private String firstName;
During execution, the program could read that information and realize it should fill that field based on the contents of that column (granted, it would also need table and row information). This would also allow the user to write, for example, a generic data handling routine that relies on annotations and doesn't need much specialization for each class.
-
How Java Annotations Are Ignored
-
As stated previously, there are three ways for the compiler to handle an annotation it comes across in code: by scrapping it (and thereby leaving it only in the code), by compiling it into the bytecode and by compiling it into the bytecode with the information needed to load it at runtime. The second option -- compiling it into the bytecode (without loading it at runtime) -- is the default for all annotations, so unless otherwise specified, an annotation will not be available at runtime. However, specifying this is extremely simple if you have access to the annotation's source code. Simply add an additional annotation to the annotation definition:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation...
Using the Annotation at Runtime
-
Once you've ensured the annotation is available at runtime, using it is extremely simple. Assuming you have an annotation named "Author" with a string value, you can use the following code to make sure you do have runtime access to the annotation:
final Author author = MyClass.class.getAnnotation(Author.class);
System.out.println(author.value());
In this case, MyClass would be declared as follows:
@Author("John Doe")
public class MyClass ...
When you execute the lines above, you should get the value for the annotation ("John Doe," in this case) printed to the console.
-
Resources
- Photo Credit Comstock/Comstock/Getty Images