Skip to content

Instantly share code, notes, and snippets.

@heipacker
Created April 9, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heipacker/1cf23e119f625924675f08a61c2d8ff3 to your computer and use it in GitHub Desktop.
Save heipacker/1cf23e119f625924675f08a61c2d8ff3 to your computer and use it in GitHub Desktop.
byteBuddyClassFileTransformer
package com.dlmu.agent.server;
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.SuperMethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
/**
* Created by fupan on 16-4-9.
*/
public class ClassTransformerSupport {
public static void addCode(Instrumentation instrumentation) {
Class[] allLoadedClasses = instrumentation.getAllLoadedClasses();
System.out.println("all load classes length:" + allLoadedClasses.length);
System.out.println("object size:" + instrumentation.getObjectSize(new Object()));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default().type(ElementMatchers.any()).transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader) {
return builder
.method(ElementMatchers.any()).intercept(MethodDelegation.to(DelagateLogging.class)
.andThen(SuperMethodCall.INSTANCE));
}
}).installOn(instrumentation);
new HelloWorld().sayHello();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment