java获取属性注释_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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值