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
2 answers

ByteBuddy, is it possible to assign a class to a generic when subclassing a parameterized class

If I have the following: public abstract class Parameterized { protected abstract String foo(); } Is it possible to do something like: DynamicType.Unloaded> subclassed = new ByteBuddy() …
MMP
  • 546
  • 2
  • 5
  • 19
3
votes
1 answer

How can I check if an object was created by byte buddy?

I create an Source object instance using the following code Source source = new ByteBuddy() .subclass(Source.class) .method(named("hello")) .intercept(MethodDelegation.to(Target.class)) …
Liviu Carausu
  • 61
  • 1
  • 5
3
votes
1 answer

Intercept constructors during rebase in ByteBuddy

What I try to do I am trying to rebase and rename class to intercept it's constructor with Bytebuddy 1.6.7 Motivation I am working on SAAS system, where user can provide annotated java classes, system should instrument them before storing or…
Mikalai Parafeniuk
  • 1,328
  • 15
  • 10
3
votes
1 answer

Error while redefining a method with ByteBuddy: "class redefinition failed: attempted to add a method"

I'm learning Byte Buddy and I'm trying to do the following: create a subclass from a given class or interface then replace a method in the subclass Note that the subclass is 'loaded' in a ClassLoader before one of its method (sayHello) is…
Xavier Coulon
  • 1,580
  • 10
  • 15
3
votes
1 answer

Redefine java.lang classes with ByteBuddy

I'm trying to redefine classes on the java.lang package such as String.class or Integer.class using ByteBuddy but with no success. My question is if that's even possible? This is the code I'm trying in my java agent: public static void…
João Dias Amaro
  • 511
  • 6
  • 14
3
votes
1 answer

Maven plugin instead of javaagent for Byte Buddy?

can there be a maven plugin instead of javaagent to reduce startup time of an application? Many ORM tools have both javaagent and maven plugin, so it should be possible - is it? Or at least something like "CompiledClassFilesBuilder" similar in…
Pavel Arnošt
  • 97
  • 2
  • 8
3
votes
1 answer

ByteBuddy: java.lang.IllegalArgumentException: Cannot attach undefined variable: V

When rebasing the class com.google.common.collect.ImmutableMultimap$Values from Google Guava, I get an IllegalArgumentException from ByteBuddy. The inner class is a subclass of com.google.common.collect.ImmutableCollection whose method…
erikh
  • 146
  • 9
3
votes
1 answer

ByteBuddy subclass constructor

I am trying to create a subclass of an abstract class in bytebuddy and want to override the constructor with my own function. I can not make it work with defineConstructor. Superclass: public abstract class AbstractDMTable { protected…
Pouyan
  • 499
  • 1
  • 5
  • 14
3
votes
1 answer

How to add a field to a class in ByteBuddy and set / get that value in a method interceptor

I am using byte-buddy to build an ORM on top of Ignite, we need to add a field to a class and then access it in a method interceptor.. So here's an example where I add a field to a class final ByteBuddy buddy = new ByteBuddy(); final Class
Kevin Daly
  • 93
  • 1
  • 7
3
votes
1 answer

Implementing around-advice with a MethodDelegation

I'm having great fun with the method delegation described here: http://www.javacodegeeks.com/2015/01/make-agents-not-frameworks.html This works nicely: .intercept(MethodDelegation.to(LogInterceptor.class) …
3
votes
1 answer

ByteBuddy and annotations

I have a bunch of WebServices running on plain JDK and I need to intercept all public methods in order to do something. Some methods are using @WebParam annotation. Subclassing the WebService with ByteBuddy drops the @WebParam annotation from the…
Paci
  • 51
  • 3
3
votes
2 answers

Why does Byte Buddy lack a StackManipulation implementation corresponding to the opcode ASTORE?

If its absence is because byte-buddy aims at the method delegation domain, then I can provide a scenario where this feature is necessary: private Object invokeSpi(Object spi, Object... params) { Reducer reducer = (Reducer) spi; return…
Winter Young
  • 801
  • 1
  • 7
  • 14
3
votes
1 answer

Can I redefine private methods using Byte Buddy?

Is it possible to use Byte Buddy to redefine a private method of a class? It seems that the entry point into using Byte Buddy is always sub-classing an existing class. When doing this, it is obviously not possible to redefine a private method of the…
chschroe
  • 149
  • 1
  • 5
2
votes
2 answers

What is the difference between withEnclosingType() and withDeclaringType() in InstrumentedType?

I'm an experienced Byte Buddy user. What practical effect does InstrumentedType#withDeclaringType(TypeDescription) have over InstrumentedType#withEnclosingType(TypeDescription)? When do I use one versus the other?
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
2
votes
2 answers

Creating a proxy with ByteBuddy that should call a protected method, I get: java.lang.VerifyError: Bad access to protected data in invokevirtual

I'm trying to create a proxy with ByteBuddy that can delegate calls of a protected method getRawId of a MyEntityA class to the same method of an object of the same class referenced in a target field. package it.mict.lab.bytebuddy.entity; public…