Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Self Modifying Code

Thread view: 
Japcuh - 02 Jul 2004 20:31 GMT
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 - 02 Jul 2004 20:56 GMT
> 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.

Signature

Eric.Sosman@sun.com

Roedy Green - 02 Jul 2004 21:19 GMT
>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

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Kristian Larsen - 02 Jul 2004 23:46 GMT
japcuh@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
Andrew Chambers - 03 Jul 2004 17:19 GMT
> japcuh@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/technicalArticles/releases/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.
Joseph Daniel Zukiger - 06 Jul 2004 05:54 GMT
> > japcuh@gmail.com (Japcuh) wrote in news:3a8cbb2e.0407021131.55805361
> > @posting.google.com:
[quoted text clipped - 5 lines]
> 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.
Japcuh - 03 Jul 2004 18:16 GMT
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
kjc - 03 Jul 2004 16:49 GMT
No, but this could be done in Smalltalk blind folded.

> How do you write self modifying code in Java?
>
[quoted text clipped - 4 lines]
> ..0
> 000
Roedy Green - 03 Jul 2004 16:52 GMT
>No, but this could be done in Smalltalk blind folded.
>
>> How do you write self modifying code in Java?

Is it a problem that you sometimes do it accidentally?

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Oliver Plohmann - 06 Jul 2004 15:02 GMT
> 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

> > How do you write self modifying code in Java?
> >
[quoted text clipped - 4 lines]
> > ..0
> > 000
Java Architect - 07 Jul 2004 08:13 GMT
Check out this package:

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

> How do you write self modifying code in Java?
>
[quoted text clipped - 4 lines]
> ..0
> 000
Dave Monroe - 07 Jul 2004 13:11 GMT
> How do you write self modifying code in Java?
>
[quoted text clipped - 4 lines]
> ..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 - 07 Jul 2004 16:05 GMT
> How do you write self modifying code in Java?
>
[quoted text clipped - 4 lines]
> ..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 - 02 Mar 2005 03:06 GMT
japcuh@gmail.com wrote...
> How do you write self modifying code in Java?

If it's truly self-modifying, good luck debugging it.
Wiseguy - 02 Mar 2005 05:34 GMT
Entropy <entropy@domain.invalid> scribbled on the stall wall:
> japcuh@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.
blinky - 02 Mar 2005 06:08 GMT
noone@uber.usachoice.net wrote...
> Entropy <entropy@domain.invalid> scribbled on the stall wall:
> > japcuh@gmail.com wrote...
[quoted text clipped - 5 lines]
> 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 - 02 Mar 2005 16:12 GMT
> Entropy <entropy@domain.invalid> scribbled on the stall wall:
>
[quoted text clipped - 7 lines]
> 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.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.