byte-buddy

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

a 夏天 提交于 2019-12-09 23:06:03
问题 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

ByteBuddy fails when trying to redefine sun.reflect.GeneratedMethodAccessor1

点点圈 提交于 2019-12-09 04:27:27
Driven by curiosity, I tried to export the bytecode of GeneratedMethodAccessor1 (generated by the JVM when using reflection). I try to get the bytecode of the class the following way: public class MethodExtractor { public static void main(String[] args) throws Exception { ExampleClass example = new ExampleClass(); Method exampleMethod = ExampleClass.class .getDeclaredMethod("exampleMethod"); exampleMethod.setAccessible(true); int rndSum = 0; for (int i = 0; i < 20; i++) { rndSum += (Integer) exampleMethod.invoke(example); } Field field = Method.class.getDeclaredField("methodAccessor"); field

Is there a runtime proxy creation library that supports to retain annotations of the proxied class? [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-08 13:50:13
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . When creating a proxy with for example cglib or javassist proxies, this proxy is implemented by creating a subclass of the proxy target. However, this means that the annotations on this proxy are lost. This is problematic when a class is processed by two libraries where: The first libraries requires the creation

ByteBuddy @Entity annotation not visible to Spring Boot ClassPathBeanDefinitionScanner basePackages scan

萝らか妹 提交于 2019-12-08 07:43:34
I am trying to transform classes such that Spring can see the transformed annotations. This would allow me to dynamically inject @Entity annotations such that Spring Boot will register it as a managed type for data use. Annotation transformation works but Spring Boot appears to be performing package scanning at the file-jar level missing the transformed versions. This means that Spring is not seeing the annotations because it is analyzing the input stream of the class file within the JAR itself. The initial spring candidate component scanning is as follows: public Set<BeanDefinition>

ByteBuddy @Entity annotation not visible to Spring Boot ClassPathBeanDefinitionScanner basePackages scan

浪子不回头ぞ 提交于 2019-12-08 06:17:10
问题 I am trying to transform classes such that Spring can see the transformed annotations. This would allow me to dynamically inject @Entity annotations such that Spring Boot will register it as a managed type for data use. Annotation transformation works but Spring Boot appears to be performing package scanning at the file-jar level missing the transformed versions. This means that Spring is not seeing the annotations because it is analyzing the input stream of the class file within the JAR

Can I redefine private methods using Byte Buddy?

北慕城南 提交于 2019-12-07 15:33:13
问题 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

Can Byte Buddy create fields and method annotations at runtime?

久未见 提交于 2019-12-06 15:06:51
I would like to implement this 3rd-party annotation to map my class's fields/properties to my database table columns. I can easily implement the annotations at compile time (as shown in the example code below) but I can't find a way to do this at runtime. (I am loading the library at runtime using reflection.) My question is how can I implement the same mapping annotation when loading a library at run time? Can Byte Buddy handle this for Android? //3rd party annotation code package weborb.service; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang

Redefine java.lang classes with ByteBuddy

旧街凉风 提交于 2019-12-06 09:34:40
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 premain(String agentArgs, Instrumentation inst) { new AgentBuilder.Default() .type(named("java.lang.String")) .transform((builder, typeDescription, classLoader) -> builder.method(named("toString")) .intercept(FixedValue.value("toString() got hacked!"))) .with(AgentBuilder.Listener.StreamWriting.toSystemOut()) .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)

Caching generated classes in Byte Buddy?

随声附和 提交于 2019-12-06 09:01:21
问题 I have been using CGLIB's Enhancer for a while, but considering to switch over to Byte Buddy. It's pretty basic stuff, proxy for up to a few hundred data access interfaces, created on demand. Enhancer enhancer = new Enhancer(); enhancer.setClassLoader(...); enhancer.setSuperclass(...); enhancer.setInterfaces(...); enhancer.setCallback(...); enhancer.create(); CGLIB is caching the generated type to improve performance. What is the recommended approach with Byte Buddy? I want to avoid any

JUnit Test framework for Javaagent Instrumentation Framework

痞子三分冷 提交于 2019-12-06 03:34:25
What are the standard ways for creating unit tests for code of a Java agent and instrumentation libraries. I have created a Java agent using the Byte Buddy framework for developing a profiler on top of a web applictaion and now i wanted to write JUnit test cases for this agent. You can take inspiration from Byte Buddy's own unit tests for creating a Java agent. For this, declare a test dependency on the byte-buddy-agent module. That module includes a class that is capable of attaching a Java agent at runtime using ByteBuddyAgent.install() which returns an Instrumentation instance. Make sure