Pin It

Using Reflection API to determine whether class object is an Annotation or not

Program to demonstrate how Reflection API can be used to determine whether a class object is an Annotation or not in Java

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 : 


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot