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
@RestController | |
public class HelloController { | |
private static final String template = "Hello, %s!"; | |
@RequestMapping("/hello") | |
public String greeting( | |
@RequestParam(value = "name", defaultValue = "World") String name) { | |
return String.format(template, name); | |
} |
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
public static void intercept(@AllArguments Object[] allArguments, | |
@Origin Method method) { | |
Logger logger = LoggerFactory.getLogger(method.getDeclaringClass()); | |
logger.info("Method {} of class {} called", method.getName(), method | |
.getDeclaringClass().getSimpleName()); | |
for (Object argument : allArguments) { | |
logger.info("Method {}, parameter type {}, value={}", | |
method.getName(), argument.getClass().getSimpleName(), | |
argument.toString()); |
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
type(ElementMatchers.isAnnotatedWith(RestController.class)) |
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
type(ElementMatchers.named( | |
"pl.halun.demo.bytebuddy.instrumented.app.HelloController")) |
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
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.1.7</version> | |
<scope>provided</scope> | |
</dependency> |
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
return new AgentBuilder.Default().disableClassFormatChanges() | |
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) | |
.type(named(className)) | |
.transform(new AgentBuilder.Transformer() { | |
@Override | |
public DynamicType.Builder<?> transform( | |
DynamicType.Builder<?> builder, TypeDescription typeDescription, | |
ClassLoader classLoader) { | |
return builder.visit(Advice.to(LoggingAdvice.class).on( |
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
public class LoggingAdvice { | |
@Advice.OnMethodEnter | |
public static void intercept(@Advice.BoxedArguments Object[] allArguments, | |
@Advice.Origin Method method) { | |
Logger logger = LoggerFactory.getLogger(method.getDeclaringClass()); | |
logger.info("Method {} of class {} called", method.getName(), method | |
.getDeclaringClass().getSimpleName()); | |
for (Object argument : allArguments) { | |
logger.info("Method {}, parameter type {}, value={}", |
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
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) { |