aspose file tools
The moose likes Java in General and the fly likes howto Log it? Looking for a generic method that checks objects fields (compare 2 obj) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "howto Log it? Looking for a generic method that checks objects fields (compare 2 obj)" Watch "howto Log it? Looking for a generic method that checks objects fields (compare 2 obj)" New topic
Author

howto Log it? Looking for a generic method that checks objects fields (compare 2 obj)

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
hi all,

I'm trying to build a logging mechanism that logs only the revisions made on an object (find the difference in the fields. think of an object that the user just got from the db, now he makes revisions - I wish to find those revisions)

I wonder if anyone knows any *generic* method that loops through 2 instances of the same class and find the difference in terms of their fields. In other words loop through 2 object's fields and compare them (must be the same class).

Below is a code example.
thanks for any thoughts.



Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42

I wonder if anyone knows any *generic* method that loops through 2 instances of the same class and find the difference in terms of their fields.


What would such a method return? The diff(...) method in your example is void. You could have an equals method that uses reflection (I think EqualsBuilder from Jakarta commmons does something like that).
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Peter:I wonder if anyone knows any *generic* method that loops through 2 instances of the same class and find the difference in terms of their fields. In other words loop through 2 object's fields and compare them


Have you tried anything using reflection?
If you are comparing based on public fields/getters(java bean convention), then this must be straight forward.
All you have to do is list the getters, execute them for both the instances. Compare the values using equals. If not equal then the field has changed.
The trick is that if the field is an object other than the primitive type then you must call the diff generator recursively.


apigee, a better way to API!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: howto Log it? Looking for a generic method that checks objects fields (compare 2 obj)
 
Similar Threads
javabean problem
Returning an array of objects through a web service
Should I use a list for this? Or something else?
Retrieving hashtable values in JSP
program error