Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Retrieve getter-method/property by name
Dear all,

my problem is that I have to sort/group loads of beans in various
ways.
I'll do this by using java.util.Comparators.
I'll extract the data to be compared from my beans via getter methods.
I would like to use a generic approach for the comparator, i.e. you
simply pass it a getter method and it will compare based on the
natural ordering of the object returned by the getter.

Sample
<code>
public class SomeComp implements Comparator, Serializable {

Method getter;
public SomeComp(Method g){
this.getter = g;
}
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object,
java.lang.Object)
*/
public int compare(Object arg0, Object arg1) {
Comparable c0 = this.getter.invoke(arg0, null);
Comparable c1 = this.getter.invoke(arg1, null);
return c0.compareTo(c1);
}
}
</code>

However, I still have the problem of getting the correct getter method
from the bean. I thought the java.beans package would do this for me,
like
<code>Method getGetter(String propname)<code>
but could not find an easy way.

Any ideas??

Thx
Christian

Report this thread to moderator Post Follow-up to this message
Old Post
Christian
12-09-04 01:57 PM


Re: Retrieve getter-method/property by name
"Christian" <christian.oeckinghaus@vodafone.com> schrieb im Newsbeitrag
news:86dacc03.0412090306.1f4f4050@posting.google.com...
> Dear all,
>
> my problem is that I have to sort/group loads of beans in various
> ways.
> I'll do this by using java.util.Comparators.
> I'll extract the data to be compared from my beans via getter methods.
> I would like to use a generic approach for the comparator, i.e. you
> simply pass it a getter method and it will compare based on the
> natural ordering of the object returned by the getter.
>
> Sample
> <code>
> public class SomeComp implements Comparator, Serializable {
>
>   Method getter;
>   public SomeComp(Method g){
>     this.getter = g;
>   }
> /* (non-Javadoc)
>  * @see java.util.Comparator#compare(java.lang.Object,
> java.lang.Object)
>  */
>   public int compare(Object arg0, Object arg1) {
>     Comparable c0 = this.getter.invoke(arg0, null);
>     Comparable c1 = this.getter.invoke(arg1, null);
>     return c0.compareTo(c1);
>   }
> }
> </code>
>
> However, I still have the problem of getting the correct getter method
> from the bean. I thought the java.beans package would do this for me,
> like
> <code>Method getGetter(String propname)<code>
> but could not find an easy way.
>
> Any ideas??

Use these methods in order:
http://java.sun.com/j2se/1.4.2/docs...trospector.html
http://java.sun.com/j2se/1.4.2/docs...ri
ptors()
http://java.sun.com/j2se/1.4.2/docs...d
Method()

Kind regards

robert


Report this thread to moderator Post Follow-up to this message
Old Post
Robert Klemme
12-09-04 01:57 PM


Re: Retrieve getter-method/property by name
In article <86dacc03.0412090306.1f4f4050@posting.google.com>,
christian.oeckinghaus@vodafone.com (Christian) wrote:

> Dear all,
>
> my problem is that I have to sort/group loads of beans in various
> ways.
> I'll do this by using java.util.Comparators.
> I'll extract the data to be compared from my beans via getter methods.
> I would like to use a generic approach for the comparator, i.e. you
> simply pass it a getter method and it will compare based on the
> natural ordering of the object returned by the getter.

I'd recommend a simpler design. No need for Method or reflection.

> public class SomeComp implements Comparator, Serializable {

add "abstract"

>
>   Method getter;
>   public SomeComp(Method g){
>     this.getter = g;
>   }

Drop these lines. Add this line:

abstract public Comparable get(Object obj);

>   public int compare(Object arg0, Object arg1) {
>     Comparable c0 = this.getter.invoke(arg0, null);
>     Comparable c1 = this.getter.invoke(arg1, null);
>     return c0.compareTo(c1);

Replace with

public int compare(Object obj1, Object obj2) {
return get(obj1).compareTo(get(obj2));
}


>   }
> }

Now you can sort a list of beans using any getter you want,
e.g., to sort a list of PersonName's by last name:

Collections.sort(names, new SomeComp() {
public Comparable get(Object obj) {
return ((PersonName) obj).getLastName();
}
});

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Riesbeck
12-11-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Beans archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:15 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.