我們有一個模糊的類,我們需要使用bytebuddy進行增強。我們基本上需要重新定義一種方法。子類似乎沒有奏效(代碼沒有執行)。重新啓動工作,但在我們攔截的方法中,我們需要調用超類(現在像在「繼承」中講超類)方法。調用攔截器的超類方法bytebuddy
class Parent {
public void connect(){
...
};
}
class WeNeedToHackThis extends Parent {
public void connect(InetAddress addr){
//... this is what we want to hack
}
public void connect(){
this.connect(null);
// this is getting called from interceptor :(which delegates to our hacked method
// we need to call "real" superclass's (Parent) method instead
}
}
...
Class<?> dynamic = new ByteBuddy()
.with(TypeValidation.DISABLED)
.rebase(commandBase, locator)
.method(named("connect").and(takesArgument(0, InetAddress.class)))
.intercept(MethodDelegation.to(Session3270ConnectMethod.class))
.make()
.load(Thread.currentThread().getContextClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
//In our interceptor:
public static void connect(InetAddress paramInetAddress,
@Origin Method origin,
@This Object self) throws SessionException {
try {
System.out.println("hi from hijacked");
c.call();
//HOW DO WE CALL SOMETHING LIKE super.connect()
// we need to call Parent.connect();
// but I am stuck at how to access superclass code (new Parent().connect().
// I cant access the Parent class method calls on this object
// if I use @SuperCall or @This then I am getting already overriden version, I need to call the super (Parent's.class) version :(
} catch (Exception e) {
throw new RuntimeException(e);
}
}
好吧,我看到它沒有說清楚:) - 類Parent.class(這是父類,類WeNeedToHackThis從繼承的>「如何稱之爲‘連接()’方法 - 基本上,我們需要執行:'super.connect()'內部攔截器) – kosta5