158. Reflecting annotations

Java annotations have got a lot of attention from the Java Reflection API. Let's see several solutions for inspecting several kinds of annotations (for example, package, class, and method).

Mainly, all major Reflection API classes that represent artifacts that support annotation (for example, Package, Constructor, Class, Method, and Field) reveal a set of common methods for working with annotations. Common methods include:

  • getAnnotations(): Return all annotations specific to a certain artifact
  • getDeclaredAnnotations(): Return all annotations declared directly to a certain artifact
  • getAnnotation(): Return an annotation by type
  • getDeclaredAnnotation(): Return an annotation by type declared directly to a certain artifact (JDK 1.8)
  • getDeclaredAnnotationsByType(): Return all annotations by type declared directly to a certain artifact (JDK 1.8)
  • isAnnotationPresent(): Return true if an annotation for the specified type is found on the given artifact
getAnnotatedReceiverType() was discussed earlier in the Get annotation on receiver type section.

In the next sections, let's talk about inspecting annotations of packages, classes, methods, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset