自定义注解

来源:互联网 发布:python读取txt文件 编辑:程序博客网 时间:2024/06/11 13:14
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