自定义注解

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
 *  数据足迹
 *  @author xzran
 *  @created 2016年7月24日 下午3:55:37
 *  @lastModified       
 *  @history           
 */
@Target( { FIELD })
@Retention(RUNTIME)
public @interface ChangeType {
	/**
	 * 变更类型
	 *  @return
	 *  @author xzran
	 *  @created 2016年7月24日 下午3:57:59
	 *  @lastModified       
	 *  @history
	 */
	 String changeType() default "";
	 /**
	 * 类型简拼
	 *  @return
	 *  @author xzran
	 *  @created 2016年7月24日 下午3:57:59
	 *  @lastModified       
	 *  @history
	 */
	String lxjp() default "";
}


@Target(ElementType.FIELD)
public @interface A{}

表示注解A只能用来修饰类中的Field

@Target({ElementType.FIELD, ElementType.METHOD})
public @interface A{}

表示注解A能用来修饰类中的Field和Method


	/**
	 * 获取表单前后变化的属性值
	 *  @param befor
	 *  @param after
	 *  @return
	 *  @author xzran
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws IntrospectionException 
	 *  @created 2016年7月24日 下午3:01:50
	 *  @lastModified       
	 *  @history
	 */
	public static <T> List<ChangeLogDto> changeProperty(Object befor ,Object after) throws  Exception{
		List<ChangeLogDto> list = new ArrayList<ChangeLogDto>();
		if(befor != null && after != null){
//			try {
				Class<?> classBefor = befor.getClass();
				Class<?> classAfter= befor.getClass();
				Field[] fsBefor = classBefor.getDeclaredFields();
				Field[] fsAfter = classAfter.getDeclaredFields();
				for(Field bField : fsBefor){
					bField.setAccessible(true);
					ChangeType bchange = bField.getAnnotation(ChangeType.class);
					if(bchange != null){
						for(Field aField :fsAfter){
							ChangeType achange = aField.getAnnotation(ChangeType.class);
							if(achange != null){
								aField.setAccessible(true);
								PropertyDescriptor bpd = new PropertyDescriptor(bField.getName(), classBefor);
								if(StringUtils.equals(bField.getName(),aField.getName())){
									//获得属性值
									Method bmethod = bpd.getReadMethod();
									PropertyDescriptor apd = new PropertyDescriptor(bField.getName(), classBefor);
									
									Method amethod = apd.getReadMethod();
									//获得属性类型
									if(bField.getGenericType().toString().equals("class java.lang.String") &&
											aField.getGenericType().toString().equals("class java.lang.String")){
										//比较对象前后的属性值时候相同
										if(!StringUtils.equals(((String)bmethod.invoke(befor)).trim(),((String)amethod.invoke(after)).trim())){
											ChangeLogDto dto = new ChangeLogDto();
											//是否是字典属性,是:保存字典对应的名称
											if(StringUtils.isBlank(bchange.lxjp())){
												dto.setBefore((String)bmethod.invoke(befor));
												dto.setAfter((String)amethod.invoke(after));
											}else {
												dto.setBefore(CacheLoadUtils.getDictMc(bchange.lxjp(),(String)bmethod.invoke(befor)));
												dto.setAfter(CacheLoadUtils.getDictMc(bchange.lxjp(),(String)amethod.invoke(after)));
											}
											dto.setType(bchange.changeType());
											list.add(dto);
										}
									}else {
										if(!StringUtils.equals(String.valueOf(bmethod.invoke(befor)),String.valueOf(amethod.invoke(after)))){
											ChangeLogDto dto = new ChangeLogDto();
											dto.setBefore(String.valueOf(bmethod.invoke(befor)));
											dto.setAfter(String.valueOf(amethod.invoke(after)));
											dto.setType(bchange.changeType());
											list.add(dto);
										}
									}
								}
							}
						}
					}
				}
//			} catch (SecurityException e) {
//				e.printStackTrace();
//			} catch (IllegalArgumentException e) {
//				e.printStackTrace();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
		}
		return list;
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值