Skip to content

Instantly share code, notes, and snippets.

@SergeyAlekseevN
Last active November 8, 2018 12:28
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 SergeyAlekseevN/18e63d0baebe216f6691426cbfbb1b92 to your computer and use it in GitHub Desktop.
Save SergeyAlekseevN/18e63d0baebe216f6691426cbfbb1b92 to your computer and use it in GitHub Desktop.
Create instanse of class with 1+ args constructor using ByteBuddy
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