Program to demonstrate how Reflection API can be used to determine whether a class object is an Annotation or not in Java
Output of the program :
package com.hubberspot.reflection; @interface Specifier { } class Public { } public class AnnotationInfo { public static void main(String[] args) { // Create a Test object Public publicInstance = new Public(); // Get the class of respective types Class publicClass = publicInstance.getClass(); Class specifier = Specifier.class; // isAnnotation() checks whether type is an annotation or not if(specifier.isAnnotation()) { // prints the simple name of the class System.out.println("Given type " + specifier.getSimpleName() + " is an Annotation"); } else { System.out.println("Given class " + specifier.getSimpleName() + " is not an Annotation"); } if(publicClass.isAnnotation()) { System.out.println("Given class " + publicClass.getSimpleName() + " is an Annotation"); } else { System.out.println("Given class " + publicClass.getSimpleName() + " is not an Annotation"); } } }
Output of the program :