Last active
April 24, 2020 11:23
-
-
Save tnishada/f195d3daf2a4f86d7dfb8b33e5e5a260 to your computer and use it in GitHub Desktop.
This file contains 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
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