Questions tagged [byte-buddy]

Byte Buddy is a code generation and manipulation library for creating and modifying Java classes during the runtime of a Java application and without the help of a compiler. Byte Buddy allows the creation of arbitrary classes and is not limited to implementing interfaces for the creation of runtime proxies. Furthermore, Byte Buddy offers a convenient API for changing classes either manually, using a Java agent or during a build.

Byte Buddy is a code generation and manipulation library for creating and modifiying Java classes during the runtime of a Java application and without the help of a compiler. Other than the code generation utilities that ship with the Java Class Library, Byte Buddy allows the creation of arbitrary classes and is not limited to implementing interfaces for the creation of runtime proxies. Furthermore, Byte Buddy offers a convenient API for changing classes either manually, using a Java agent or during a build.

In order to use Byte Buddy, one does not require an understanding of Java byte code or the class file format. In contrast, Byte Buddy's API aims for code that is concise and easy to understand for everybody. Nevertheless, Byte Buddy remains fully customizable down to the possibility of defining custom byte code. Furthermore, the API was designed to be as non-intrusive as possible and as a result, Byte Buddy does not leave any trace in the classes that were created by it. For this reason, the generated classes can exist without requiring Byte Buddy on the class path. Because of this feature, Byte Buddy's mascot was chosen to be a ghost.

Byte Buddy is written in Java 6 but supports the generation of classes for any Java version. Byte Buddy is a light-weight library and only depends on the visitor API of the Java byte code parser library ASM which does itself not require any further dependencies.

749 questions
3
votes
1 answer

How to avoid NoClassDefFoundError when using ByteBuddy to implement OpenTracing

I'm trying to implement a Java Agent that can easily be added to the commandline of some of our legacy services to enable distributed tracing using the OpenTracing API. I'm using ByteBuddy (1.9.12) and hitting NoClassDefFoundErrors when the…
Randall T.
  • 187
  • 12
3
votes
1 answer

Can byte-buddy by used as non-reflective way to collect [field]/[method return] values as an collection or map?

I was trying to find usable answer but with no luck. I wonder if it would be possible to use byte-buddy for following: Let's say we have a POJO with number of values. For some specific kind of processing i am interested only in some. I could mark…
3
votes
1 answer

Can I only subclass with Interfaces when I want to intercept a method?

I'm trying to use byte buddy to intercept methods but have found that it only appears to work when you sub-type an interface. The code snippet below demonstrates this, you can see "hello" being passed into the same "message" method of the 3 types…
sbnarra
  • 556
  • 4
  • 9
  • 25
3
votes
1 answer

Byte Buddy instantiate class without parameters for constructor

I am trying to instantiate a class that doesn't have an empty parameter constructor (and it's direct parent also doesn't have an empty parameter constructor) Class newClass = new ByteBuddy(); .subclass(BufferedImage.class) …
IpFruion
  • 35
  • 3
3
votes
1 answer

ASM - strange localVar index using newLocal from LocalVariableSorter

I'm adding new locals via newLocal from LocalVariableSorter. The method I'm adding the locals to is an instance method with a long parameter. I'm adding two locals; one long, one object. There are no other local vars in the sample code. As a result…
3
votes
1 answer

Byte Buddy Generated class not visible to Orika (Javaassist)

I use Byte Buddy to generate some DTO classes in a Spring Boot application. I also use the Orika mapper librarie to map Entity to/from DTO classes. This library uses another runtime code generation tool to generate mapper classes, which is…
sbraconnier
  • 465
  • 5
  • 11
3
votes
1 answer

Redefine/Rebase native method

I am trying to replace Java bootstrap class method System.currentTimeMillis with ByteBuddy. However after spent more than one day I still don't get the expected result. Following is my code:- The inteceptor class in bootstrap.jar used for…
Tatera
  • 448
  • 3
  • 11
3
votes
1 answer

Instrumenting multiple methods within same class with ByteBuddy

I'm currently trying to make a logger agent where I'm currently intercepting the class PrepareStatement. Within PrepareStatement there are multiple methods which I wish to trace, but I'm having a feeling that I'm doing it wrong. Typically what I do…
Solve Johnsen
  • 67
  • 1
  • 1
  • 7
3
votes
1 answer

Generate parameter annotations in Byte Buddy

I would like to use ByteBuddy to generate simple interfaces like this: public interface MyInterface { void myMethod(@Deprecated String myDeprecatedParameter); } This is just an example, but the point is that the parameters of the methods need a…
raner
  • 1,175
  • 1
  • 11
  • 21
3
votes
2 answers

Tell bytebuddy to "not care" about generic information

So I ran into Exception in thread "Thread-0" java.lang.IllegalArgumentException: Unknown type: null at net.bytebuddy.description.type.TypeDefinition$Sort.describe(TypeDefinition.java:213) at…
User1291
  • 7,664
  • 8
  • 51
  • 108
3
votes
1 answer

How to define a new method inside an existing class and add a call to it in an existing method inside the same class using bytebuddy?

I have a class which looks like below public class HelloWorld{ public void sayHelloWorld(){ System.out.println("Hello World"); } } Now I would like to add another method to the HelloWorld class using bytebuddy and add a call to the new…
Kaarthik
  • 627
  • 2
  • 12
  • 32
3
votes
2 answers

Redefine non-static methods

I try to redefine simple non-static method but I get an exception: Exception in thread "main" java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields) Classes: class Source { def…
nigredo
  • 41
  • 2
3
votes
2 answers

Create dynamic / runtime (and simple) bean from JSON String

In what way can Java generate a Bean (not just a Map object)--a bean with fields, getters, and setters from a JSON String. Here's the code I am trying now using ByteBuddy (non-working code/error): Object contextObject = new ByteBuddy() …
quarks
  • 33,478
  • 73
  • 290
  • 513
3
votes
2 answers

Chain/transform method calls with ByteBuddy

Using ByteBuddy, can I implement one instance method by calling another and transforming the result? For instance (toy example): public abstract class Foo { public String bar() { return "bar"; } public abstract int baz(); } Given the…
David Moles
  • 48,006
  • 27
  • 136
  • 235
3
votes
1 answer

Byte buddy stack manipulation: how to work with local variables and if statements

Generally, the questions are: How and at which moment ByteBuddy generates local variables table and stackmap frames? What is the correct way to work with local variables and generate if statements in ByteBuddy's Implementation API? Details: I am…
skapral
  • 1,128
  • 9
  • 26