• 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) { 
                    }
                }
            }
  • 相关阅读:
    springboot: 使web项目支持jsp
    springboot: 集成freemark模板引擎
    Springboot的优点和实现
    Spring的两种动态代理:Jdk和Cglib 的区别和实现
    JAVA单例模式:懒汉式,饿汉式
    java的内存管理 对象的分配与释放
    JAVA反射调用方法
    JAVA内置注解 基本注解
    图——图的定义与操作
    树——二叉树的线索化
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/14180034.html
Copyright © 2020-2023  润新知