Generic JRDataSource with Reflection

By: Andrew Arrow - andrewarrow
Generic JRDataSource with Reflection
2002-07-09 15:19

Not sure if something like this has already been posted but...

I wrote a generic JRDataSource class that uses reflection to call the appropriate methods on the array of objects you pass in. This has made my life much easier. Rather than having to write seperate JRDataSource's for each type of object I just use my ReportGenericData class. Here's an example:

Student[] students = ClassManager.getStudentsByClassID("1001");

String[] fields = {"getName", "getAddress", "getPhone"};

ReportGenericData rgd = new ReportGenericData(students, fields);

In the XML I use:

<field name="0" class="java.lang.String"/>
<field name="1" class="java.lang.String"/>
<field name="2" class="java.lang.String"/>

You give up getting to name your fields a descriptive name. You have to name them 0,1,2,3,4,5,etc.

But in the getFieldValue method of ReportGenericData I have this:

Object value = null;

String fieldName = jrField.getName();
int col = Integer.parseInt(fieldName);

String methodString = (String)fields[col];
Class c = sections[count].getClass();

Method m = c.getMethod(methodString, new Class[0]);
value = m.invoke(sections[count], new Object[0]);
catch (Exception e)
value = new String("Error");

return value;

So if the field is 0 it will call student.getName(), if the field is 1 it will call student.getAddress(), if it's 2 student.getPhone().

Does anyone else have a use for this class?


By: Peter Kelley - yellekau
RE: Generic JRDataSource with Reflection
2002-07-16 01:44
There are a couple of us already working on something similar to this to tie jasper reports to webwork. Seeing as webwork already has this sort of reflection with naming support built in and it allows you to navigate object links as well it turns out to be quite powerful.

What would be really useful though would be a method on the JRDataSource interface to get another JRDataSource with a String query paramater so that we can derive data sources for subreports from master report data sources. Are there any plans afoot to add such a method ? Something like getNewDataSource(String query) would be just what we need.

By: Teodor Danciu - teodord
RE: Generic JRDataSource with Reflection
2002-07-10 11:02


I have planned to provide more JavaBean oriented
data source implementations that use reflection
to retrieve field data from arrays or lists of objects.

The idea behind it would be to establish a naming
convention. The data source implementation would
call the getName() on the current object in the array
or object list if the field is called 'name' or 'Name'.

I hope to add this in the future versions.

Thank you,

By: Teodor Danciu - teodord
RE: Generic JRDataSource with Reflection
2002-08-08 01:37


I'm curious to see whether somebody have tried
the supplied JRDataSource implementations that
use reflection and come with the 0.3.3 version.

They are used in the "database" sample for those
who want to perform a quick test.

Thank you,

By: JC Tchitchiama - jeanch
RE: Generic JRDataSource with Reflection
2002-10-15 09:17
This Generic DS is indeed a good idea, looking at the code snippet I was confused by
sections[count] it does not quite tie with the reste of the code. I wonder where it's coming from.
If anybody out there has done something similar, I would be curious to see how it was done.


By: Teodor Danciu - teodord
RE: Generic JRDataSource with Reflection
2002-10-15 12:58


Have you checked the
that come with the JasperReports library?

Thank you,
2001 JI Open Discussion's picture
Joined: Aug 10 2006 - 3:26am
Last seen: 17 years 1 month ago

0 Answers:

No answers yet