Monday, November 3, 2014

Java annotations



How to create basic custom annotation

package com.java2novice.annotations;
public interface MySampleAnn
{
    String name();
    String desc();
}
class MyAnnTest
{
    MySampleAnn(name = "test1", desc = "testing annotations")
    public void myTestMethod()
    {
    }
}

What is Retention policy in java annotations

package com.java2novice.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Retention(RetentionPolicy.Runtime)
Public interface MySampleAnn
{
   String name();
    String desc();
}
class MyAnnTest
{
    MySampleAnn(name = "test1", desc = "testing annotations")
    public void myTestMethod()
   {
       }
}

How to get annotations at runtime using java reflection

package com.java2novice.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
Retention(RetentionPolicy.Runtime)
interface MyAnnotation
{
    String key();
    String value();
}
public class MyAnnotationTest
{
   MyAnnotation(key="site", value="java2novice.com")
    public void myAnnotationTestMethod()
{
         try
       {
            Class MyAnnotationTest> cls = this.getClass();
            Method mth = cls.getMethod("myAnnotationTestMethod");
            MyAnnotation myAnno = mth.getAnnotation(MyAnnotation.class);
            System.out.println("key: "+myAnno.key());
            System.out.println("value: "+myAnno.value());
         }
         catch (SecurityException e)
         {
         e.printStackTrace();
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String a[])
   {
        MyAnnotationTest mat = new MyAnnotationTest();
        mat.myAnnotationTestMethod();
    }
}

 How to get all annotations from a class

package com.java2novice.annotations;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
Retention(RetentionPolicy.Runtime)
interface MyAnnoOne
{
    String key();
    String value();
}
Retention(RetentionPolicy.Runtime)
interface MyAnnoTwo
{
     int count();
}
public class MyMultipleAnns
{
      MyAnnoTwo(count=20)
        MyAnnoOne(key="site", value="java2novice.com")
 public void myAnnotationTestMethod()
{
 try
     {
            Class MyMultipleAnns> cls = this.getClass();
            Method mth = cls.getMethod("myAnnotationTestMethod");
            Annotation[] anns = mth.getAnnotations();
            for(Annotation an:anns)
           {
                System.out.println(an);
            }
        }
        catch (SecurityException e)
       {
        e.printStackTrace();
        }
        catch (NoSuchMethodException e)
       {
                   e.printStackTrace();
        }
    }
    public static void main(String a[])
   {
        MyMultipleAnns mat = new MyMultipleAnns();
        mat.myAnnotationTestMethod();
    }
}

No comments:

Post a Comment