Created
October 11, 2015 10:12
-
-
Save raphw/7458fb0b8c389b868f2e to your computer and use it in GitHub Desktop.
Example for bug report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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