Design Question. Reflection, Create Class on fly

Discussion in 'Java' started by Homer, Feb 21, 2006.

  1. Homer

    Homer Guest

    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
    Homer, Feb 21, 2006
    #1
    1. Advertising

  2. Homer

    IchBin Guest

    Homer wrote:
    > 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-)
    IchBin, Feb 21, 2006
    #2
    1. Advertising

  3. Homer

    Chris Uppal Guest

    Homer wrote:

    > 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
    Chris Uppal, Feb 22, 2006
    #3
  4. Homer

    Ian Shef Guest

    "Homer" <> wrote in news:1140555476.972043.98630
    @g14g2000cwa.googlegroups.com:

    <snip>
    > 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.


    --
    Ian Shef 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
    Ian Shef, Feb 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    2
    Views:
    5,557
    _nuhb_
    Aug 14, 2003
  2. Julian
    Replies:
    1
    Views:
    691
    Roland
    May 24, 2005
  3. py_genetic

    Create a new class on the fly

    py_genetic, May 31, 2007, in forum: Python
    Replies:
    6
    Views:
    271
    Josh West
    Jun 2, 2007
  4. guss
    Replies:
    4
    Views:
    1,673
  5. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    94
    Brian
    Apr 8, 2005
Loading...

Share This Page