Created
February 20, 2015 18:35
-
-
Save ifesdjeen/e2d706bb63a50d6283c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package introspect; | |
import clojure.lang.AFn; | |
import clojure.lang.IFn; | |
import net.bytebuddy.ByteBuddy; | |
import net.bytebuddy.agent.ByteBuddyAgent; | |
import net.bytebuddy.agent.builder.AgentBuilder; | |
import net.bytebuddy.dynamic.ClassLoadingStrategy; | |
import net.bytebuddy.dynamic.DynamicType; | |
import net.bytebuddy.instrumentation.FieldAccessor; | |
import net.bytebuddy.instrumentation.MethodDelegation; | |
import net.bytebuddy.instrumentation.SuperMethodCall; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.AllArguments; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.Origin; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.RuntimeType; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.This; | |
import net.bytebuddy.matcher.ElementMatchers; | |
import net.bytebuddy.modifier.Visibility; | |
import sun.reflect.ReflectionFactory; | |
import java.lang.reflect.Method; | |
public class MainTest { | |
public static void initialize(String name) { | |
new AgentBuilder.Default() | |
.rebase(ElementMatchers.nameContains(name) | |
.and(ElementMatchers.not(ElementMatchers.nameContains("load"))) | |
.and(ElementMatchers.not(ElementMatchers.nameContains("init"))) | |
) | |
.transform(new AgentBuilder.Transformer() { | |
@Override | |
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder) { | |
System.out.println(builder); | |
return builder.method(ElementMatchers.any()) | |
.intercept(MethodDelegation.to(LogInterceptor.class) | |
.andThen(SuperMethodCall.INSTANCE)); | |
} | |
}).allowRetransformation() | |
.installOn(ByteBuddyAgent.installOnOpenJDK()); | |
} | |
public static void main(String[] name) { | |
initialize("FnImpl"); | |
System.out.println(new FnImpl().invoke(1, 2)); | |
} | |
} |
This file contains hidden or 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
package introspect; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.AllArguments; | |
import net.bytebuddy.instrumentation.method.bytecode.bind.annotation.Origin; | |
import java.lang.reflect.Method; | |
public class LogInterceptor { | |
public static void log(@AllArguments Object[] allArguments, | |
@Origin Method method) { | |
for (int i = 0; i < allArguments.length; i++) { | |
System.out.println(allArguments[i]); | |
} | |
System.out.println("Called on:" + method.getDeclaringClass().toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment