A short project to understand custom annotations and use reflection to prove annotation existence. Some pointers about annotations:
- annotations are Java types that are preceded by an “@” symbol.
- Support added in Jdk 5
- Basically, an annotation assigns extra metadata to the source code it’s bound to. By adding an annotation to a method, interface, class, or field, we can:
- Inform the compiler about warnings and errors
- Manipulate source code at compilation time
- Modify or examine behavior at runtime
- Java Built-in Annotations, which take affect during compilation
- @Override: indicate that a method overrides an inherited method.
- @SuppressWarnings: we want to ignore certain warnings from a part of the code.
- @Deprecated: used to mark an API as not intended for use anymore.
- @SafeVarargs: acts on a type of warning related to using varargs.
- @FunctionalInterface: If we intend a SAM interface to be used by lambdas, we can optionally mark it as such with @FunctionalInterface, if the interface is not SAM, the compiler will complain.
- Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation.
- @Target: to what elements can the annotation be applied
- @Retention: till what lifecycle of app would the annotation be associated with the annotated class
- @Inherited: if this annotation should be automatcally be applied to all the sub-classes of annotated class
- @Documented: if the documentation should appear in java doc
- @Repeatable: used when we want add same annotation multiple times on a class, we don't have to wrap it inside other annotation anymore
Just do a git clone, run it as java application and enjoy!
Thanks :)