java 常用的反射实例

1. 新建User实体,在类上添加自定义属性和属性上添加注解@JsonFormat

@Data
@UserAnnotations(value = {@UserAnnotation(name = "11", value = "22", type = 1)})
public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer userId;
	
	private String user_name;

	private String address;

	private Integer age;

	private String mobile;
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	private Date birthday;

	private Integer is_del;

	private Date create_time;
	
	private Date update_time;
	
	private Date del_time;
	
	@UserAnnotations(value = {@UserAnnotation(name = "张三", value = "administrator", type = 100)})
	public String getUserInfo(String user_name, String address) {
		return "";
	}

}

2. 获取User类注解(@UserAnnotations)

User user = new User();
Class<? extends User> clazz = user.getClass();
/***
 * <li>获取类注解(@UserAnnotations)</li>
 */
UserAnnotations annotation2 = (UserAnnotations) clazz.getAnnotation(UserAnnotations.class);
UserAnnotation[] list2 = annotation2.value();
UserAnnotation userAnnotation = list2[0];
String name2 = userAnnotation.name();
String value2 = userAnnotation.value();
int type2 = userAnnotation.type();

3. 获取User实体中的方法注解(getUserInfo方法)

User user = new User();
Class<? extends User> clazz = user.getClass();
/***
 * <li>获取某个方法注解(getUserInfo方法)</li>
 */
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
	String methodName = method.getName();
	if (methodName.equals("getUserInfo")) {
		//UserAnnotations annotations = declaredMethods[4].getAnnotation(UserAnnotations.class);
		UserAnnotations annotations = method.getAnnotation(UserAnnotations.class);
		UserAnnotation[] list = annotations.value();
		UserAnnotation annotation = list[0];
		String name = annotation.name();
		String value = annotation.value();
		int type = annotation.type();
	}
}

4. 获取birthday属性的注解(@JsonFormat) 

User user = new User();
Class<? extends User> clazz = user.getClass();
/***
 * 获取某属性的注解(@JsonFormat)
 */
for (Field field : declaredFields) {
  JsonFormat annotation3 = field.getAnnotation(JsonFormat.class); 
  String fieldName = field.getName();
  if (fieldName.equals("birthday")) {
	  String pattern = annotation3.pattern(); 
	  String timezone = annotation3.timezone();
	  System.out.println("日期格式化 :" + pattern); 
	  System.out.println("时区 :" + timezone);
  }
}

5. 反射赋值User属性

User user = new User();
Class<? extends User> clazz = user.getClass();
JSONObject json = new JSONObject();
String userName = "张三";
String address = "天津市";
String mobile = "18812345678";
Integer age = 30;
Integer isDel = 0;
String birthday = "2000-08-08";
Date date = new Date();
json.put("user_name", userName);
json.put("address", address);
json.put("mobile", mobile);
json.put("age", age);
json.put("is_del", isDel);
json.put("birthday", birthday);
json.put("create_time", date);
/***
 * 获取所有属性,并赋值
 */
Field[] declaredFields = clazz.getDeclaredFields();
for (Method method : declaredMethods) {
	for (Field field : declaredFields) {
		String fieldName = field.getName();
		String upperCase = upperCase(fieldName);
		String getMethod = "get" + upperCase;
		String setMethod = "set" + upperCase;
		String type = field.getGenericType().toString();
		// System.out.println(type);
		JSONObject obj = JSONObject.parseObject(json);
		if (Constant.JAVA_STRING.equals(type)) {
			Method set = clazz.getMethod(setMethod, String.class);
			if ("user_name".equals(fieldName)) {
				boolean containsKey = obj.containsKey("user_name");
				if (containsKey) {
					String userName = (String) obj.get("user_name");
					set.invoke(user, userName);
				}
			}
			if ("address".equals(fieldName)) {
				boolean containsKey = obj.containsKey("address");
				if (containsKey) {
					String address = (String) obj.get("address");
					set.invoke(user, address);
				}
			}
			if ("mobile".equals(fieldName)) {
				boolean containsKey = obj.containsKey("mobile");
				if (containsKey) {
					String mobile = (String) obj.get("mobile");
					set.invoke(user, mobile);
				}
			}
		} else if (Constant.JAVA_INTEGER.equals(type)) {
			Method set = clazz.getMethod(setMethod, Integer.class);
			if ("age".equals(fieldName)) {
				boolean containsKey = obj.containsKey("age");
				if (containsKey) {
					Integer age = (Integer) obj.get("age");
					set.invoke(user, age);
				}
			}
			if ("is_del".equals(fieldName)) {
				boolean containsKey = obj.containsKey("is_del");
				if (containsKey) {
					Integer is_del = (Integer) obj.get("is_del");
					set.invoke(user, is_del);
				}
			}
		} else if (Constant.JAVA_DATE.equals(type)) {
			Method set = clazz.getMethod(setMethod, Date.class);
			if ("birthday".equals(fieldName)) {
				boolean containsKey = obj.containsKey("birthday");
				if (containsKey) {
					Date date = obj.getDate("birthday");
					set.invoke(user, date/* CommonUtils.stringToDate(birthday, "yyyy-MM-dd") */);
				}
			}
			if ("create_time".equals(fieldName)) {
				boolean containsKey = obj.containsKey("create_time");
				if (containsKey) {
					Date date = obj.getDate("create_time");
					set.invoke(user, date);
				}
			}
		}
		// method.invoke(setMethod, type);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值