4

I wish to dynamically add and remove instrumentation code to a Java class file several times without restarting the Java JVM. Is this possible?

yazz.com
  • 57,320
  • 66
  • 234
  • 385
  • 1
    No, this would be a serious security and design flaw. – leppie Jan 28 '11 at 12:07
  • 1
    - "Is this possible?", - "No, it would be a design flaw." ??? – aioobe Jan 28 '11 at 12:10
  • 2
    @leppie It _is_ possible, it's _not_ necessarily a security flaw (heard of `SecurityManager`s?) and it's _not_ necessarilly a design flaw (heard of AspectJ?) – adamax Jan 28 '11 at 12:14
  • @adamax: Not at runtime... Surely you can modify a class and create a new instance of it, but not an existing instance. That case is both a security and design flaw. – leppie Jan 28 '11 at 12:18
  • Not the same but related: http://stackoverflow.com/questions/4817670/how-can-i-add-a-javaagent-to-a-jvm-without-stopping-the-jvm – user85421 Jan 28 '11 at 12:22
  • 1
    Update: I see JRebel is capable of this. I still believe this is security flaw that 'managed' runtimes like JVM and .NET is specifically meant to prevent. – leppie Jan 28 '11 at 12:24
  • @leppie First link at google: http://vinaytechs.blogspot.com/2009/10/java-rebel-on-fly-class-reloading.html. BTW, I meant AOP, I don't really know about AspectJ in particular. – adamax Jan 28 '11 at 12:24
  • 1
    @leppie Instrumentation API is available in the platform and this is not a security flaw. Of course, there's an option to switch it off with SecurityManager, but a lot of Java frameworks use that for being able to provide a programming model that keep developers sane - AOP, ORM, declarative transactions, monitoring agents - all this stuff is based on instrumentation. Depends, why this particular question was asked - patching the code in live environments isn't super-safe of course. – Anton Arhipov Sep 14 '13 at 21:39

4 Answers4

7

I suggest you look at the java.lang.instrument package, especially the ClassFileTransformer.

Here is a good article: Instrumentation: Modify Applications with Java 5 Class File Transformations

For actual bytecode generation, I suggest you have a look at libraries such as BCEL or ASM.

The JRebel framework may also interest you. It can change implementation of method bodies, add / remove methods and constructors, add/remove fields, add/remove classes etc, all in runtime.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • +1 While JRebel looks interesting, it's purpose is counter-intuitive. – leppie Jan 28 '11 at 12:25
  • 1
    @leppie, I guarantee you that it is extremely convenient if you for instance develop web-applications and want the changes to take effect between consecutive requests. – aioobe Jan 28 '11 at 12:28
2

You could use a helper class (Strategy Design Pattern) which may be swapped for another one at run-time.

Highland Mark
  • 1,002
  • 1
  • 7
  • 12
1

You can use Jrebel for hot deployment. It will allow you to change the code without server restart.

Sudhir Mane
  • 308
  • 1
  • 9
0

You also might want to look at ByteMan from JBoss. It uses the same Java agent mechanism and specifically supports installing and uninstalling your modification scripts, see the tutorial. What follows is an abridged version of the tutorial:

For example lets say we have some running Java Process:

$ jps
15295 Jps
4884 main

We can then install ByteMan into the running process:

$ bminstall.sh 4884

You could then create a ByteMan script:

$ youreditor thread.btm
RULE trace thread start
CLASS java.lang.Thread
METHOD start()
IF true
DO traceln("*** start for thread: "+ $0.getName())
ENDRULE

You can then install a ByeMan script with:

$ bmsubmit.sh -l thread.btm

To remove:

$ bmsubmit.sh -u thread.btm

To list what is currently running just issue it without any arguments:

$ bmsubmit.sh

If you are running on windows replace the .sh in each command with .bat.