vendredi 24 juin 2016

Java: Changing a method at runtime from outside

First the situation:
I have a foreign jar-File, let's say a game. I, myself, am located in another jar-File, the injector.
I have managed hooking to the game-jar's JVM, thus sharing the same space etc. I have also managed that my injector gets called at injection, thus being able to interact.

Let's say the game has a class and method named Game#foo(). I want to inject own code into the method of such an object.
For example a method bar() should be called everytime foo() gets called.

I face several problems:

  • How can I retrieve the Game-object, as I'm already at runtime?
  • How to manipulate the method?

In the end it could look like:

Game game = getGameObject();
game.injectBarIntoFoo();

The situation is strict, the foreign game-jar already is at runtime, I can not load it myself. My injector must start in Java, however, I would have no problem switching to C via JNA/JNI if there's an easy solution.

One could say I'm trying to do something like a DLL-Injection for jar-Files.

Remarks:
I already have heard of hotswapping, JRebel, JavaAssist and of course the Reflection API. But it seems that they all require the Game-object to be created by myself, while setting up a special environment. However, the Game-jar already is at runtime when my stuff comes into play.
Please note that patching the Game-jar prior to execution via ByteCode-Manipulation is also not a favorable solution for me, though possible.





Aucun commentaire:

Enregistrer un commentaire