Tag: byte buddy

是否有运行时代理创建库,它支持保留代理类的注释?

使用例如cglib或javassist代理创建代理时 ,通过创建代理目标的子类来实现此代理。 但是,这意味着此代理上的注释将丢失。 当一个类由两个库处理时,这是有问题的,其中: 第一个库需要创建给定类的代理才能运行。 第二个库通过从中读取注释来处理对象。 对于第二个库,当同时使用第一个库时,注释已消失。 问题是:是否存在具有高级API的运行时代码生成库,可以轻松保留代理类的注释?

尝试重新定义sun.reflect.GeneratedMethodAccessor1时,ByteBuddy失败

在好奇心的驱使下,我试图导出GeneratedMethodAccessor1的字节码(使用reflection时由JVM生成)。 我尝试通过以下方式获取类的字节码: 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.setAccessible(true); Object methodAccessor = field.get(exampleMethod); Field delegate […]

Java中如何使用Byte Buddy分配字段?

我很难理解Byte Buddy的文档。 为了帮助我学习API,我想生成与此Java相当的字节代码: public final class GeneratedByByteBuddy { private final int a; public GeneratedByByteBuddy(final int a) { this.a = a; } } 我很难找到使用Instrumentation创建字段分配的正确方法。

我可以使用Byte Buddy重新定义私有方法吗?

是否可以使用Byte Buddy来重新定义类的私有方法? 似乎使用Byte Buddy的入口点总是对现有类进行子类化。 这样做时,显然不可能重新定义父类的私有方法(至少不能以在父类中使用重新定义的方法的方式)。 请考虑以下示例: 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(FixedValue.value(“Byte Buddy!”)) .make() .load(Main.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded() .newInstance(); foo.sayHello(); 输出将是“Hello World!”。 有没有机会得到“Byte Buddy!” 作为输出?

无法使用javaagent为apach httpclient设置spring boot uber jar应用程序

我正在尝试用Bytebuddy编写一个javaagent来拦截apache httpclient请求,我想将这个代理用于spring boot应用程序。 当我从Idea(直接运行main方法)启动测试Spring启动应用程序时,代理工作正常。 但是,当我将应用程序打包到spring boot uber jar并使用java -javaagent:myagent.jar -jar myapplication.jar运行它时,它会抛出以下exception。 onError:org.apache.http.impl.client.AbstractHttpClient java.lang.NoClassDefFoundError: org/apache/http/HttpHost at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.getDeclaredMethods(Class.java:1975) at net.bytebuddy.description.method.MethodList$ForLoadedType.(MethodList.java:106) at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:985) at net.bytebuddy.implementation.MethodDelegation$MethodContainer$ForExplicitMethods.ofStatic(MethodDelegation.java:1037) at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:247) at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:226) at com.yiji.dtrace.agent.httpclient4.interceptor.HttpClient4Interceptors$1.transform(HttpClient4Interceptors.java:48) at net.bytebuddy.agent.builder.AgentBuilder$Transformer$Compound.transform(AgentBuilder.java:457) at net.bytebuddy.agent.builder.AgentBuilder$Default$Transformation$Simple$Resolution.apply(AgentBuilder.java:2791) at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:3081) at sun.instrument.TransformerManager.transform(TransformerManager.java:188) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at […]

如何使用Byte Buddy创建默认构造函数

我想拦截我的一个类上的一些方法调用,但这些类没有默认的构造函数。 鉴于以下类,我如何设置Byte Buddy也创建一个公共无参数构造函数来创建生成的类? public class GetLoggedInUsersSaga extends AbstractSpaceSingleEventSaga { private final UserSessionRepository userSessionRepository; @Inject public GetLoggedInUsersSaga(final UserSessionRepository userSessionRepository) { this.userSessionRepository = userSessionRepository; } @StartsSaga public void handle(final GetLoggedInUsersRequest request) { // this is the method in want to intercept } } 编辑:具体的用例是简化unit testing设置。 目前我们总是要写这样的东西: @Test public void someTest() { // Given // When GetLoggedInUsersRequest request […]