zoukankan      html  css  js  c++  java
  • 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) { 
                    }
                }
            }
  • 相关阅读:
    7.12
    Powerdesigner使用方法
    数据库中float类型字段,转化到前端显示,统一保留两位小数
    【1】直接插入排序
    KMP算法
    ssm框架下web项目,web.xml配置文件的作用
    客户要求输入框要记录下上一次输入的内容
    tomcat启动闪退
    页面第一次加载,JS没有效果,刷新一下就好了
    机器学习,安装python的支持包
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/14180034.html
Copyright © 2011-2022 走看看