Yesterday, I was teaching my Sunday online Java class. One of the topics was Java annotations. I was explaining that a program’s metadata is the data about your code. Any Java class contains its metadata, and you can write a program that “asks” another class, something like “What methods do you have?”. After explaining how to declare a custom annotation @MyJDBCExecutor
I gave a code snippet that may use it:
@MyJDBCExecutor (sqlStatement="Select * from Employee”)
public List getEmployees(){
// The code to get the the data from DBMS goes here,
// result set goes in ArrayList, which is returned to the
// caller of the method getEmployees()
…
return myEmployeeList;
}
One student asked me a question, “How you can access the value of the sqlStatement from inside the method getEmployees()?” This was a clear signal that I didn’t do a good job of explaining what metadata is for. I started to think of an example from the real world illustrating metadata, and this is what I came up with.
When you check in your luggage at the airport, they put this sticker tape that includes the luggage ID, destination, the airline carrier and the flight number. This can be considered a metadata about your piece of luggage. It’s meant to provide some additional information about your bag and is supposed to be read and interpreted by those who are handling the luggage, not by the luggage itself.
If there is a pair of jeans inside your luggage it could care less what the sticker on the bag reads. On the same note, the code from inside the method getEmployee()
is not supposed to read and interpret the data that was written on its “sticker” (a.k.a. annotation).
Software developers who create custom annotations have to create another piece of software (annotation processors) that would introspect your class, find and interpret all annotations that are present and perform the appropriate action. In the ancient pre-Java6 times, there was a special tool for that called APT. Now it’s deprecated because its functionality was incorporated inside the javac compiler itself. But this is another story. I hope my analogy with luggage stickers will help junior Java developers in understanding of the very important concept of metadata.
If you are teaching or studying Java, feel free to download the slides to my Java training at this site.
thanks for providing excellent real life example of Annotations and Metadata. Good work..
Your student asked a very legitimate question. Sometimes it is indeed helpful to introspect from within your own method. Here a “real world” example: our build process gets all the runtime configuration from a variety of sources, then generates the Java source by sticking all the configuration related annotations in all the classes. If a class cares about a certain configuration value, it introspects that very annotation, if not – no harm is done, the generated code is transient anyway. This way no class has to be aware of how configuration values are obtained, ever.
And, actually, your own example can be, quite usefully, modified to demonstrate the same need. You may want to introduce an overload to your getEmployees() that takes a WHERE predicate to filter out the whole list, in which case it (the overloading method) will have a need to access the sqlStatement value.