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 get a Java method's parameter's actual name from a MethodDescription with Byte Buddy?

Inspired by this article http://mydailyjava.blogspot.com/2022/02/using-byte-buddy-for-proxy-creation.html, I managed to intercept a method invocation, check all the method parameter's values, and return a mock response. However, I cannot get the…
Bing Ren
  • 1,589
  • 17
  • 26
3
votes
1 answer

VerifyError when redefining Spigot classes using ByteBuddy agent

I'm currently trying to redefine the CraftPlayer class in spigot to add more events. I'm using Byte-Buddy and ASM to edit the classes using this utility class. However, even when running CraftPlayer::class.java.redefine { } (which doesn't change…
ByteZ
  • 131
  • 1
  • 6
3
votes
1 answer

Bytebuddy - Stackmanipulation implement lambda call

I would like to generate follwing method with bytebuddy stackmanipulation. public void test() { this.process(() -> { System.out.println("Im lambda call"); } } if such code would be compiled with javac it would produce: private…
user2749903
  • 1,275
  • 1
  • 10
  • 20
3
votes
1 answer

What is the ByteBuddy recipe for building an upper-bounded wildcard?

I know some of this, but not all of it. Most notably, I am aware of TypeDescription.Generic.Builder but I have a very specific question about it. Suppose I want to build Supplier>. Suppose further that all I know I have is a…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
3
votes
1 answer

How to stop/halt the main program/thread from a java agent

I have a gradle test task which runs a list of tests from a given file. Sometimes, any particular test execution simply gets stuck and does not move on to execute the next test(s) in the list. For this, I am trying to add a java agent which will…
Abhijith Gururaj
  • 437
  • 4
  • 14
3
votes
1 answer

How to pass InvokeDynamic as parameter to MethodCall in ByteBuddy

I want to generate byte code of a class which passes a method reference as argument to another method. e.g.: public class GeneratedClass { public GeneratedClass() { Test.foo((Function)Test::getId) } } Using ByteBuddy I can generate…
Rasoul
  • 53
  • 1
  • 5
3
votes
1 answer

Is there any method in ByteBuddy to convert a TypeDescription.Generic into an appropriate java.lang.reflect.Type?

(The surface area of the ByteBuddy API is overwhelmingly enormous, which is why I'm asking the question.) I'm aware that I can take a TypeDescription.Generic and determine its "sort" and proceed rather laboriously "by hand" from there, but often…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
3
votes
1 answer

Mockito cannot mock this class : Mockito can only mock non-private & non-final classes

For a university exam i was given to test some of apache bookkeeper classes/methods and in doing so i thought to use mockito in my parameterized test. Test without mockito works fine but when i try to mock an interface i get this…
CecBazinga
  • 53
  • 1
  • 1
  • 5
3
votes
1 answer

How to replace input arguments using ByteBuddy's @Advice.AllArguments?

I am using ByteBuddy's @Advice to transform my classes and it works fine until I try to replace input arguments. I have a FooService with a join method which just joins two strings with a space. public class FooService { public String…
Guan Hao
  • 136
  • 4
3
votes
1 answer

Objenesis dependency causes instantiation error

Just starting a new Gradle project. This test passes: def 'Launcher.main should call App.launch'(){ given: GroovyMock(Application, global: true) when: Launcher.main() then: 1 * Application.launch( App, null ) >> null } ...…
mike rodent
  • 14,126
  • 11
  • 103
  • 157
3
votes
1 answer

Intercepting and changing the return value of a static native method with byte-buddy (System.nanoTime())

I want to change what happens when System.nanoTime() is called for arbitrary Java programs (I want to shift back time to help container checkpoint/restore use cases). Whenever System.nanoTime() is called, I want to run the original System.nanoTime()…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
3
votes
5 answers

Access classLoader field via reflection

We have an application with custom classloader and I need to access classLoader field on given classes. However this field is not accessible via reflection :-( The JavaDoc for java.lang.Class is clear: // This field is filtered from reflection…
kosta5
  • 1,129
  • 3
  • 14
  • 36
3
votes
1 answer

How to use byte-buddy to create delegated class that can modify method returned value?

Now there is an interface, I want to use byte buddy to create a delegated implementation. For example, I have such an interface: public interface Foo { String foo(); } And I want to dynamically create a class like this: public class FooImpl…
MoonFruit
  • 1,490
  • 1
  • 11
  • 11
3
votes
1 answer

ByteBuddy generic method return cast to concrete type

I am trying to use byte buddy to work with protobuf data and having a hard time with the generic extensions APIs. DynamicType.Builder addSetter(final Class container, final GeneratedExtension
ekaqu
  • 2,038
  • 3
  • 24
  • 38
3
votes
1 answer

ByteBuddy: how to add local variable across enter/exit when transforming a method

I am trying to use ByteBuddy within a Java Agent to instrument some older libraries using OpenTracing. This is associated with the OpenTracing Contrib Java Special Agent project. I had this successfully working when using a private class member to…
Randall T.
  • 187
  • 12