Questions tagged [java-bytecode-asm]

ASM is a Java library used for JVM bytecode manipulation and creation.

ASM is a Java bytecode manipulation and analysis framework. It provides a library to read, write, transform, and analyze compiled Java classes, directly in the binary form of byte arrays. Its API allows easy access to class items such as annotations, fields, and methods. Instructions in methods can be analyzed, removed, or inserted as necessary. Output classes can be saved into files, or loaded dynamically by a ClassLoader.

Website: http://asm.ow2.org/

Document: ASM 4.0 A Java bytecode engineering library

798 questions
7
votes
1 answer

Using an Annotation Processor to create a list of classes with a certain annotation

I have a custom annotation that I've implemented and I'd like to use an annotation processor to generate a list of all the classes in my app that use that particular annotation. I've found this tutorial which describes how to generate a class file…
emmby
  • 99,783
  • 65
  • 191
  • 249
7
votes
1 answer

Understanding how to use visitFrame

I am reading in a bunch of classes from a JAR file in which I plan to inject a simple method (and then dump the new jar) in Java which posts some data to a PHP file: public static void post(final String n, final String o){ try{ final URL…
Josh M
  • 11,611
  • 7
  • 39
  • 49
7
votes
2 answers

Invalid Entry Compressed Size

I'm using a bytecode library known as ASM to alter classfiles, then I want to write each classfile back into a jar file rather than a folder filled with class files. I do this by running this code: My problem occurs when a ZipException is throw for…
6
votes
3 answers

ASM or CGLIB analog for Dalvik

I'm looking for a CGLIB analog usable for Dalvik bytecode. Is there such library in the Android world? Maybe, there is a way to translate CGLIB result to Davlik bytecode on-the-fly?
uhbif19
  • 3,139
  • 3
  • 26
  • 48
6
votes
3 answers

ASM - How can I convert Java class name from Java bytecode name?

I'm using ASM (a bytecode modification library) and it provides access to type names in the bytecode naming format, for example a String field is reported to have the description: Ljava/lang/String I need to invoke Class.forName for some classes,…
mahonya
  • 9,247
  • 7
  • 39
  • 68
6
votes
2 answers

Manupulating byte code generated from ASM

I want to generate byte code for a java class only with the public or protected fields, constructors, and methods. I am trying with the below code, but I don't know is it the correct approach? Client code: String sourceFileName =…
Shashwat
  • 2,342
  • 1
  • 17
  • 33
6
votes
1 answer

What is the use case for adding non-exported/non-opened packages to a module-info's ModulePackages?

The JVMS states in section 4.7.26 that: The ModulePackages attribute indicates all the packages of a module that are exported or opened by the Module attribute, as well as all the packages of the service implementations recorded in the Module…
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
6
votes
1 answer

Jvm: At what locations in the method bytecode does a compiler need to necessarily specify stack map frames?

This is an excerpt from the asm user guide: In order to save space, a compiled method does not contain one frame per instruction: in fact it contains only the frames for the instructions that correspond to jump targets or exception handlers,…
saga
  • 1,933
  • 2
  • 17
  • 44
6
votes
3 answers

What is vmovdqu doing here?

I have a Java loop that looks like this: public void testMethod() { int[] nums = new int[10]; for (int i = 0; i < nums.length; i++) { nums[i] = 0x42; } } The assembly I get is this: 0x00000001296ac845: cmp …
An SO User
  • 24,612
  • 35
  • 133
  • 221
6
votes
1 answer

ASM Get exact value from stack frame

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…
i0xHeX
  • 315
  • 2
  • 15
6
votes
1 answer

Use of stackmap frames and how it helps in byte code verification?

I have been trying to get my head around the obscure stack map frame and it's role in making the verification of a dynamically loaded class in just a single pass. Few stack overflow answers & other resources which I found immensely helpful are Is…
KodeWarrior
  • 3,538
  • 3
  • 26
  • 40
6
votes
5 answers

Is there a way to tell if a class is an interface?

I'm trying to examine (at a bytecode level, ASM) classes implementing some specific interfaces (in this case, java.sql.Connection) and find that in some cases, the library has another interface extending something from my set of interfaces... and…
ticktock
  • 1,593
  • 3
  • 16
  • 38
6
votes
1 answer

Is bytecode manipulation safe

Performing bytecode manipulation using APIs like javaassist modify class files after compilation. But, if the java code is optimized can't the modifications be performed in the wrong place? Are there ways to avoid this problem? Is the story any…
6
votes
0 answers

Different behaviors between Unsafe defineAnonymousClass and ClassLoader

I used classloader and Unsafe::definedAnonymous() to load generated bytecode byte[]. The usage of Class returned by classLoader.loadClass() succeeds while it fails with c.getMethod() in which c=Unsafe.defineAnonymousClass() API. So is the generated…
6
votes
3 answers

Embed the existing code of a method in a try-finally block (2)

Some time ago, I asked in Embed the existing code of a method in a try-finally block how to wrap the body of a method in a try-finally block using ASM. The solution was to visit a label for the try block at the beginning of the method body in…