Using ByteBuddy, I'd like to create a proxy for a class which has a private constructor. That's the class:
public class Foo {
private Foo() {
}
}
I tried write some code like this but not work?
public class CreateAndExecuteProxy {
public static void main(String[] args) throws Exception {
Constructor<?> superConstructor = Foo.class.getDeclaredConstructor();
Class<? extends Foo> proxyType = new ByteBuddy()
.subclass( Foo.class, ConstructorStrategy.Default.NO_CONSTRUCTORS )
.defineConstructor( Visibility.PUBLIC )
.intercept( MethodCall.invoke( superConstructor ).onSuper() )
.make()
.load( CreateAndExecuteProxy.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
Foo foo = proxyType.newInstance();
}
}