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
7
votes
1 answer

How to create a default constructor with Byte Buddy

I want to intercept some method calls on one of my classes but those classes dont have a default constructor. Given the following class, how would I setup Byte Buddy to also create a public no-argument constructor to be able to create the generated…
leozilla
  • 1,296
  • 2
  • 19
  • 29
7
votes
1 answer

Can Byte Buddy access local variable name of a method?

Suppose I have a method m: public void m() { String foo = "foo"; int bar = 0; doSomething(foo, bar); } I want to use ByteBuddy to instrument the code so that when calling doSomething in m, it will automatically put the value of foo and bar…
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
7
votes
1 answer

ByteBuddy Proxy Interface

I am trying to convert Cglib proxy to ByteBuddy. Cglib has net.sf.cglib.proxy.Proxy interface to intercept all method calls. I check the documentation of ByteBuddy but couldnt find such an example. Without such interface for every object that i…
user452425
6
votes
1 answer

Interceptor class visibility in Byte Buddy

Byte Buddy seems to only appreciate public classes as interceptor implementations even if I provide the actual instance; frequently I find myself wanting to do something like this: import static MethodDelegation.to; new…
Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
6
votes
1 answer

Error using Byte Buddy on Android

I'm trying to use Byte Buddy library in Android but I get an error: java.lang.IllegalStateException: This JVM's version string does not seem to be valid: 0 I have coded nothing yet, just: ByteBuddy test = new ByteBuddy(); in my App.java I have…
Héctor
  • 24,444
  • 35
  • 132
  • 243
6
votes
1 answer

After and before constructor interceptor

I know how to create a BEFORE constructor interceptor: return builder.constructor(isDeclaredBy(typeDescription)) .intercept(MethodDelegation.to(constructorInterceptor) .andThen(SuperMethodCall.INSTANCE)); I know how to create an AFTER…
user3408654
  • 301
  • 1
  • 13
6
votes
1 answer

Problems with instrumentation using Byte Buddy

I have Byte Buddy up and running as an agent and it is successfully intercepting the vast majority of my codebase, which is quite large by the way! There are though a few outliers which I could not instrument and which I have documented below in the…
user1563817
  • 121
  • 1
  • 5
6
votes
1 answer

Using Byte Buddy for Java Agent

I wish to create an agent to attach to our live Tomcat & Weblogic servers which will intercept all method calls to all classes declared in my companies package and do some logging of metrics such as execution time. I came across the Byte Buddy…
user1563817
  • 121
  • 1
  • 5
5
votes
1 answer

How do I compose two Implementations?

I have an Implementation resulting from the return value of Advice#wrap(Implementation), and a FixedValue. I need to turn these into a single Implementation that does not result in a bytecode verification error. That is: Implementation…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
5
votes
1 answer

How do I install and use a constant MethodHandle in ByteBuddy?

I'm playing with the support that ByteBuddy has for constant MethodHandles. I am trying to (effectively) look up a MethodHandle on one class, and then use it from a ByteBuddy-generated subclass. (I am aware that I could do this using a static…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
5
votes
1 answer

Add method annotation at runtime with Byte Buddy

I've been searching for the answer to "how to add an annotation to the method at runtime" for several days already and found this awesome tool called Byte Buddy, played with it, but still cannot make it work as I need to. I'm sure it must be able to…
lub0v
  • 801
  • 9
  • 17
5
votes
1 answer

Is it possible to return from a method using @Advice.OnMethodEnter?

Using Byte Buddy's advice API, is it possible to return from the instrumented method without actually executing it? One use case would be to implement a cache and to return the cached value, if present, instead of computing the value…
Felix
  • 5,804
  • 4
  • 25
  • 37
5
votes
1 answer

Byte Buddy - define constructor with call to super class and initialize field

I have a class such as: public class Sample{ private String a; private String b; public Sample(String a, String b) { this.a = a; this.b = b; } public String getA() {return a;} public String getB() {return b;} } I want to create a…
Rotem ben
  • 156
  • 2
  • 13
5
votes
1 answer

Define field with generic type using ByteBuddy

I just started playing with ByteBuddy and I am working on a couple examples in order to get the hang of it. What I am trying to accomplish with this exercise is to replace some code that uses ASM, with ByteBuddy. So far I have been successful when…
geoand
  • 60,071
  • 24
  • 172
  • 190
1
2
3
49 50