dinamically creating a class (then an object)

Discussion in 'Java' started by lbrtchx@gmail.com, May 13, 2008.

  1. Guest

    you simply create an object if you know the class, K, by going

    ClassType IObj = (ClassType)K.newInstance();

    Then you can do what ever you want with the created object, for
    example introspecting the fields:

    public String getObjFieldsInfo(ClassType IObj){
    StringBuffer aB = new StringBuffer();
    Field[] Flds[] = (IObj.getClass()).getDeclaredFields();
    for (int i = 0; i < Flds.length; i++) {
    System.out.println("// __ Flds[" + i + "].getName(): |" +
    Flds.getName() + "|");
    System.out.println("// __ Flds[" + i + "].getType(): |" +
    Flds.getType() + "|");
    System.out.println("// __ Modifier.toString(Flds[" + i +
    "].getModifiers()): |" + Modifier.toString(Flds.getModifiers()));
    System.out.println("~");
    }

    Now, say you know you will need a certain class with some primitive
    and some other types, say, an int, a long, a String and a
    java.awt.Rectangle; how could you:

    1) actually created such a class programmatically (other than going
    the monkey way and creating it by adding lines of text, saving the
    file as ".java" and trying to compile it)

    2) loading it with the same classloader that is running the actual
    code you are using

    so that you can then create objects as I outlined above

    THanks
    lbrtchx
     
    , May 13, 2008
    #1
    1. Advertising

  2. Stefan Ram Guest

    writes:
    >1) actually created such a class programmatically


    A real programer uses »copy con: Example.class« (or the
    equivalent in Java), Quiche eaters might use ASM, BCEL or so:

    http://google.to/search?q=ASM BCEL bytecode
     
    Stefan Ram, May 14, 2008
    #2
    1. Advertising

  3. Guest

    On May 13, 7:07 pm, -berlin.de (Stefan Ram) wrote:
    > writes:
    > >1) actually created such a class programmatically

    >
    > A real programer uses »copy con: Example.class« (or the
    > equivalent in Java), Quiche eaters might use ASM, BCEL or so:
    >
    > http://google.to/search?q=ASM BCEL bytecode


    I am familiar with ASM, but could you be a little more explicit on
    your »copy con: Example.class« method?
    ~
    lbrtchx
     
    , May 14, 2008
    #3
  4. Stefan Ram Guest

    writes:
    > I am familiar with ASM, but could you be a little more explicit on
    >your »copy con: Example.class« method?


    A class file can be written with only Java SE and without any
    additional tools (such as ASM) as long as the author of the
    program is acquainted with the class file format.

    http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

    The copy command will copy keyboard input to a file. Using a
    keyboard, one can input most or all codes (e.g., using ALT+NUM
    keys). So, a real programmer will develop a bytecode program
    in his brain and then type it in via the keyboard observing
    the class file format, even if the enemy already is entering
    the building.

    For example, Herbert Kleebauer wrote a batch file that
    will create a Microsoft® Windows executable using »echo«:

    echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>sudo.com
    ,,,

    For the full code, see:

    http://groups.google.com/group/de.sci.mathematik/msg/db8088fafbdf5131?output=gplain&dmode=source

    After »copy con: sudo.com« you could as well start to enter
    »hD1X-s0P_kUHP0...« on the keyboard.

    Usually real programmers live on islands. When a major crisis
    happens, the president of the USA will travel to them via
    helicopter to fetch them from the island when all of the
    Haskell-programming Quiche eaters are lost in their monadic IO
    and unit test writing. The real programmer will decline all
    offers »I have refrained from programming long ago.«
    (unshaven), while the president says »You are the only one who
    can do this, your country needs you.« But then, there also is
    this little girl, who tells him, her friend will die unless he
    goes back one last time to do his ultimate programming
    mission. Eventually he ...
     
    Stefan Ram, May 14, 2008
    #4
  5. Mark Space Guest

    Stefan Ram wrote:

    > (unshaven), while the president says »You are the only one who
    > can do this, your country needs you.« But then, there also is
    > this little girl, who tells him, her friend will die unless he
    > goes back one last time to do his ultimate programming
    > mission. Eventually he ...
    >


    .... wakes up and gets back to work?
     
    Mark Space, May 14, 2008
    #5
  6. Roedy Green Guest

    On Tue, 13 May 2008 15:49:23 -0700 (PDT), wrote,
    quoted or indirectly quoted someone who said :

    > 1) actually created such a class programmatically (other than going
    >the monkey way and creating it by adding lines of text, saving the
    >file as ".java" and trying to compile it)


    you mean by creating a *.class file in RAM? or do you mean without
    constructing a class, writing some sort of interpreter?
    The "monkey" way now has official sanction, with ability to compile
    generated code in RAM that never lives on disk.

    see http://mindprod.com/jgloss/onthefly.html

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, May 14, 2008
    #6
  7. Roedy Green Guest

    Roedy Green, May 14, 2008
    #7
  8. Guest

    I have checked the links you have provided me with and played quite a
    bit with ASM, but I haven't found a single example of how to use, say,
    ASM to create class types as I initially explained

    lbrtchx
     
    , May 14, 2008
    #8
  9. Tom Anderson Guest

    On Wed, 14 May 2008, wrote:

    > I have checked the links you have provided me with and played quite a
    > bit with ASM, but I haven't found a single example of how to use, say,
    > ASM to create class types as I initially explained


    Did you look at BCEL?

    http://jakarta.apache.org/bcel/

    That pretty much does that. There's an example here:

    http://www.ibm.com/developerworks/java/library/j-dyn0414/

    That does generate a class, but does a lot of other stuff on the way.

    If your goal is to generate classes which do something, though, i do think
    going via a compiler is the way. Writing bytecode - ouch. But if you
    really have a passion for it, then BCEL is the way to go.

    tom

    --
    THE DRUMMER FROM DEF LEPPARD'S ONLY GOT ONE ARM!
     
    Tom Anderson, May 14, 2008
    #9
    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. JMMB

    Creating JavaScript Dinamically

    JMMB, Aug 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    Kevin Spencer
    Aug 26, 2003
  2. Cesar Garcia H

    Really Creating dinamically Web Controls

    Cesar Garcia H, Feb 15, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    117
    Nick Stansbury
    Feb 22, 2005
  3. Replies:
    0
    Views:
    324
  4. Daniel

    Dinamically creating controls

    Daniel, Oct 11, 2005, in forum: ASP General
    Replies:
    2
    Views:
    100
  5. Fabio Cavassini

    Retrieve Object from dinamically loaded HTML

    Fabio Cavassini, Dec 13, 2005, in forum: Javascript
    Replies:
    2
    Views:
    135
    Fabio Cavassini
    Dec 14, 2005
Loading...

Share This Page