public class BeanChangeUtil < T> {
public void contrastObj ( Object oldBean, Object newBean, StringBuilder before, StringBuilder later) {
T pojo1 = ( T) oldBean;
T pojo2 = ( T) newBean;
Class clazz = pojo1. getClass ( ) ;
Field[ ] fields = clazz. getDeclaredFields ( ) ;
jdk8OrAfter ( fields, pojo1, pojo2, before, later, clazz) ;
}
public class BeanChangeUtil < T> {
public void contrastObj ( Object oldBean, Object newBean, StringBuilder before, StringBuilder later) {
T pojo1 = ( T) oldBean;
T pojo2 = ( T) newBean;
Class clazz = pojo1. getClass ( ) ;
Field[ ] fields = clazz. getDeclaredFields ( ) ;
jdk8OrAfter ( fields, pojo1, pojo2, before, later, clazz) ;
}
public void jdk8OrAfter ( Field[ ] fields, T pojo1, T pojo2, StringBuilder before, StringBuilder later, Class clazz ) {
final int [ ] i = { 1 } ;
before. append ( "[" ) ;
later. append ( "[" ) ;
Arrays. asList ( fields) . forEach ( f - > {
if ( f. isAnnotationPresent ( PropertyMsg. class ) ) {
try {
PropertyDescriptor pd = new PropertyDescriptor ( f. getName ( ) , clazz) ;
Method getMethod = pd. getReadMethod ( ) ;
Object o1 = getMethod. invoke ( pojo1) ;
Object o2 = getMethod. invoke ( pojo2) ;
System. out. println ( "222" ) ;
if ( o1 != null && o2 != null) {
if ( ! o1. toString ( ) . equals ( o2. toString ( ) ) ) {
if ( StringUtil. isNotEmpty ( before. toString ( ) ) && ! before. toString ( ) . equals ( "[" ) ) {
before. append ( "," ) ;
}
if ( StringUtil. isNotEmpty ( later. toString ( ) ) && ! later. toString ( ) . equals ( "[" ) ) {
later. append ( "," ) ;
}
before. append ( "{ \"key\":" + "\"" + f. getAnnotation ( PropertyMsg. class ) . value ( ) + "\"," + "\"value\":" + "\"" + o1 + "\"}" ) ;
later. append ( "{ \"key\":" + "\"" + f. getAnnotation ( PropertyMsg. class ) . value ( ) + "\"," + "\"value\":" + "\"" + o2 + "\"}" ) ;
}
}
if ( o1 == null && o2 != null) {
if ( StringUtil. isNotEmpty ( later. toString ( ) ) && ! later. toString ( ) . equals ( "[" ) ) {
later. append ( "," ) ;
}
later. append ( "{ \"key\":" + "\"" + f. getAnnotation ( PropertyMsg. class ) . value ( ) + "\"," + "\"value\":" + "\"" + o2 + "\"}" ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
} ) ;
before. append ( "]" ) ;
later. append ( "]" ) ;
}
@Target ( ElementType. FIELD)
@Retention ( RetentionPolicy. RUNTIME)
@Documented
@Inherited
public @interface PropertyMsg {
String value ( ) ;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TestChange {
private String id;
@PropertyMsg ( "年龄" )
private Integer age;
@PropertyMsg ( "姓名" )
private String name;
}
@Test
public void test ( ) {
TestChange u1 = new TestChange ( "1" , 30 , "张三" ) ;
TestChange u2 = new TestChange ( "2" , 20 , "张三丰" ) ;
StringBuilder before = new StringBuilder ( ) ; ;
StringBuilder later = new StringBuilder ( ) ; ;
BeanChangeUtil< TestChange> t = new BeanChangeUtil < > ( ) ;
t. contrastObj ( u1, u2, before, later) ;
System. out. println ( before) ;
System. out. println ( later) ;
}
[ { "key" : "年龄" , "value" : "30" } , { "key" : "姓名" , "value" : "张三" } ]
[ { "key" : "年龄" , "value" : "20" } , { "key" : "姓名" , "value" : "张三丰" } ]