Skip to content

Instantly share code, notes, and snippets.

Jakub Halun jakubhalun

Block or report user

Report or block jakubhalun

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jakubhalun
jakubhalun / MethodDelegation.java
Created Oct 9, 2017
Byte Buddy Java Agent with method delegation to interceptor
View MethodDelegation.java
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) {
@jakubhalun
jakubhalun / HelloController.java
Created Oct 9, 2017
RestController for Byte Buddy instrumentation demo
View HelloController.java
@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);
}
@jakubhalun
jakubhalun / LoggingInterceptor.java
Created Oct 9, 2017
Interceptor logs method execution and its parameters
View LoggingInterceptor.java
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());
@jakubhalun
jakubhalun / MatchingToAnnotation.java
Created Oct 9, 2017
Byte Buddy: matching the class by its annotation
View MatchingToAnnotation.java
type(ElementMatchers.isAnnotatedWith(RestController.class))
@jakubhalun
jakubhalun / MatchingToClassName.java
Created Oct 9, 2017
Byte Buddy matching to a class name
View MatchingToClassName.java
type(ElementMatchers.named(
"pl.halun.demo.bytebuddy.instrumented.app.HelloController"))
@jakubhalun
jakubhalun / DependencyLogback.xml
Created Oct 9, 2017
Dependency to Logback Classic
View DependencyLogback.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
<scope>provided</scope>
</dependency>
@jakubhalun
jakubhalun / JavaAgentByteBuddyFinal.java
Created Oct 9, 2017
Byte Buddy Java Agent working both from command line parameter and Attach API
View JavaAgentByteBuddyFinal.java
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(
@jakubhalun
jakubhalun / LoggingAdvice.java
Created Oct 9, 2017
Logging interceptor as Advice
View LoggingAdvice.java
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={}",
You can’t perform that action at this time.