bytecode-manipulation

native java bytecode instrumentation

我是研究僧i 提交于 2019-12-21 05:13:23
问题 for bytecode instrumentation in java, there is the asm framework and the bcel and javaassist libraries. However I need to do instrumentation in native code, since some java classes are already loaded by the time the javaagent runs, eg java.lang.Thread, java.lang.Class, etc is there any library for instrumenting java classes in native code? Edit: Seems there is a bit of confusion. What I want is: Create a native java agent, which uses JVMTI apis to change the bytecode of a class while its

Remapper variables during bytecode method inlining by ASM

核能气质少年 提交于 2019-12-20 20:43:12
问题 I am doing an online bytecode method inlining optimization using ASM. My changes are based on the example 3.2.6 Inline Method (http://asm.ow2.org/current/asm-transformations.pdf). The test example (inline callee's calculate(int,int) at Caller::test) is: public class Caller { final Callee _callee; public Caller(Callee callee){ _callee = callee; } public static void main(String[] args) { new Caller(new Callee("xu", "shijie")).test(5, 100); } public void test(int a, int b){ int t = a; int p = b;

Wrong Stack Size calculated by ASM library

风格不统一 提交于 2019-12-20 05:15:34
问题 I generate bytecodes using ASM library and 'Max stack size' for a method is left to be calculated automatically. During runtime,i found this value (max stack size) is not correct. My source code is: ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); .... MethodType initType = MethodType.methodType(void.class, clsList); mv = cw.visitMethod(ACC_PUBLIC, "<init>", initType.toMethodDescriptorString(), null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL

Change behaviour of static method in Java - byte code manipulation

岁酱吖の 提交于 2019-12-20 02:59:12
问题 I am trying to manipulate a static method. For this, Byte Buddy or any other framework can be used. There is one library that is called Pi4J that is used for controlling GPIO of Raspberry Pi. This library has a method called: GpioController gpio = GpioFactory.getInstance(); And this call is called in several places of a program that I might not have control such that I need to modify the invocation. What I would like to do is that when GpioFactory.getInstance is executed in some way detect

Retro-actively add Java annotations to methods?

半世苍凉 提交于 2019-12-20 02:15:13
问题 Is there a way to modify .class files in order to add Java annotations to certain methods? Basically I want to traverse methods of each class file in a jar file and annotate certain ones. Note that this is not at run-time while using the jar file. Rather, after I'm done I want to have modified class files with the annotations. I do have access to the source code, so if there's an automatic source code modifier, that would work as well... I'm assuming I'll need a tool such as Javassist or ASM.

Bytecode manipulation to intercept setting the value of a field

感情迁移 提交于 2019-12-19 08:12:42
问题 Using a library like ASM or cglib , is there a way to add bytecode instructions to a class to execute code whenever the value of a class field is set? For example, let’s say I have this class: public class Person { bool dirty; public String name; public Date birthDate; public double salary; } Let’s say a section of code contains this line: person.name = "Joe"; I want this instruction to be intercepted so the dirty flag is set to true . I know this is possible for setter methods -- person

Inserting bytes in the middle of binary file

拜拜、爱过 提交于 2019-12-17 19:27:24
问题 I want to add some string in the middle of image metadata block. Under some specific marker. I have to do it on bytes level since .NET has no support for custom metadata fields. The block is built like 1C 02 XX YY YY ZZ ZZ ZZ ... where XX is the ID of the field I need to append and YY YY is the size of it, ZZ = data. I imagine it should be more or less possible to read all the image data up to this marker (1C 02 XX) then increase the size bytes (YY YY), add data at the end of ZZ and then add

How to make a copy of a Java class in runtime?

£可爱£侵袭症+ 提交于 2019-12-14 03:47:49
问题 I have a class class Foo { int increment(int x) { return x + 1; } } I want to obtain a copy of this class in runtime, e. g. a class like class Foo$Copy1 { int increment(int x) { return x + 1; } } Which has all the same methods, but a different name. Proxy seem to help with delegating , but not copying the methods with all their bodies. 回答1: You can use Byte Buddy for this: Class<?> type = new ByteBuddy() .redefine(Foo.class) .name("Foo$Copy1") .make() .load(Foo.class.getClassLoader())

problem with classes not found during PlayPlugin.enhance

谁说我不能喝 提交于 2019-12-13 15:29:00
问题 I'm experimenting with a basic bytecode enhancement in a Play plugin, but when it tries to operate on the ApplicationClasses.ApplicationClass that it's given, the class can't be found. public void enhance(ApplicationClasses.ApplicationClass applicationClass) throws NotFoundException, IOException, CannotCompileException { ClassPool classPool = ClassPool.getDefault(); CtClass ctClass = classPool.get(applicationClass.name); ... } The exception is Oops: NotFoundException An unexpected error

Tracing method invocation arguments in bytecode using ASM

故事扮演 提交于 2019-12-12 16:02:26
问题 How can I inspect the bytecode of a class (using something such as ASM) to learn which initial values were passed through to a method? For example: Given some methods that pass values to each other: void m1(Object o) { Object v = o; m2(v); m2("box"); } void m2(Object o) { Object v = o; m3(x); } void m3(Object o) { } And some method calls, all defined in the same class: { Object foo = "foo"; m1(foo); m2("bar"); m3("baz"); } How can I inspect the class' bytecode to learn that m3 will be called