in theJavaIt is common to compare whether two objects are the same as the modified values before they are modified, generally we use the reflection technique to get the object'sGetMethod[or the other way]gets the value and makes a comparison. If the modified property name is also displayed, it will be more intuitive to display the value of which property in the class has been modified. ThenJavacan only get the name of the property, which is the attribute name of the English identity, but generally we will add a comment to the property after the property, butJavadoes not provide a way to get comments. So we can only use another way to correlate attributes and attribute annotations, which isJavain the@Annotation.
Public static map<string, map<string, string>> compile (Object Db,object new_) {map<string, map<string, string>> map = new HashMap<String, Map<String, String>> ()//Store property values before and after modification class<object> cdb = (class<object>) db.getclass (); Field[] filesdb = cdb.getdeclaredfields (); class<object> cnew_ = (class<object>) new_.getclass (); Map<string, string> valdbmap = new hashmap<string, string> ();// The modified value before the change is stored map<string, string> valnewmap = new hashmap<string, string > ()///Store modified value for (FIELD&NBSP;FIELD&NBSP;:&NBSP;FILESDB) {String getMethodName = " Get "+ field.getname (). substring (0, 1). toUpperCase () + field.getname (). substring (1); try { method mdb = (Method) cdb.geTMethod (Getmethodname); method mnew_ = (Method) cnew_.getmethod (getmethodname);//annotation class for custom implementations Annotation meta = mdb.getannotation (Annotation.class); try {if (meta!= null) {Object valDb = mdb.invoke (db); Object valnew = mnew_.invoke (New_);if (valDb != null ) {if (!valdb.equals (valnew)) { valdbmap.put (Meta.annotation (), string.valueof (valDb) ); valnewmap.put (Meta.annotation (), string.valueof (Valnew));}}} catch (illegalaccessexception e) {e.printstacktrace ();} catch (illegalargumentexception e) {e.printstacktrace ();} catch (invocationtargetexception e) {e.printstacktrace ();} } catch (nosuchmethodexception e) {log.debug ("No way to show calls");} catch (securityexception e) {e.printstacktrace ();}} Map.put ("Pre-change", valdbmap), Map.put ("after", valnewmap); Return map;}
The following provides the annotation class, here is relatively simple, actually do not want to write, but in order to facilitate the needs of the person still provides the custom annotation class:
@Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interface Annotation {public String annotation ();}
Definition of the annotation class I'm not going to tell you this piece. How to use it, add @annotation (annotation= "note") to the method of the class we need to compare
My method in this case is the Get method. I will not provide the following, there is a question of welcome to put forward.
Compare property values in two objects in Java [reflection, annotations]