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

Is it possible to access local variables with ByteBuddy's Advice?

When intercepting a method's implementation with an @Advice, is it possible to access local variables?
Carlos Melo
  • 3,052
  • 3
  • 37
  • 45
5
votes
1 answer

Intercepting default constructor with Byte Buddy

I'm trying to intercept constructor calls with Byte Buddy, this is my sample code: package t; import static net.bytebuddy.dynamic.loading.ClassLoadingStrategy.Default.INJECTION; import static net.bytebuddy.implementation.MethodDelegation.to; import…
Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
5
votes
1 answer

How do I build an concrete implementation of a Java Class from an interface using Byte-Buddy?

I have an interface, lets say it looks like this. public interface TestObject { String getString(); Long getLong(); } I want to actually build a concrete implementation of this object using ByteBuddy. Here's what I tried. public class…
Kevin Daly
  • 93
  • 1
  • 7
4
votes
2 answers

Issue with using mockito with mock-maker-inline in Spring boot application

I have a Spring web application that can operate in “special” mode where mockito is used to spy on certain objects. Some of those objects are final (protobuf messages). I know, this may smell like a bad idea but lets say it’s an experiment. When…
marcin_koss
  • 5,763
  • 10
  • 46
  • 65
4
votes
1 answer

How to augment methods during ByteBuddy transformation?

Context I am implementing byte code transformations with ByteBuddy and the process of manipulation is a multi step process. Because of that, the manipulation has to be able to: augment originally existing methods create new methods entirely augment…
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
4
votes
1 answer

ByteBuddy - unable to intercept static method from superclass

I'm working on command line tool for Android (think of am), trying to utilize the power of ByteBuddy to stub the static method getApplicationContext defined in android.security.KeyStore However - the method seem to be invisible to ByteBuddy…
4
votes
1 answer

Byte-Buddy: Method Interception InvocationHandler vs MethodDelegation to GeneralInterceptor

I am using Byte-Buddy to dynamically generate implementations of Java interface methods, and to delegate the calls to these methods to a single method of an existing proxy object. The first version was inspired by How to create a dynamic proxy using…
FlyingSheep
  • 804
  • 1
  • 9
  • 20
4
votes
1 answer

Classpath problems while Instrumenting Springboot application

I have a springboot application which I'm trying to instrument using bytebuddy. I'm running into classpath issues which I'm not able to understand. Firstly, the following is other literature on…
prongs
  • 9,422
  • 21
  • 67
  • 105
4
votes
2 answers

Disable validation of identifiers in Byte Buddy

I am working on a JVM-based programming language and I use Byte Buddy for the code generator. The language is somewhat similar to Java but typically uses annotations where Java would use keywords. Some example annotations are public, private,…
raner
  • 1,175
  • 1
  • 11
  • 21
4
votes
1 answer

How can I create an enum with fields initialized by each enum constant using Byte Buddy?

I would like to generate an enum containing a field using Byte Buddy, where each enum constant passes a different argument to the constructor. public enum MyEnum { private final String blah; A("foo") B("bar"); private MyEnum(String…
Michael
  • 41,989
  • 11
  • 82
  • 128
4
votes
3 answers

how to proxy an existing object using ByteBuddy

I would like to use AOP to automatically add some functionality to annotated classes. Suppose, for example, that there is an interface (StoredOnDatabase) with some useful methods to read and write beans from a database. Suppose that there are…
Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
4
votes
1 answer

Define method body with Byte buddy instead of MethodDelegation

I am trying to generate a class and methods in it, using Byte Buddy, based on some configuration that is available at runtime. The class is trying to create a Hazelcast Jet pipeline to join multiple IMaps. Based on the provided configuration, the…
Anoop
  • 813
  • 2
  • 10
  • 24
4
votes
0 answers

Mockito + Espresso + Dagger2 => ClassNotFoundException when running SDK is lower than target SDK

I came across this limitation when trying to mock a class that refers to classes present in SDK >= 26 and executed the test in a device running SDK 24. I created a test app to better understand the problem. open class RandomStuff{ …
4
votes
1 answer

How to intercept field accesses (without getter/setter) using Bytebuddy

I am trying to use bytebuddy to intercept getfield and putfield accesses. I have read the rather comprehensive documentation on the site, but from what I can understand, it covers adding getters and setters to fields, rather than intercepting field…
4
votes
1 answer

How to take the exception thrown by a constructor using a ByteBuddy agent?

I'm trying to log every call, returned objects and exceptions thrown in methods and constructors using a ByteBuddy (v1.7.9) java agent, without iterfering with the normal functioning of the instrumented code. My current instantiation of the agent…
1 2
3
49 50