import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
public class PrivateMethodDelegateExample {
private void privateMethod() {
System.out.println("This is a private method.");
}
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
Class> proxyClass = new ByteBuddy()
.subclass(Object.class)
.method(ElementMatchers.isDeclaredBy(PrivateMethodDelegateExample.class)
.and(ElementMatchers.named("privateMethod")))
.intercept(MethodDelegation.to(PrivateMethodDelegateExample.class))
.make()
.load(PrivateMethodDelegateExample.class.getClassLoader())
.getLoaded();
PrivateMethodDelegateExample delegateExample = (PrivateMethodDelegateExample) proxyClass.newInstance();
delegateExample.privateMethod();
}
}