Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 38 inside java @ Languages English instead of the more verbose @ Languages value English These syntactic shortcuts were designed to reduce the boilerplate code necessary when using annotations Annotations in Action Now that you can define simple annotations how do you actually use them By design annotations defined by thirdparty developers are completely ignored by the compiler There are a few specific exceptions that the compiler acts upon such as @ Deprecated but as a rule annotations will never modify the semantics of the code to which they are applied Therefore the only way to make use of annotations is to write a tool that will act upon them There are two ways such tools can be written as external tools or as annotation processors External Tools External tools are the simplest approach to processing exceptions you implement a separate application with its own main method and users of your annotations simply need to run this tool on their classes This is the approach used by TestNG JUnit Guice and other well known tools Such tools are typically run as part of your build and the output of these tools can be quite varied source files documentation files XML and so on There is really no limit on what these tools can do The JDK comes with an API to look up annotations in class files that is sprinkled throughout the reflection package For example you can either obtain all the annotations on a given class or only retrieve specific ones Consider the following code @ Languages English French class MyClass We can look up the annotation as follows Languages languages getClass getAnnotationsByType Languages class for Languages language languages System out println Languages spoken language value Annotation Processors Annotation processors are a relatively recent addition to the JDK and they have opened up a whole new level of innovation in the annotation field Annotation processors were born from the observation that a large proportion of tools that process annotations generate Java source files which then need to be compiled Therefore it appeared useful to integrate such processing inside the Java compiler itself so that the process could be streamlined The idea behind annotation processors is to declare them to the compiler so that it will invoke the processors first and then automatically compile the resulting output Then the compiler resumes its usual process after adding your compiled classes to its classpath The API is a bit diferent from the reflection code I just covered with a few variations For example instead of you looking up annotations the compiler notifies you whenever it encounters an annotation This approach is much more efficient Writing annotation processors is a bit more involved and would require a full article of its own So for now Ill explain the value of annotation processors Generating source code is not a new practice on the JVM But because annotation processors hook directly into the compiler a lot of the pain in building and processing the
You must have JavaScript enabled to view digital editions.