Skip to content

Instantly share code, notes, and snippets.

@tnishada
Last active April 24, 2020 11:23
Show Gist options
  • Save tnishada/f195d3daf2a4f86d7dfb8b33e5e5a260 to your computer and use it in GitHub Desktop.
Save tnishada/f195d3daf2a4f86d7dfb8b33e5e5a260 to your computer and use it in GitHub Desktop.
public class ByteBuddyExampleMain {
public static void main(String[] args) throws Exception {
Class<?> type = new ByteBuddy()
.redefine(Robot.class)
.visit(Advice.to(MyAdvices.class).on(ElementMatchers.isMethod()))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
// use java reflection to invoke the method
String returnVal = (String)type.getDeclaredMethod("greetUser", String.class).invoke(type.getDeclaredConstructor().newInstance(), "John");
System.out.println("return value: " + returnVal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment