Discussion:
Self Modifying Code
(too old to reply)
Japcuh
2004-07-02 19:31:17 UTC
Permalink
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
Eric Sosman
2004-07-02 19:56:53 UTC
Permalink
Post by Japcuh
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.
--
***@sun.com
Roedy Green
2004-07-02 20:19:03 UTC
Permalink
Post by Japcuh
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.
kjc
2004-07-03 15:49:21 UTC
Permalink
No, but this could be done in Smalltalk blind folded.
Post by Japcuh
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
Roedy Green
2004-07-03 15:52:04 UTC
Permalink
Post by kjc
No, but this could be done in Smalltalk blind folded.
Post by Japcuh
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.
Oliver Plohmann
2004-07-06 14:02:00 UTC
Permalink
Post by kjc
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
Post by kjc
Post by Japcuh
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
Roedy Green
2004-07-03 15:55:25 UTC
Permalink
Post by Japcuh
How do you write self modifying code in Java?
By the way comp.lang.java.developer is not an official Java newsgroup.
Just a few lost souls out here. If you want to post where people will
read you, see,
http://mindprod.com/jgloss/newsgroups.html
for a list of the Java newsgroups.

To get started try comp.lang.java.help.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Java Architect
2004-07-07 07:13:14 UTC
Permalink
Check out this package:

http://jakarta.apache.org/bcel/
Post by Japcuh
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
Dave Monroe
2004-07-07 12:11:59 UTC
Permalink
Post by Japcuh
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.
Michael N. Christoff
2004-07-07 15:05:40 UTC
Permalink
Post by Japcuh
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
Entropy
2005-03-02 03:06:27 UTC
Permalink
Post by Japcuh
How do you write self modifying code in Java?
If it's truly self-modifying, good luck debugging it.
Wiseguy
2005-03-02 05:34:16 UTC
Permalink
Post by Entropy
Post by Japcuh
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 =----
blinky
2005-03-02 06:08:10 UTC
Permalink
Post by Wiseguy
Post by Entropy
Post by Japcuh
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.
Eric Sosman
2005-03-02 16:12:06 UTC
Permalink
Post by Wiseguy
Post by Entropy
Post by Japcuh
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.
--
***@sun.com
Loading...