java-bytecode-asm

Why doesn't ASM call my ``visitCode``?

混江龙づ霸主 提交于 2019-12-24 00:58:54
问题 I'll add my code to the end of this post. I'm using byteBuddy 1.7.9 and whatever ASM version comes with that. In a nutshell I have byte[] rawClass = ...; ClassReader cr = new ClassReader(rawClass); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); MethodAdder ma = new MethodAdder(Opcodes.ASM5,cw); cr.accept(ma,ClassReader.EXPAND_FRAMES); Where in MethodAdder , I want to add a static initialiser: @Override public MethodVisitor visitMethod(int access, String name, String desc,

Expecting a stackmap frame - Java 8

限于喜欢 提交于 2019-12-23 05:28:04
问题 I am getting this error "Expecting a stackmap frame at this location". I am using Java 8. I know that for Java 7 there is a workaround to use -XX:-UseSplitVerifier to use the less strict verification method. However that option was removed in Java 8. I was wondering if there is any other solution for this. Switching to an earlier Java is not an option. 回答1: The option -XX:-UseSplitVerifier was intended to give bytecode library and tool developers time to catch up and fix problems with

Inserting to InsnList before several nodes

醉酒当歌 提交于 2019-12-23 02:56:11
问题 I am trying to: 1) Iterate instructions and find all relevant nodes 2) Insert custom code before found nodes I used streams and iterator to make it and insert, which works only for the first node InsnList instructions = methodNode.instructions; InsnList addition = ... //It work: found n nodes for n return instructions Stream<AbstractInsnNode> returnNodes = Stream.iterate(instructions.getFirst(), AbstractInsnNode::getNext).limit(instructions.size()) .filter(n -> returnOpcodes.contains(n

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;

How to change static variable value using ASM?

女生的网名这么多〃 提交于 2019-12-20 05:53:37
问题 I started learning Java Agent few days ago. But documentation is not very good and beginners like me struggling to understand the basics. I created a basic multiplier class and export it to runnable jar using eclipse. Here is the code snippet. Main jar file: public class Multiplier { public static void main(String[] args) { int x = 10; int y = 25; int z = x * y; System.out.println("Multiply of x*y = " + z); } } Bytecode for above class Now I want to manipulate the value of x from an agent. I

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

ASM Get exact value from stack frame

孤人 提交于 2019-12-17 16:52:59
问题 I have some method, which contains instrustion like ILOAD, and I want in some way to get value of stack after this instruction. Not just type, but exact value. I know I need to emulate method execution in order to do that, but I don't know how to make this properly. I have such method for test called main : sipush 15649 istore_0 /* c */ getstatic java/lang/System.out:Ljava/io/PrintStream; bipush 45 bipush 11 iload_0 /* c */ ... I want to get value, loaded by iload_0 . I tried to make Analyzer