mardi 11 octobre 2016

Delegate private method in bytebuddy with @Super - possible?

I'm trying to delegate a private method in bytebuddy - but how do I call the 'overriden' version? If I have

    TypePool typepool = TypePool.Default.ofClassPath();
    new ByteBuddy()
        .rebase(typepool.describe("Foo").resolve(), ClassFileLocator.ForClassLoader.ofClassPath())
        .method(named("getNum"))
        .intercept(MethodDelegation.to(typepool.describe("FooInterceptor").resolve()))
        .make()
        .load(typepool.describe("Foo").resolve().getClass().getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());

    Foo foo1 = new Foo();
    System.out.println("Foo says " + foo1.getMessage());

and

public class Foo
{
    private int num = 0;

    public String getMessage()
    {
        return "Message is Foo " + getNum();
    }
    private int getNum()
    {
        return num++;
    }
}

and

import net.bytebuddy.implementation.bind.annotation.Super;

public class FooInterceptor
{
    public static int getNum(@Super Foo foo)
    {
        // This won't work!
        return foo.getNum() + 100;
    }
}

As far as the compiler is concerned, even if @Super Foo foo is going to become something else at runtime, I'm not allowed to call a private method on Foo. I don't seem to be able to reflect/invoke getNum() either - whatever @Super Foo becomes, it doesn't seem to have a getNum() method.

Can someone perhaps point me in the right direction here?





Aucun commentaire:

Enregistrer un commentaire