我想出了一个可以在您的场景中工作的解决方案,但它的工作方式与您最初想要解决的问题(super 调用的解决)略有不同:
public class OpenInitialHandler extends InitialServerLegacyDelegate {
public OpenInitialHandler(BungeeCord bungee, ListenerInfo listener) {
super(bungee, listener);
}
public OpenInitialHandler(ProxyServer proxyServer, ListenerInfo listener) {
super(proxyServer, listener);
}
}
public class InitialServerLegacyDelegate /* implements and extends whatever you need */ {
private static final Constructor<InitialDelegate> targetConstructor = InitialServer.getConstructors()[0];
private final InitialServer delegate;
protected InitialServerLegacyDelegate(BungeeCord bungee, ListenerInfo listener) {
this(bungee, listener);
}
protected InitialServerLegacyDelegate(ProxyServer proxyServer, ListenerInfo listener) {
try {
// This is the critical part.
// Instead of binding/checking the constructor parameter types
// at compile-time, this will be resolved at runtime.
delegate = targetConstructor.newInstance(proxyServer, listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// implement all neccessary interface methods here
// and simply make them delegate methods
}
基本上,InitialServerLegacyDelegate 处理这种遗留行为。它看起来像一个有效的超类(因为它实现了与InitialServer 相同的接口,但实际上它只是将所有调用委托给它在运行时解析的InitialServer 实例。
您可能面临的一个问题是:如果您的班级在OpenInitialHandler(ProxyServer proxyServer, ListenerInfo listener) 获得输入,其中ProxyServer 不是BungeeCord 类型。在这种情况下,如果存在较新的依赖项(使用BungeeCord 构造函数)并且它获得非BungeeCord-input,则实现将失败并返回ClassCastException。
Eclipse 可以很容易地生成委托方法。详情请见this question on how to generate delegate methods in Eclipse。