getAnnotation

 

annotation for class

Below programs demonstrate the getAnnotation() method.

Example 1:

filter_none
edit
play_arrow

brightness_4
// Java program to demonstrate 
// getAnnotation() method 
  
import java.util.*; 
import java.lang.annotation.*; 
  
// create a custom Annotation 
@Retention(RetentionPolicy.RUNTIME) 
@interface Annotation { 
  
    // This annotation has two attributes. 
    public String key(); 
  
    public String value(); 
} 
  
// call Annotation for method 
// and pass values for annotation 
@Annotation(key = "GFG", value = "GeeksForGeeks") 
public class Test { 
  
    public static void main(String[] args) 
        throws ClassNotFoundException 
    { 
  
        // returns the Class object for this class 
        Class myClass = Test.class; 
  
        System.out.println("Class represented by myClass: "
                           + myClass.toString()); 
  
        // Get the annotation 
        // using getAnnotation() method 
        System.out.println( 
            "Annotation of myClass: "
            + myClass.getAnnotation( 
                  Annotation.class)); 
    } 
} 

 

annotation for method

package com.yiibai;

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

// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {

   String str();

   int val();
}

public class PackageDemo {

   // set values for the annotation
   @Demo(str = "Demo Annotation", val = 100)
   // a method to call in the main
   public static void example() {
      PackageDemo ob = new PackageDemo();

      try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation for class Demo
         Demo annotation = m.getAnnotation(Demo.class);

         // print the annotation
         System.out.println(annotation.str() + " " + annotation.val());
      } catch (NoSuchMethodException exc) {
         exc.printStackTrace();
      }
   }

   public static void main(String args[]) {
      example();
   }
}

 

annotation for  method

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

public class MethodDemo {
   public static void main(String[] args) {
      Method[] methods = SampleClass.class.getMethods();
      Annotation[] annotations = methods[0].getDeclaredAnnotations();
      for(Annotation annotation : annotations){
         if(annotation instanceof CustomAnnotation){
            CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
            System.out.println("name: " + customAnnotation.name());
            System.out.println("value: " + customAnnotation.value());
         }
      }
   }
}

@CustomAnnotation(name="SampleClass",  value = "Sample Class Annotation")
class SampleClass {
   private String sampleField;

   @CustomAnnotation(name="getSampleMethod",  value = "Sample Method Annotation")
   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   } 
}

@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
   public String name();
   public String value();
}

 

Method[] declaredMethods = this.getClass().getMethods();
for(int i = 0; i < declaredMethods.length; ++i) {
            Annotation[] methodAnnotations = declaredMethods[i].getDeclaredAnnotations();

            for(int j = 0; j < methodAnnotations.length; ++j) {
                if (methodAnnotations[j] instanceof xx) { 
                }
            }
        }

 

posted @ 2020-12-23 17:58  kakaisgood  阅读(439)  评论(0编辑  收藏  举报