Skip to content

Instantly share code, notes, and snippets.

@kriegaex
kriegaex / BootstrapAgent.java
Last active January 19, 2024 03:52 — forked from joshgord/BootstrapAgent.java
An example agent that intercepts a method of the bootstrap class loader. Tested with Byte Buddy 1.14.11 on JDKs 8 to 21.
package net.bytebuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.matcher.ElementMatchers;
@kriegaex
kriegaex / BootstrapAdviceAgent.java
Last active January 19, 2024 06:41
An example agent that intercepts bootstrap class method String::concat. Tested with Byte Buddy 1.14.11 on JDKs 8 to 21. Based on a question about https://gist.github.com/kriegaex/0f4dd4c9d05f3a68e3a8e1ed75359c3b.
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.agent.builder.ResettableClassFileTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.*;
import net.bytebuddy.dynamic.loading.ClassInjector;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;