dinamically creating a class (then an object)

L

lbrtchx

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
 
S

Stefan Ram

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

Mark Space

Stefan said:
(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?
 
R

Roedy Green

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
 
L

lbrtchx

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
 
T

Tom Anderson

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
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top