3 ms·
I was wondering whether such a thing is possible for JVM based languages and it turns out it is: https://stackoverflow.com/questions/8273685/is-it-possible-to-o
by The_rationalist 6y ago
I was wondering whether such a thing is possible for JVM based languages and it turns out it is:
https://stackoverflow.com/questions/8273685/is-it-possible-to-override-a-method-at-runtime https://stackoverflow.com/questions/8273685/is-it-possible-t...
- MaxBarraclough 6y agoSo, is it possible? I get the impression you can create a new class using a bytecode-manipulation library, but that's not the same as modifying an existing class at runtime. I believe classes, once loaded by the JVM, cannot be changed. https://stackoverflow.com/a/43653466/ https://stackoverflow.com/a/43653466/
- CHY872 6y agoIt's generally possible to rewrite loaded classes. Some changes are possible (e.g. replacing a method's body), others are not (adding new methods). The state of the art library for doing this at the application layer is ByteBuddy https://github.com/raphw/byte-buddy#changing-existing-classes https://github.com/raphw/byte-buddy#changing-existing-classe... but this functionality is used by plenty of tooling - profilers such as YourKit rewrite methods to add telemetry - I've seen some security libraries attempt to add additional security related hooks.
- The_rationalist 6y agoWhen would you use ByteBuddy versus https://asm.ow2.io https://asm.ow2.io ?
- CHY872 6y agoASM is far lower level than ByteBuddy. To write some ASM code you probably want to have some proficiency with the bytecode format itself (e.g. you're typically outputting individual JVM bytecode instructions ;(invokespecial,athrow,etc). Personally I'd probably always use ByteBuddy unless I had some very specific reason why not. For comparison, these two examples https://github.com/raphw/byte-buddy#changing-existing-classes https://github.com/raphw/byte-buddy#changing-existing-classe... http://web.cs.ucla.edu/~msb/cs239-tutorial/ http://web.cs.ucla.edu/~msb/cs239-tutorial/ explain how to do a System.out.println wrapper around a method. In one case, the developer writes `System.out.println`. In the other, the developer must individually get the static field System.err and push it to the stack, reference PrintStream's method println, including the arguments (Ljava.lang.String;)V. That means it takes an array of strings and returns void.
- danmg 6y agoIn Java, you can write an agent and intercept when the bytecode for a class is loaded by the JVM by the Class Loader. ASM is just a bytecode reader/writer/visitor library that can then modify it. You could also just do it statically by having the bytecode you want injected ready to go in an array or resource. if (className.equals("a/b/c/d$e")) return fixedClassBytecode; else return bytecode; It's possible to create custom loaders (e.g., loading a class from an encrypted zip-file, or one that creates custom bytecode on the fly) and things which are trying to obfuscate what they're doing will have custom loaders, but this is a choke point that every class that's read in and instantiated must pass through.