zoukankan      html  css  js  c++  java
  • 18.3.2从Class上获取信息(注解)

    package d18_3_1;
    /**
     * Class类上所包含的注解
     * 
     * getAnnotation(Class annotationClass) 获取该元素上指定的类型的注解
     * getAnnotations():返回此元素上存在的所有注解
     * getDeclaredAnnotations():返回直接存在于此元素上的所有注解
     */
    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;
    
    @XmlRootElement(name = "user")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ClassInfo4 {
    
    	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 = ClassInfo4.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 = ClassInfo4.class.getAnnotations();
    
    		for (Annotation cAnnotation : classAnnotation) {
    			Class annotationType = cAnnotation.annotationType();
    			System.out.println("User类上的注解有: " + annotationType);
    		}
    
    		System.out.println("----------华丽的分割线--------------");
    
    		// 5、获取方法上的所有注解
    		Method method;
    		try {
    			method = ClassInfo4.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;
    	}
    }
    

      

  • 相关阅读:
    爬虫系列---多线程爬取实例
    爬虫系列---selenium详解
    爬虫系列二(数据清洗--->bs4解析数据)
    爬虫系列二(数据清洗--->xpath解析数据)
    爬虫系列二(数据清洗--->正则表达式)
    爬虫实例系列一(requests)
    selenium PO模式
    setUp和tearDown及setUpClass和tearDownClass的用法及区别
    chromeIEFirefox驱动下载地址
    HTTP通信机制
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/7357132.html
Copyright © 2011-2022 走看看