Design Question. Reflection, Create Class on fly

H

Homer

Hi All,

In company I am working we have wide variation of FlatDataFiles and
pretty much what we are doing is process those files and create another
bunch of flat files.

What I've done is past (and present) is to create a class structure for
each file type and put all the fields and functions related to that
file_type inside that class. Class will read the file and give me back
object (either reads the whole file if is small or I pass one record to
class and get back formatted data inside object).

Now I am thinking about why not write only one class that reads
everything based on a XML format_file. Inside XML file I explain what
kind of records and fields I am going to have (format of the file) and
I use the same code for different files.

Now the problem is I can't create a class on fly because
class/variables names can't be dynamic. I am writing a version that
keeps everything inside arraylist but it doesn't look good at all. I
was thinking about opening a file and Write my class inside the file
(fileObj.write("class myclass { .....}"); ), then close it (as
something.java) and compile and link it.

Does any body have any idea if I am going the right way and how to
solve my problem?


Thanks in advance,

Homer
 
I

IchBin

Homer said:
Hi All,

In company I am working we have wide variation of FlatDataFiles and
pretty much what we are doing is process those files and create another
bunch of flat files.

What I've done is past (and present) is to create a class structure for
each file type and put all the fields and functions related to that
file_type inside that class. Class will read the file and give me back
object (either reads the whole file if is small or I pass one record to
class and get back formatted data inside object).

Now I am thinking about why not write only one class that reads
everything based on a XML format_file. Inside XML file I explain what
kind of records and fields I am going to have (format of the file) and
I use the same code for different files.

Now the problem is I can't create a class on fly because
class/variables names can't be dynamic. I am writing a version that
keeps everything inside arraylist but it doesn't look good at all. I
was thinking about opening a file and Write my class inside the file
(fileObj.write("class myclass { .....}"); ), then close it (as
something.java) and compile and link it.

Does any body have any idea if I am going the right way and how to
solve my problem?


Thanks in advance,

Homer
Not sure but with Java 1.6.0 you can compile a class from with in a
running class.

javax.tools.JavaCompilerTool;
javax.tools.JavaFileManager;
javax.tools.JavaFileObject;
javax.tools.ToolProvider;

Mustang: Compile Programmatically with the JSR-199 Compiler API
http://www.javalobby.org/java/forums/m91985882.html

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
C

Chris Uppal

Homer said:
In company I am working we have wide variation of FlatDataFiles and
pretty much what we are doing is process those files and create another
bunch of flat files. [...]
Now the problem is I can't create a class on fly because
class/variables names can't be dynamic. I am writing a version that
keeps everything inside arraylist but it doesn't look good at all. I
was thinking about opening a file and Write my class inside the file
(fileObj.write("class myclass { .....}"); ), then close it (as
something.java) and compile and link it.

I think you may be overcomplicating things. Assume that you have code to parse
the XML and generate the Java source for classes corresponding to each kind of
file. The question is when to use that code. To me it seems simpler just to
run it as part of your build process to generate/update classes that your other
code can use directly.

You may have other requirements which you haven't told us about which make the
simple solution inappropriate, but if so then you will have problems /using/
your dynamically-generated files from your hand-written code. The compiler
won't let you use those classes (since they don't exist) and so you'll have to
define a /generic/ interface to which they all confirm. (When I say "generic"
I'm not referring to the new Java generics.) The interface that your
hand-written code uses won't have accessors for each field (since that's
dependent on the specific file), so all it can have is one or two
general-purpose accessors which take the Sting name of a field as parameter.
But then you're back in the position you are trying to avoid, since you can do
that /anyway/ without any dynamically (or statically) generated classes at
all...

-- chris
 
I

Ian Shef

@g14g2000cwa.googlegroups.com:

Now I am thinking about why not write only one class that reads
everything based on a XML format_file. Inside XML file I explain what
kind of records and fields I am going to have (format of the file) and
I use the same code for different files.

Now the problem is I can't create a class on fly because
class/variables names can't be dynamic. I am writing a version that
keeps everything inside arraylist but it doesn't look good at all. I
was thinking about opening a file and Write my class inside the file
(fileObj.write("class myclass { .....}"); ), then close it (as
something.java) and compile and link it.
<snip>
You may want to pick up a copy of "Java Reflection in Action (In Action
series)" by Ira R. Forman and Nate Forman. The book (and the accompanying
web site) provides code for introspecting existing classes and creating new
subclasses on the fly, by writing a file and causing it to be compiled and
loaded.

It doesn't make use of XML, but a variation of the same techniques should
work. It should get easier when Java 1.6 provides a standardized mechanism
for compilation.

Disclaimer: I am not involved with this book except as a reader.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top