MyAnno.java Source code

Java tutorial

Introduction

Here is the source code for MyAnno.java

Source

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
    String str();

    int val();
}

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

@What(description = "An annotation")
@MyAnno(str = "Meta2", val = 99)
public class Main {
    @What(description = "test method")
    @MyAnno(str = "Testing", val = 100)
    public static void myMeth() throws Exception {
        Main ob = new Main();
        Annotation annos[] = ob.getClass().getAnnotations();
        System.out.println("All annotations for Meta2:");
        for (Annotation a : annos) {
            System.out.println(a);
        }
        Method m = ob.getClass().getMethod("myMeth");
        annos = m.getAnnotations();
        for (Annotation a : annos) {
            System.out.println(a);
        }

    }

    public static void main(String args[]) throws Exception {
        myMeth();
    }
}