469,352 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Self Modifying Code

How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
..0.
...0
000
Jul 17 '05 #1
16 7658
Japcuh wrote:
How do you write self modifying code in Java?


What do you mean by "modify?" A Java program can
extend itself by loading new classes on the fly, if
that suits your needs.

--
Er*********@sun.com

Jul 17 '05 #2
On 2 Jul 2004 12:31:17 -0700, ja****@gmail.com (Japcuh) wrote or
quoted :
How do you write self modifying code in Java?


You can't modify a class once it has been loaded, but you can create a
similar class and load that.

You can do it by generating Java source and compiling that on the fly
to byte codes, or by generating JVM byte codes directly.

See http://mindprod.com/jgloss/eval.html
http://mindprod.com/jgloss/jasm.html
http://mindprod.com/jgloss/javacexe.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #3
ja****@gmail.com (Japcuh) wrote in news:3a8cbb2e.0407021131.55805361
@posting.google.com:
How do you write self modifying code in Java?


You may want to check out the Java code generation library cglib:
http://cglib.sourceforge.net/

Best Regards
Kristian Larsen
Jul 17 '05 #4
kjc
No, but this could be done in Smalltalk blind folded.

Japcuh wrote:
How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
.0.
..0
000


Jul 17 '05 #5
On Sat, 03 Jul 2004 15:49:21 GMT, kjc <ks*****@elp.rr.com> wrote or
quoted :
No, but this could be done in Smalltalk blind folded.

Japcuh wrote:
How do you write self modifying code in Java?


Is it a problem that you sometimes do it accidentally?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #6
> ja****@gmail.com (Japcuh) wrote in news:3a8cbb2e.0407021131.55805361
@posting.google.com:
How do you write self modifying code in Java?


http://java.sun.com/developer/techni...leases/j2se15/

The metadata bit of this seems to suggest that in future releases of
java, you'll be able to generate actual source code on the fly.
Jul 17 '05 #7
Thanks for all the help.

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
..0.
...0
000
Jul 17 '05 #8
an************@gmail.com (Andrew Chambers) wrote in message news:<f3************************@posting.google.co m>...
ja****@gmail.com (Japcuh) wrote in news:3a8cbb2e.0407021131.55805361
@posting.google.com:
How do you write self modifying code in Java?


http://java.sun.com/developer/techni...leases/j2se15/

The metadata bit of this seems to suggest that in future releases of
java, you'll be able to generate actual source code on the fly.


I think the metadata is for hints for documentation generation.

I could be wrong.

But I'd hate to work out a security system for Java if they allow the
code to modify itself. Dynamically extending classes is plenty.
Jul 17 '05 #9
kjc <ks*****@elp.rr.com> wrote in message news:<5A*****************@fe1.texas.rr.com>...
No, but this could be done in Smalltalk blind folded.
You can use Java Reflection which is Java's counterpart to Smalltalk's
perform: concatenate some strings to create an existing method name
and call that method. Or you can use the command pattern and add
commands as required to a list and execute them sequentially.

Cheers, Oliver
Japcuh wrote:
How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
.0.
..0
000

Jul 17 '05 #10
Check out this package:

http://jakarta.apache.org/bcel/
"Japcuh" <ja****@gmail.com> wrote in message
news:3a**************************@posting.google.c om...
How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
.0.
..0
000

Jul 17 '05 #11
ja****@gmail.com (Japcuh) wrote in message news:<3a**************************@posting.google. com>...
How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
.0.
..0
000


It's nowhere near as easy as it is in Perl.

If you look at JDBC you'll see that there's a call to Class.forName()
to load the driver class. That may be of use to you. It's sort of an
'import' on the fly.
Jul 17 '05 #12

"Japcuh" <ja****@gmail.com> wrote in message
news:3a**************************@posting.google.c om...
How do you write self modifying code in Java?

Japcuh
(Just Another Perl C Unix Hacker)
http://www.catb.org/~esr/faq/hacker-howto.htm#what_is
.0.
..0
000


Look up "Javassist" on google. Its what JBoss 4.0 uses to do dynamic aspect
weaving and dynamic remote proxy generation among other things. By the way,
for those who have used cglib, can you point toward some good online docs?
I have been unable to find any I found adequate. Javassist has pretty good
docs however.

l8r, Mike N. Christoff

Jul 17 '05 #13
ja****@gmail.com wrote...
How do you write self modifying code in Java?


If it's truly self-modifying, good luck debugging it.
Jul 17 '05 #14
Entropy <en*****@domain.invalid> scribbled on the stall wall:
ja****@gmail.com wrote...
How do you write self modifying code in Java?


If it's truly self-modifying, good luck debugging it.


the simple answer is "you can't"...self modifying code hasn't really been
feasible since the fortran 4 days...or maybe in C but that is still
platform dependent.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 17 '05 #15
no***@uber.usachoice.net wrote...
Entropy <en*****@domain.invalid> scribbled on the stall wall:
ja****@gmail.com wrote...
How do you write self modifying code in Java?


If it's truly self-modifying, good luck debugging it.


the simple answer is "you can't"...self modifying code hasn't really been
feasible since the fortran 4 days...or maybe in C but that is still
platform dependent.


I recall the capacity of LISP-like functional languages (ie, Scheme)
to dynamically generate new functions and apply them.

It was a long time ago when I sat in that class, however, and I've
never used such a language outside of the classroom.
Jul 17 '05 #16


Wiseguy wrote:
Entropy <en*****@domain.invalid> scribbled on the stall wall:
ja****@gmail.com wrote...
How do you write self modifying code in Java?


If it's truly self-modifying, good luck debugging it.

the simple answer is "you can't"...self modifying code hasn't really been
feasible since the fortran 4 days...or maybe in C but that is still
platform dependent.


Java "modifies itself" every time it loads a new class
definition. An important point is that the decision to load
a class is made at run time, not at a "link time."

A Java program can even generate an array of bytecodes
at run time, turn them into a class, and create objects of
that class; see defineClass() in java.lang.ClassLoader.

--
Er*********@sun.com

Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by qwweeeit | last post: by
13 posts views Thread by Robin Becker | last post: by
12 posts views Thread by AES | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.