byte-buddy

Can I redefine private methods using Byte Buddy?

≯℡__Kan透↙ 提交于 2019-12-05 21:40:30
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 parent class (at least not in a way that the redefined method is used in the parent class). Consider the following example: public class Foo { public void sayHello() { System.out.println(getHello()); } private String getHello() { return "Hello World!"; } } Foo foo = new ByteBuddy() .subclass(Foo.class) .method(named("getHello")).intercept

How in Java to assign to fields with Byte Buddy?

烂漫一生 提交于 2019-12-05 18:42:00
I'm having difficulty understanding the documentation for Byte Buddy . To help me learn the API I would like to generate the byte code equivalent of this Java: public final class GeneratedByByteBuddy { private final int a; public GeneratedByByteBuddy(final int a) { this.a = a; } } I've had difficulty working out the right way to use Instrumentation to create the field assignment. You are creating a class with customized byte code. For this, you cannot use a built-in Instrumentation but you need to write your own instrumentation which creates the particular byte code for your constructor. This

Unable to instrument apache httpclient using javaagent for spring boot uber jar application

旧城冷巷雨未停 提交于 2019-12-04 17:54:08
I'm trying to write a javaagent with Bytebuddy to intercept apache httpclient requests and I want to use this agent for spring boot application. The agent works fine when I start my test spring boot application from Idea (run the main method directly). However, when I package the application into a spring boot uber jar and run it using java -javaagent:myagent.jar -jar myapplication.jar , it throws the following exception. onError:org.apache.http.impl.client.AbstractHttpClient java.lang.NoClassDefFoundError: org/apache/http/HttpHost at java.lang.Class.getDeclaredMethods0(Native Method) at java

byte-buddy throws java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet

断了今生、忘了曾经 提交于 2019-12-04 16:07:34
I'm trying to instrument calls to the service() method of javax.servlet.http.HttpServlet using a Java agent based on Byte Buddy. The premain function in my code is called properly but instrumentation fails with the stack trace: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.instrument.InstrumentationImpl

Intercepting calls to Java 8 lambda-expressions using Byte Buddy

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:31:15
问题 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() .type(ElementMatchers.nameContainsIgnoreCase("foo")) .transform((builder, typeDescription) -> builder.method(ElementMatchers.any()) .intercept(MethodDelegation.to(LogInterceptor.class))) .installOn(inst); } public static class LogInterceptor { @RuntimeType public static Object log(

Intercepting calls to Java 8 lambda-expressions using Byte Buddy

隐身守侯 提交于 2019-12-03 08:18:54
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() .type(ElementMatchers.nameContainsIgnoreCase("foo")) .transform((builder, typeDescription) -> builder.method(ElementMatchers.any()) .intercept(MethodDelegation.to(LogInterceptor.class))) .installOn(inst); } public static class LogInterceptor { @RuntimeType public static Object log(@SuperCall Callable<?> superCall) throws Exception { System.out.println("yeah..."); return superCall.call();

Change behaviour of static method in Java - byte code manipulation

点点圈 提交于 2019-12-02 02:01:24
I am trying to manipulate a static method. For this, Byte Buddy or any other framework can be used. There is one library that is called Pi4J that is used for controlling GPIO of Raspberry Pi. This library has a method called: GpioController gpio = GpioFactory.getInstance(); And this call is called in several places of a program that I might not have control such that I need to modify the invocation. What I would like to do is that when GpioFactory.getInstance is executed in some way detect and modify the methods of GpioController so they log that they have been called. Maybe the only solution

Byte-buddy: generate classes with cyclic types

倖福魔咒の 提交于 2019-12-01 15:54:10
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 depends on class B, and vice-versa final class A { B theB; } final class B { A theA; } The accepted answer in the link above did not provide enough information for me to get this to work. This is what I tried: import net.bytebuddy.ByteBuddy; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.DynamicType; import net

Byte-buddy: generate classes with cyclic types

旧巷老猫 提交于 2019-12-01 14:54:00
问题 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 depends on class B, and vice-versa final class A { B theB; } final class B { A theA; } The accepted answer in the link above did not provide enough information for me to get this to work. This is what I tried: import net.bytebuddy.ByteBuddy;

How to implement a wrapper decorator in Java?

一世执手 提交于 2019-11-30 19:03:31
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 object implement all methods that are defined by another interface The interface to add to existing objects is: public interface EnhancedNode { Node getNode(); void setNode(Node node); Set getRules(); void setRules(Set rules); Map getGroups(); void setGroups(Map groups); } With Byte Buddy I managed to subclass and to implement my interface. The problem is the