Skip to content

Instantly share code, notes, and snippets.

@jakubhalun
Created October 9, 2017 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubhalun/50bab833ba085dc02247912feb6848d4 to your computer and use it in GitHub Desktop.
Save jakubhalun/50bab833ba085dc02247912feb6848d4 to your computer and use it in GitHub Desktop.
Byte Buddy Java Agent with method delegation to interceptor
private static AgentBuilder createAgent(Class<? extends Annotation> annotationType,
String methodName) {
return new AgentBuilder.Default().type(
ElementMatchers.isAnnotatedWith(annotationType)).transform(
new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader) {
return builder
.method(ElementMatchers.named(methodName))
.intercept(MethodDelegation
.to(LoggingInterceptor.class)
.andThen(SuperMethodCall.INSTANCE));
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment