super.super.method() を呼び出したかったのですが、Java ではできません。いくつかの質問と
このような多くの回答があります
が、この場合はどれもうまくいきません.
「悪い設計」やカプセル化の破壊の問題ではありません。バグがあるためにサードパーティのクラスをオーバーライドする必要がある実際のユースケースがあり、良い解決策を探しています。
したがって、私が解決策を探している状況は次のとおりです。
クラス ContextGetter と SpecialContextGetter は、第 3 部のクラス ライブラリにあります。SpecialContextGetter のメソッド getContext にはバグがあります (i が 7 ではなく 8 に設定されます)。
私はそれを修正したい。そこで、SpecialContextGetter を SpecialContextGetterCorrected で拡張し、getContext を再実装して (SpecialContextGetter からコピーして変更を加えました)、SpecialContextGetter の代わりに私のクラスである SpecialContextGetterCorrected を使用するようにフレームワークに指示しました。
問題は、私の新しいメソッドがまだ ContextGetter.getContext を呼び出す必要があり、Java にそれを行うように指示できないことです。(super.super.getContext を呼び出したい)
クラスパスの前に自分の com. thirdparty. SpecialContextGetter を配置する以外に、どうすればそれを達成できますか?
package com.thirdparty;
class ContextGetter {
//has several state variables
public Context getContext(Set x) throws Exception {
/* uses the state vars and its virtual methods */
/* may return a Context or throw an Exception */
}
/* other methods, some overridden in SpecialContextGetter */
}
class SpecialContextGetter {
//has several state variables
public Context getContext(Set x) throws Exception {
/* uses the state vars and its virtual methods */
/* somewhere in it it contains this: */
if (isSomeCondition()) {
try {
// *** in my copied code i want to call super.super.getContext(x) ***
Context ctxt=super.getContext(x);
/* return a modified ctxt or perhaps even a subclass of Context */
} catch( Exception e) {
/* throws new exceptions as well as rethrows some exceptions
depending on e and other state variables */
}
else {
/* some code */
int i=8; // *** this is the bug. it should set i to 7 ***
/* may return a Context or throw an Exception */
}
}
/* other methods, some overridden in SpecialContextGetter */
}