byte-buddy

how to debug an internal error?

自古美人都是妖i 提交于 2019-12-12 19:30:39
问题 So I have a class Foo that should eventually adjust and reload classes. It has a method for that, too: private void redefineClass(String classname, byte[] bytecode) { ClassFileLocator cfl = ClassFileLocator.Simple.of(classname,bytecode); Class clazz; try{ clazz = Class.forName(classname); }catch(ClassNotFoundException e){ throw new RuntimeException(e); } Debug._print("REDEFINING %s",clazz.getName()); new ByteBuddy() .redefine(clazz,cfl) .make() .load(clazz.getClassLoader(),

MethodHandler in Hibernate using ByteBuddy proxies gets stuck in endless loop

北慕城南 提交于 2019-12-11 17:46:53
问题 I'm currently migrating an old tool for Hibernate to automate prefetching based on entity statistics. The old tool used Hibernate 3.1, so there's some job to be done. Traditionally, Hibernate used CGlib proxies, but as I'm developing for the latest release of Hibernate I am changing the proxy generation to ByteBuddy. However, since the change to ByteBuddy I've been having some problems with getting the MethodHandler to work. The methodhandler in my tool is supposed to handle all the calls to

Slf4j loggers with Byte Buddy

谁说胖子不能爱 提交于 2019-12-11 15:49:01
问题 I try to instrument a java class called ThreadPoolExecutor and i want to get details of threads using slf4j loggers and I am getting following error Exception in thread "pool-2-thread-2" Exception in thread "pool-2-thread-1" java.lang.NoClassDefFoundError: com/github/shehanperera/threadagent/MonitorInterceptor at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java) at java.lang.Thread.run(Thread.java:748)java.lang.NoClassDefFoundError: com/github/shehanperera

Byte Buddy - Handling cyclic references in generated classes

放肆的年华 提交于 2019-12-11 05:26:49
问题 I'm trying to generate runtime wrappers around classes in some class graph, but I don't know how to handle the situation when there is a cycle in the graph. Imagine there's a class A that has a field of type B, but type B has a field of type A. I want to generate classes A' and B' so that class A' has a field of type B' and B' has a field of type A'. In Byte Buddy the method "defineField" can receive a parameter of type TypeDefinition. I think there must be a way to define TypeDefinition for

ByteBuddy: newly defined fields not visible through reflection

∥☆過路亽.° 提交于 2019-12-11 02:05:53
问题 I use ByteBuddy in an Agent to add a tracking variable to each Runnable in a test program: new AgentBuilder.Default() .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(ElementMatchers.isSubTypeOf(Runnable.class) .and(ElementMatchers.not(ElementMatchers.isInterface()))) .and(ElementMatchers.not(ElementMatchers.isAbstract())) .transform((builder, typeDescription, classLoader, module) -> builder .defineField("foo", String.class) .constructor(ElementMatchers.any()) .intercept(Advice

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

*爱你&永不变心* 提交于 2019-12-10 20:51:17
问题 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<? extends TestDataEnh> clz = buddy.subclass(TestDataEnh.class) .defineField("stringVal",String.class) .method(named("setFieldVal")).intercept( MethodDelegation.to(new SetterInterceptor()) ) .make() .load(getClass().getClassLoader(), ClassLoadingStrategy

How to efficiently wrap POJO with Bytebuddy?

白昼怎懂夜的黑 提交于 2019-12-10 18:45:09
问题 I want to wrap simple POJO class. The thing is I know nothing about that class beforehand, only that it's POJO with setters and getters. I want to substitute this class with my Proxyclass so that every time client calls getter or setter I would be able to intercept that call. So when the call is intercepted, I want to do some pre-get(or set) operation, then invoke the getter(or setter), and then to do some post-get(or set) operations. I'm creating my proxy like that private Pojo

Intercepting default constructor with Byte Buddy

会有一股神秘感。 提交于 2019-12-10 18:23:36
问题 I'm trying to intercept constructor calls with Byte Buddy, this is my sample code: package t; import static net.bytebuddy.dynamic.loading.ClassLoadingStrategy.Default.INJECTION; import static net.bytebuddy.implementation.MethodDelegation.to; import static net.bytebuddy.matcher.ElementMatchers.any; import java.util.concurrent.Callable; import net.bytebuddy.ByteBuddy; import net.bytebuddy.implementation.bind.annotation.RuntimeType; import net.bytebuddy.implementation.bind.annotation.SuperCall;

Redefine java.lang classes with ByteBuddy

萝らか妹 提交于 2019-12-10 11:26:22
问题 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

How in Java to assign to fields with Byte Buddy?

让人想犯罪 __ 提交于 2019-12-10 10:11:10
问题 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. 回答1: You are creating a class with customized byte code. For this, you cannot use a built-in Instrumentation but