Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > dinamically creating a class (then an object)

Reply
Thread Tools

dinamically creating a class (then an object)

 
 
lbrtchx@gmail.com
Guest
Posts: n/a
 
      05-13-2008
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[i].getName() + "|");
System.out.println("// __ Flds[" + i + "].getType(): |" +
Flds[i].getType() + "|");
System.out.println("// __ Modifier.toString(Flds[" + i +
"].getModifiers()): |" + Modifier.toString(Flds[i].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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      05-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
lbrtchx@gmail.com
Guest
Posts: n/a
 
      05-14-2008
On May 13, 7:07 pm, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-14-2008
(E-Mail Removed) 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/...sFile.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_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndn pbbn>sudo.com
,,,

For the full code, see:

http://groups.google.com/group/de.sc...n&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 ...

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-14-2008
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?
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Tue, 13 May 2008 15:49:23 -0700 (PDT), (E-Mail Removed) 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2008
On Tue, 13 May 2008 15:49:23 -0700 (PDT), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

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

your tools are:
http://mindprod.com/jgloss/classforname.html
http://mindprod.com/jgloss/reflection.html
http://mindprod.com/jgloss/onthefly.html
http://mindprod.com/jgloss/jasm.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
lbrtchx@gmail.com
Guest
Posts: n/a
 
      05-14-2008
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
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-14-2008
On Wed, 14 May 2008, (E-Mail Removed) 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/ja...ary/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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dinamically creating controls Daniel ASP General 2 10-13-2005 03:30 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Really Creating dinamically Web Controls Cesar Garcia H ASP .Net Building Controls 1 02-22-2005 05:18 PM
Why i cannot bind the event handler of the control that i dinamically added ? Umut Tezduyar ASP .Net 2 11-10-2003 07:33 AM
Creating JavaScript Dinamically JMMB ASP .Net 1 08-26-2003 04:18 PM



Advertisments