Last active
November 8, 2018 12:28
-
-
Save SergeyAlekseevN/18e63d0baebe216f6691426cbfbb1b92 to your computer and use it in GitHub Desktop.
Create instanse of class with 1+ args constructor using ByteBuddy
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 A { | |
private String message; | |
public A(String message) { | |
this.message = message; | |
} | |
public String getMessage() { | |
return message; | |
} | |
} | |
@Test | |
public void createInstanсe() throws Exception { | |
Class<? extends A> loaded = new ByteBuddy() | |
.subclass(A.class) | |
.defineConstructor(Visibility.PUBLIC) | |
.intercept(MethodCall.invoke(A.class.getDeclaredConstructor(String.class)).with("XXX")) | |
.make() | |
.load(A.class.getClassLoader()) | |
.getLoaded(); | |
A a = loaded.newInstance(); | |
assertEquals("XXX", a.getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment