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
30
votes
7 answers

Mockito fails with inlined mocks enabled with Invalid paramter name exception

I've enabled inlined mocks so that I can mock final classes but now I get the following exception when running tests with Junit. When I run all the tests at once only two tests fail, but if I run tests individually then most of them fail. I'm not…
mekazu
  • 2,545
  • 3
  • 23
  • 21
16
votes
7 answers

Mockito 2 for Android Instrumentation test : Could not initialize plugin: interface org.mockito.plugins.MockMaker

I'm using Mockito 2 and Espresso for Unit tests, and met a problem trying to use Mockito 2 in Instrumentation test. While running the test, I'm getting this error java.lang.IllegalStateException: Could not initialize plugin: interface…
Arshak92
  • 634
  • 3
  • 10
  • 24
14
votes
3 answers

mock-maker-inline - Could not initialize inline Byte Buddy mock maker (power-mockito + jdk 8 + bytebuddy)

I am using Powermockito to mock and spy static classes. Version: 2.0.4 Also using mockito-core version 3.0.0. In, one of my projects, i am able to mockStatic by enabling mock-maker-inline as specified…
DecKno
  • 293
  • 1
  • 5
  • 21
14
votes
4 answers

Can I dynamically generate and reference a class in a source file in the same Maven project?

In a Maven Build, I am dynamically generating some Java types using a byte code generation library (Byte Buddy). Naturally, these class files don't have corresponding source files. Only a few classes would be generated this way. The majority of the…
Mark Chesney
  • 1,082
  • 12
  • 20
11
votes
2 answers

Display generated bytebuddy bytecode

I am using ByteBuddy to create a class at runtime with dynamically generated byte code. The generated class does what it is intended to do, but I want to manually inspect the generated byte code, to ensure it is correct. For example Class
bramp
  • 9,581
  • 5
  • 40
  • 46
10
votes
2 answers

What does "No compatible attachment provider is available" mean

Recently I developed a java agent using byte buddy. The development in eclipse was fine, then I put the agent to production and the message: No compatible attachment provider is available along with some stacktraces originating from the lines…
CoronA
  • 7,717
  • 2
  • 26
  • 53
10
votes
1 answer

Byte Buddy: Create implementation for an abstract class

I would like to create an implementation at runtime for an abstract class using Byte Buddy and I'm facing the issue, that a java.lang.AbstractMethodError is being thrown when invoking a method from a created instance. I have an existing abstract…
qqilihq
  • 10,794
  • 7
  • 48
  • 89
9
votes
2 answers

Byte-buddy: generate classes with cyclic types

I'm trying to generate classes with a cyclic class dependency, similar to this question: Byte Buddy - Handling cyclic references in generated classes As a minimal example, the kind of classes I want to generate have dependencies like this: //class A…
9
votes
1 answer

How to create a dynamic proxy using ByteBuddy

In Java it is possible to create dynamic proxies using an implementation of InvocationHandler. Despite JVM optimizations, using reflection will always have some overhead invoking a method. To try to solve this problem, I tried to use ByteBuddy to…
Carlos Melo
  • 3,052
  • 3
  • 37
  • 45
9
votes
1 answer

How to implement a wrapper decorator in Java?

The problem is to create a dynamic enhanced version of existing objects. I cannot modify the object's Class. Instead I have to: subclass it wrap the existing object in the new Class delegate all the original method calls to the wrapped…
Teg
  • 1,302
  • 13
  • 32
8
votes
1 answer

Intercepting calls to Java 8 lambda-expressions using Byte Buddy

I try to intercept calls to methods and calls to Java 8 lambda expressions using a Byte Buddy AgentBuilder as follows: static { final Instrumentation inst = ByteBuddyAgent.install(); new AgentBuilder.Default() …
Peti
  • 1,670
  • 1
  • 20
  • 25
7
votes
2 answers

What is the proper way to instrument classes loaded by bootstrap / extension class loader?

I finally wrote a Java agent with Byte Buddy which uses the Advice API to print a message upon entering and leaving a method. With my current configuration, this agent appears to apply on classes loaded by the Application ClassLoader only. However,…
AntoineG
  • 93
  • 7
7
votes
1 answer

Using mockito-inline in project throws "Mockito cannot mock this class" Error

I am using Mockito 2.23.4 and byte-buddy 1.9.3 in my project. I wanted to mock final classes and methods, so I used mockito-inline following this post Mockito mock final classes and methods and setting mock-maker-inline inside…
Gregory Javis
  • 105
  • 1
  • 4
  • 10
7
votes
1 answer

Hibernate NoSuchMethodError in Byte buddy when Building Session Factory

I've been searching all night and I can't seem to find a solution anywhere. Every time I try and log into the database from my application, I get this: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at…
jysk131
  • 131
  • 1
  • 6
7
votes
1 answer

Adding annotation with a given value using Byte Buddy

How can I use Byte Buddy to add an annotation with a given value? I'm playing around with generating test classes for JUnit and I'd like to annotate a generated class by @RunWith(SomeRunner.class).
Kimble
  • 7,295
  • 4
  • 54
  • 77
1
2 3
49 50