Skip to content

Instantly share code, notes, and snippets.

@raphw
Created October 11, 2015 10:12
Show Gist options
  • Save raphw/7458fb0b8c389b868f2e to your computer and use it in GitHub Desktop.
Save raphw/7458fb0b8c389b868f2e to your computer and use it in GitHub Desktop.
Example for bug report
package net.bytebuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.reflect.Method;
public class Interceptor {
public static void main(String[] args) throws ClassNotFoundException {
new AgentBuilder.Default()
.rebase(ElementMatchers.nameEndsWith("BasicJDBCDemo"))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription) {
return builder.method(ElementMatchers.isDeclaredBy(typeDescription)).intercept(MethodDelegation.to(Delegator.class));
}
}).installOn(ByteBuddyAgent.install());
Class.forName("net.bytebuddy.BasicJDBCDemo");
}
public static class Delegator {
@RuntimeType
public Object foo(@Origin Method m) {
System.out.println("Intercepted: " + m);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment