java 通過反射獲取註解
阿新 • • 發佈:2018-11-11
使用反射獲取某個類的註釋、方法上的註釋、屬性上的註釋。
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