1

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 */
}
4

1 に答える 1

1

いくつかのオプションがありますが、サードパーティ ソフトウェアの可視性の宣言が厳しすぎる場合、どちらも実行できない可能性があります。

  • 犯人メソッドだけを拡張SpecialContextGetterしてオーバーライドする代わりに、クラス全体をコピーして貼り付け、SpecialContextGetterそこでバグを修正します。これは醜いかもしれませんが、唯一の方法です。
  • を拡張する代わりにSpecialContextGetter、拡張し、(この新しいクラスのコンストラクターで受け取る)ContextGetterのインスタンスに委譲しSpecialContextGetterます。必要な にアクセスできるバグを修正したいメソッドを除きますsuper。運が良ければ、これを実行できるかもしれませんが、一部の可視性宣言または可変状態では実行できないと感じています。
于 2012-11-15T11:25:04.023 に答える