| Start |
|---|
| What is Bytecode? Bytecode is the set of instructions used by the JVM. To illustrate this let's take this Hello World program. This is what it turns into when compiled into bytecode. What's the logic behind this?getstatic - Retreives the value of a static field of a class. In this case, the PrintStream "Out" of System. ldc - Push a constant onto the stack. In this case, the String "Hello World" invokevirtual - Invokes a method on a loaded reference on the stack and puts the result on the stack. Parameters of the method are also taken from the stack. Well, there has to be more right?There are 255 opcodes, but not all of them are implemented yet. A table with all of the current opcodes can be found here: Java bytecode instruction listings (opens new window). How can I write / edit bytecode?There's multiple ways to write and edit bytecode. You can use a compiler, use a library, or use a program. For writing: For editing:
Graphical Editors:
I'd like to learn more about bytecode!There's probably a specific documentation page specificially for bytecode. This page focuses on the modification of bytecode using different libraries and tools. |
| How to modify jars with the ASM library Firstly the classes from the jar need to be loaded. We'll use three methods for this process:
With these methods loading and changing a jar file becomes a simple matter of changing ClassNodes in a map. In this example we will replace all Strings in the jar with capitalized ones using the Tree API. Now that all of the ClassNode's strings have been modified we need to save the changes. In order to save the changes and have a working output a few things have to be done:
From the last portion above, we'll create three methods.
Usage: The methods used: That's it. All the changes will be saved to "sample-edit.jar". |
How to load a ClassNode as a Class |
How to rename classes in a jar file SimpleRemapper is an existing class in the ASM library. However it only allows for class names to be changed. If you wish to rename fields and methods you should create your own implemenation of the Remapper class. |
| Javassist Basic Javassist is a bytecode instrumentation library that allows you to modify bytecode injecting Java code that will be converted to bytecode by Javassist and added to the instrumented class/method at runtime. Lets write the first transformer that actually take an hypothetical class "com.my.to.be.instrumented.MyClass" and add to the instructions of each method a log call. Now in order to use this transformer (so that our JVM will call the method transform on each class at load time) we need to add this instrumentor this with an agent: Last step to start our first instrumentor experiment is to actually register this agent class to the JVM machine execution. The easiest way to actually do it is to register it with an option into the shell command: As we can see the agent/transformer project is added as a jar to the execution of any application named MyJavaApplication that is supposed to contain a class named "com.my.to.be.instrumented.MyClass" to actually execute our injected code. |
Read Article
https://develop.horje.com/learn/1434/reference