Reflection question

D

Dave

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?
 
O

Oliver Wong

Dave said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top