Reputation: 21
How can I insert this code into a method in a Java class file using the ASM library?
long MEGABYTE = 1024L * 1024L;
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long memory = runtime.totalMemory() - runtime.freeMemory();
double memoryUsage=(double)memory/MEGABYTE;
Upvotes: 2
Views: 1872
Reputation: 43972
The ASM package knows a utility called the ASMifier
. This utility can be used to translate a compiled class into instructions for ASM to create this code. You could implement the code, use the ASMifier to read it and copy the created instructions into a new ASM ClassWriter
that appends the required method to a byte stream it receives from an ASM ClassReader
.
If this does not make sense to you: Read the ASM documentation. It is a great document and you will be able to understand how to do this with the above guidance after you read it.
Upvotes: 6