Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Reflection question

Thread view: 
Dave - 09 Dec 2005 19:23 GMT
I am using glazed lists to create JTables from simple POJO objects.
With my understanding of glazed lists you basically need to create a
TableFormat and TextFilterator for each POJO in order to create the
table (with filtering).  I am trying to use reflection to dynamically
determine the column count, column names and column values.  Here is
how I access the POJO objects in order to get the column values:

   /* */
   public Object getColumnValue(Object baseObject, int column)
   {
       Object columnValue = null;
       Class c = baseObject.getClass();
       Field [] fields = c.getDeclaredFields();
       try
       {
           columnValue = fields[column].get(baseObject);
       }
       catch (IllegalAccessException e)
       {

TalonAuditTrails.getInstance(this.getClass().toString()).exceptionThrown(e);
       }
       catch (Exception e)
       {

TalonAuditTrails.getInstance(this.getClass().toString()).exceptionThrown(e);
       }
       return columnValue;
   }

Here's the rub:

The attributes must be declared as public in the POJO to use this
method; which I don't want to because I feel this is bad design.

Is there a better way to do this with reflection?
Oliver Wong - 09 Dec 2005 19:29 GMT
>I am using glazed lists to create JTables from simple POJO objects.
> With my understanding of glazed lists you basically need to create a
[quoted text clipped - 32 lines]
>
> Is there a better way to do this with reflection?

   I believe the typical approach to this is to only get the fields which
are public (there may be zero of these), and then additionally get values
from all the getters that can be found.

   That is, after getting all the declared fields, you then get all the
declared methods which take no arguments, which return something, and whose
name starts with "get" (or "is" in the case where the method returns a
boolean).

   So if I had a class like this:

<code>
public class myClass {
 private int x, y, z;

 public int getX() {
   return this.x;
 }

 public void doubleX() {
   x = x * 2;
 }

 public int getY() {
   return this.y;
 }

 public void setY(int value) {
   this.y = value;
 }
}
</code>

Then the reflection code would know to use getX() and getY() to populate the
column values, but to not expose the value of z.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.