java-bytecode-asm

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

Method code too large! exception using ASM

徘徊边缘 提交于 2019-12-01 20:55:39
问题 I am iterating over one class using ASM code without manipulating any byte code. But when I am converting classwriter to bytearray(cw.toByteArray()), I am getting Method code too large! exception. Can anybody tell me when does this happen .. My code snippet is as follows --- InputStream in= new FileInputStream("D:/AshiqWorkspace/RandD/ByteCodeStudy/temp/GameManager.class"); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES); ClassVisitor ca = null; ca = new

How do I overcome the “VerifyError:Expecting a stackmap frame” for a JDK 7/8 application?

北战南征 提交于 2019-12-01 10:32:22
I am using ASM 5.0.3 bytecode modification library with Tomcat 8 and JDK 8. My intention is to inject bytecode successfully into all the classes. However, I encountered the following error: java.lang.VerifyError: Expecting a stackmap frame at branch target 18 Exception Details: Location: com/sun/crypto/provider/SunJCE.getInstance()Lcom/sun/crypto/provider/SunJCE; @0: getstatic Reason: Expected stackmap frame at this location. Bytecode: 0x0000000: b200 0bc7 000b bb00 3659 b700 0cb0 b200 0x0000010: 0bb0 bf Exception Handler Table: bci [0, 18] => handler: 18 Stackmap Table: append_frame(@14

How do I overcome the “VerifyError:Expecting a stackmap frame” for a JDK 7/8 application?

怎甘沉沦 提交于 2019-12-01 09:40:53
问题 I am using ASM 5.0.3 bytecode modification library with Tomcat 8 and JDK 8. My intention is to inject bytecode successfully into all the classes. However, I encountered the following error: java.lang.VerifyError: Expecting a stackmap frame at branch target 18 Exception Details: Location: com/sun/crypto/provider/SunJCE.getInstance()Lcom/sun/crypto/provider/SunJCE; @0: getstatic Reason: Expected stackmap frame at this location. Bytecode: 0x0000000: b200 0bc7 000b bb00 3659 b700 0cb0 b200

Java ASM GeneratorAdapter variable naming

江枫思渺然 提交于 2019-12-01 05:51:58
问题 I am generating a simple class and unable to inject a proper variable name. ASM version is 5.2 . Here is the code: package com.test; import org.objectweb.asm.*; import org.objectweb.asm.commons.GeneratorAdapter; import org.objectweb.asm.commons.Method; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); String name = "com.test.Sub"; cw.visit(Opcodes

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

Finding the Bytecode Size of a Method

旧巷老猫 提交于 2019-12-01 04:10:15
I am trying to figure out the bytecode size of a method because I want to be sure that it will be small enough to be inlined by compiler optimizations. I saw that the default max size for inlining methods is 35, so if the method is larger than that I will revise the code or break it into multiple methods. I have a method that generates the bytecode below (disassembled via the ASM Bytecode Outline plugin for IntelliJ IDEA). How can I tell the bytecode size of that method? the LINENUMBERs seem to reference the line numbers of the original source code. TIA public static mergeNativeArrays([Ljava

Finding the Bytecode Size of a Method

别来无恙 提交于 2019-12-01 00:47:29
问题 I am trying to figure out the bytecode size of a method because I want to be sure that it will be small enough to be inlined by compiler optimizations. I saw that the default max size for inlining methods is 35, so if the method is larger than that I will revise the code or break it into multiple methods. I have a method that generates the bytecode below (disassembled via the ASM Bytecode Outline plugin for IntelliJ IDEA). How can I tell the bytecode size of that method? the LINENUMBERs seem

How to get bytecode as byte array from Class

回眸只為那壹抹淺笑 提交于 2019-11-30 20:50:30
问题 Given an arbitrary Class instance, including one that's runtime generated (no .class file on disk), is there any way to get the class bytes? 回答1: In general, this is not possible. While loading a class, JVM parses its bytecode and converts it to the internal representation. After that JVM is free to forget the original bytecode, and that's what really happens with HotSpot JVM. However, with the certain hacks it is possible to inspect the internal class representation and convert it back to a

Is it possible to have the System ClassLoader load .class files specified at run time?

三世轮回 提交于 2019-11-30 15:25:14
I am writing a static analysis tool for an assignment, it analyses Java bytecode using the ASM library. One of the parts of ASM that we use requires (or at least, appears to require) that the class be loaded from the ClassLoader. We were hoping the tool would be able to analyse .class files without requiring them on the classpath. We already load the .classes from a specified directory at run time and read them in using an InputStream. This is acceptable for ASM in most cases. There are some classes, such as SimpleVerifier , which attempt to load the classes though. Is it possible, under this