Skip to content

Latest commit

 

History

History
128 lines (89 loc) · 3.27 KB

File metadata and controls

128 lines (89 loc) · 3.27 KB

Annotations with Reflection

  • Annotations with the RUNTIME retention policy can be queried at run time by using reflection.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    String str();
    int value();
}

@Retention(RetentionPolicy.CLASS)
@interface MyAnnotationClass {
    String str();
    int value();
}

@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotationSource {
    String str();
    int value();
}

class Main {

    @MyAnnotationClass(str = "hello1", value = 241)
    @MyAnnotationSource(str = "hello2", value = 242)
    @MyAnnotation(str = "hello", value = 24)
    public static void hello(){

    }

    @MyAnnotation(str = "hello", value = 24)
    public static void say(String message, int repeatTimes){}

    public static void main(String[] args){
        try{
            Class<?> c = Main.class;

            // method with arguments
            Method methodWithArgs = c.getMethod("say", String.class,int.class);
            MyAnnotation annotation = methodWithArgs.getAnnotation(MyAnnotation.class);
            System.out.println(annotation.str() + " " + annotation.value());

            System.out.println();

            // method without arguments
            Method methodWithOutArgs = c.getMethod("hello");

            MyAnnotationClass annotation1 = methodWithOutArgs.getAnnotation(MyAnnotationClass.class); // not available
            if(annotation1 == null) System.out.println("MyAnnotationClass is not available at runtime");

            MyAnnotationSource annotation2 = methodWithOutArgs.getAnnotation(MyAnnotationSource.class); // not available
            if(annotation2 == null) System.out.println("MyAnnotationSource is not available at runtime");

            MyAnnotation annotation3 = methodWithOutArgs.getAnnotation(MyAnnotation.class); // available
            System.out.println(annotation3.str() + " " + annotation3.value());

        }catch (Exception ex){
            System.out.println("not found: " + ex.getMessage());
        }
    }

}

Obtaining all annotations

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.*;
import java.util.Arrays;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    String str();
    int value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Description {
    String description();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Singleton {
    boolean singleton();
}


@Description(description = "simple class")
@Singleton(singleton = true)
class Main {

    @MyAnnotation(str = "hello1", value = 241)
    public static void hello(){

    }

    public static void main(String[] args){
        try{
            Class<?> c = Main.class;
            System.out.println(Arrays.toString(c.getAnnotations())); // [@Description(description="simple class"), @Singleton(singleton=true)]

            Method method = c.getMethod("hello");
            System.out.println(Arrays.toString(method.getAnnotations())); // [@MyAnnotation(str="hello1", value=241)]

        }catch (Exception ex){
            System.out.println("not found: " + ex.getMessage());
        }
    }

}