bytecode-manipulation

Rewriting method calls within compiled Java classes

陌路散爱 提交于 2019-12-03 16:21:00
I want to replace calls to a given class with calls to anther class within a method body whilst parsing compiled class files... or put another way, is there a method of detecting usages of a given class in a method and replacing just that part of the method using something like javaassist . for example.. if I had the compiled version of class A { public int m() { int i = 2; B.multiply(i,i); return i; } } is there a method of detecting the use of B and then altering the code to perform class A { public int m() { int i = 2; C.divide(i,i); return i; } } I know the alternative would be to write a

Javassist: re-creating a class - delete first, or defrost() and modify?

早过忘川 提交于 2019-12-03 08:58:48
I use Javassist to create a class. And in a test suite, when a second test tries to create the same class, it fails at pool.makeClass( ... ) because the class is frozen (i.e. already created via toClass() . What's the way to overcome this? Ideally, the first test should delete the class somehow - perhaps unload from the classloader - but as I read in JLS , the unload operation is not reliable. So perhaps the workaround is to check in the class creating code whether it exists, and if it does, defrost() it, remove all members etc, and re-create it. Any other ideas? Or is there some reliable way

Overriding the default type() metaclass before Python runs

和自甴很熟 提交于 2019-12-03 07:40:10
问题 Here be dragons. You've been warned. I'm thinking about creating a new library that will attempt to help write a better test suite. In order to do that one of the features is a feature that verifies that any object that is being used which isn't the test runner and the system under test has a test double (a mock object, a stub, a fake or a dummy). If the tester wants the live object and thus reduce test isolation it has to specify so explicitly. The only way I see to do this is to override

Remapper variables during bytecode method inlining by ASM

一世执手 提交于 2019-12-03 06:48:01
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; int r = t+p-_callee.calculate(a, b); int m = t-p; System.out.println(t); } } public class Callee {

Overriding the default type() metaclass before Python runs

谁说胖子不能爱 提交于 2019-12-02 21:05:23
Here be dragons. You've been warned. I'm thinking about creating a new library that will attempt to help write a better test suite. In order to do that one of the features is a feature that verifies that any object that is being used which isn't the test runner and the system under test has a test double (a mock object, a stub, a fake or a dummy). If the tester wants the live object and thus reduce test isolation it has to specify so explicitly. The only way I see to do this is to override the builtin type() function which is the default metaclass. The new default metaclass will check the test

Wrong Stack Size calculated by ASM library

百般思念 提交于 2019-12-02 06:02:05
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, "java/lang/invoke/BaseTemplate", "<init>", "()V", false); for(int i=0; i< list.size(); i++){ mv

Change behaviour of static method in Java - byte code manipulation

点点圈 提交于 2019-12-02 02:01:24
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 and modify the methods of GpioController so they log that they have been called. Maybe the only solution

Bytecode manipulation to intercept setting the value of a field

痴心易碎 提交于 2019-12-01 05:49:25
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.setName (“Joe”) -- as class methods can be modified by bytecode manipulation, but I want to do the same

Inserting bytes in the middle of binary file

不羁的心 提交于 2019-11-30 12:07:52
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 the rest of the original file? Is this correct? How should I go on with it? It needs to work as fast as

Is it possible to inject code in an android application?

淺唱寂寞╮ 提交于 2019-11-30 07:03:01
问题 I would like to inject code in an android application at runtime. I have tried to use dx tool to generate a dexfile in the sdcard but when i want to instantiate, it fails. Are there any tools to inject code generating new dalvik bytecode? I am studing some libraries, aspecjt or guice for android. Is it better to work with a script language? Thanks people :) 回答1: No, it is not possible. Android application permissions would not work if that was possible. 回答2: Dexmaker is new and designed just