zoukankan      html  css  js  c++  java
  • java 通过反射获取注解

    package com.mine.practice.reflectfield;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
      * 
      * @author 2014-11-10 下午01:54:48 
      * @version V1.0  
     */
    @XmlRootElement(name="user")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class User {
    
      private String pwd;
    
      @XmlElement(name = "ID")
      private int id;
      
      @XmlAttribute
      @XmlElement
      private String name;
      
      /***
       *  1、获取属性上的指定类型的注释
       *  2、获取属性上的指定类型的注释的指定方法
       *  3、获取属性上的所有注释
       *  4、获取类上的所有注释
       *  5、获取方法上的所有注释
        * @author 2014-11-10 下午02:18:24
        * @param args 
       */
      @SuppressWarnings("rawtypes")
      public static void main(String[] args) {
        
        Field[] fields =  User.class.getDeclaredFields();
        
        for(Field f : fields){
          String filedName = f.getName();
          System.out.println("属性名称:【"+filedName+"】");
    
          //1、获取属性上的指定类型的注释
          Annotation annotation = f.getAnnotation(XmlElement.class);
          
          //有该类型的注释存在
          if (annotation!=null) {
            //强制转化为相应的注释    
            XmlElement xmlElement = (XmlElement)annotation;
            //3、获取属性上的指定类型的注释的指定方法
            //具体是不是默认值可以去查看源代码
            if (xmlElement.name().equals("##default")) {
              System.out.println("属性【"+filedName+"】注释使用的name是默认值: "+xmlElement.name());
            }else {
              System.out.println("属性【"+filedName+"】注释使用的name是自定义的值: "+xmlElement.name());
            }
          }
    
          //2、获取属性上的所有注释
          Annotation[] allAnnotations = f.getAnnotations();
          
          for(Annotation an : allAnnotations){
            
            Class annotationType = an.annotationType();
            
            System.out.println("属性【"+filedName+"】的注释类型有: " + annotationType);
          }
          System.out.println("----------华丽的分割线--------------");
        }
        
        //4、获取类上的所有注释
        Annotation[] classAnnotation = User.class.getAnnotations();
        
        for(Annotation cAnnotation : classAnnotation){
          Class annotationType =  cAnnotation.annotationType();
          System.out.println("User类上的注释有: " +annotationType);
        }
        
        System.out.println("----------华丽的分割线--------------");
        
        // 5、获取方法上的所有注释
        Method method;
        try {
          method = User.class.getMethod("setPwd",String.class);
          
          Annotation[] methodAnnotations = method.getAnnotations();
    
          for(Annotation me : methodAnnotations){
            Class annotationType =  me.annotationType();
            System.out.println("setPwd方法上的注释有: " + annotationType);
          }
        } catch (SecurityException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }
      }
    
      @XmlElement
      public void setPwd(String pwd) {
        this.pwd = pwd;
      }
    
      public String getPwd() {
        return pwd;
      }
    }
    运行结果如下所示
    属性名称:【pwd】
    ----------华丽的分割线--------------
    属性名称:【id】
    属性【id】注释使用的name是自定义的值: ID
    属性【id】的注释类型有: interface javax.xml.bind.annotation.XmlElement
    ----------华丽的分割线--------------
    属性名称:【name】
    属性【name】注释使用的name是默认值: ##default
    属性【name】的注释类型有: interface javax.xml.bind.annotation.XmlAttribute
    属性【name】的注释类型有: interface javax.xml.bind.annotation.XmlElement
    ----------华丽的分割线--------------
    User类上的注释有: interface javax.xml.bind.annotation.XmlAccessorType
    User类上的注释有: interface javax.xml.bind.annotation.XmlRootElement
    ----------华丽的分割线--------------
    setPwd方法上的注释有: interface javax.xml.bind.annotation.XmlElement
    
    
    
    
    public static void main(String[] args) {
            Field[] fields=AdmEvaluationitemEntity.class.getDeclaredFields();
            for(Field field:fields){
                //获得属性名
                String name=field.getName();
                System.out.print("属性名:"+name+"  ");
                //获得属性column的dataType;
                Column col=field.getAnnotation(Column.class);
                DataType d=col.dataType();
                System.out.print("数据类型:"+d.name()+"  ");
                //获得column的name;
                String tname=col.name();
                System.out.print("column-name:"+tname+"  ");
                //获得id注释
                Id id=field.getAnnotation(Id.class);
                if(id!=null){
                    System.out.println("id:"+field.getName());
                }else{
                    System.out.println();
                }
            }
            //获得表名
            Table t=AdmEvaluationitemEntity.class.getAnnotation(Table.class);
            String n=t.name();
            System.out.println("表名:"+n);
        }
    
    属性名:id  数据类型:STRING  column-name:ID  id:id
    属性名:itemtype  数据类型:STRING  column-name:itemType  
    属性名:itemname  数据类型:STRING  column-name:itemName  
    属性名:description  数据类型:STRING  column-name:description  
    属性名:weight  数据类型:DECIMAL  column-name:weight  
    属性名:typeindex  数据类型:DECIMAL  column-name:typeIndex  
    表名:ADM_EVALUATIONITEM
  • 相关阅读:
    RocketMQ中Producer消息的发送源码分析
    VS等待调试
    Window&Linux遍历某一文件夹
    遍历当前USB设备信息
    批处理常用符号详解
    Windows 批处理(bat)语法大全
    Windows CMD命令大全(值得收藏)
    遍历文件夹
    ASCII,UTF-8,Unicode字符串相互转换
    shellexecute的使用和X64判断
  • 原文地址:https://www.cnblogs.com/gym2017/p/7475673.html
Copyright © 2011-2022 走看看