The second problem with annotations is that anyone can define their own. A key benefit of annotations is their
suitability for automatic analysis, generally via annotation processing tools, or APTs. But automation is most
helpful if there are common, standard annotations. While you have seen how annotation types can be defined,
in practice very few programmers should need to define their own types. There are currently only a few
defined annotation types:[4]
[4] Under the Java Community Process, JSR 250 is defining common annotations for future
use.
- The meta-annotations @Target and @Retention, which you have seen.
- The @Deprecated and @Documented annotations, which you have also seen.
The @Inherited meta-annotation that indicates an annotation should be inherited. For example, if
a class Foo is queried for a specific annotation type that is not present on Foo, but that annotation
type has the @Inherited meta-annotation, then Foo's superclass will be queried, and so forth.
•
The @Override annotation, which informs the compiler that a method is meant to override an
inherited method from a superclass. This allows the compiler to warn you if you mistakenly overload
the method instead of overriding it (a common mistake).
•
The @SuppressWarnings annotation tells the compiler to ignore certain warnings, as defined by
the strings used to initialize the annotation. Unfortunately, there is no predefined set of warning
strings, meaning that interoperability between different compilers could be a problem.
•
Without standards in this area, the effective use of annotations could be stifled.
Finally, another potential problem with annotations is that they could be misused to change the semantics of
the code they are applied to. According to the Java Language Specification, "annotations are not permitted to
affect the semantics of programs in the Java programming language in any way." Indeed, any unrecognized
annotations should just be ignored by the compiler, the virtual machine, and any other annotation processing
tool. However, it is easy to see how, with a suitable processing tool, annotations could be used to achieve a
style of metaprogramming, that allows people to create specialized dialects of the Java programming language
to meet their own specific needs. If programs become dependent on specialized annotation support, then the
portability benefits of the Java platform will be lost.
I don't care who does the electin' as long as I get to do the nominatin'.
Boss Tweed
Chapter 16. Reflection
A sense of humor keen enough to show a man his own absurdities will keep him from the
commission of all sins, or nearly all, save those that are worth committing.
Samuel Butler
The package java.lang.reflect contains the reflection package, the classes you can use to examine a
type in detail. You can write a complete type browser using these classes, or you can write an application that
interprets code that a user writes, turning that code into actual uses of classes, creation of objects, invocations
of methods, and so on. Almost all the types mentioned in this discussion on reflection are contained in the
package java.lang.reflect, except for the classes Class and Package, which are part of the
package java.lang, and Annotation, which is part of the java.lang.annotation package.
Reflection starts with a Class object. From the Class object you can obtain a complete list of members of
the class, find out all the types of the class (the interfaces it implements, the classes it extends), and find out