抱歉标题含糊;我不完全确定问题是什么。
背景
简而言之:某个基类的子类必须定义3个具体的静态方法,隐藏基类的静态方法。实现描述类在初始化时检查这一点。然而,在运行应用程序时似乎是随机的,但在初始化期间我得到运行时异常,说我没有正确地重新实现这些方法。但是当这种情况发生时,我在其他地方的不相关的类中工作,这种情况很少发生,只是简单地打乱方法的顺序就可以解决很长一段时间。
代码
因此,三个类:Base、Derived 和 AlgImplementation 类:
AlgImplementation 构造函数:
/* verifying that the extending class has implemented these methods */
if (this.getAlgorithmClassName() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassName() method from as specified.");
}
if (this.getAlgorithmClassDescription() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassDescription() method from as specified.");
}
if (this.getAlgorithmClassAnalyticLevel() == null) {
throw new IllegalArgumentException("The Algorithm Class of this algorithm has not re-implemented the getAlgorithmClassAnalyticLevel() method from as specified.");
}
这就是问题发生的地方,其中一项检查失败了。我从上述一项或多项中得到 IllegalArgumentException。我可以简单地在派生类中移动实现的顺序,强制重建该代码,然后它就可以正常工作了。
基类和派生类都具有相同的简单静态方法,但它们返回的静态字段定义不同:
class Derived extends Base {
public static AnalyticLevel getAlgorithmClassAnalyticLevel()
{
return ANALYTIC_LEVEL;
}
public static String getAlgorithmClassName()
{
return NAME;
}
public static String getAlgorithmClassDescription()
{
return DESCRIPTION;
}
}
以上字段都是非空的静态最终字符串。
现在,在 Derived 类中,我声明了一个静态 final AlgImplementation 字段: public static final AlgImplementation derivedAlg = new AlgImplementation("xyz", Derived.class, "Our XYZ derived class", "");
最后,我认为您需要知道的最后一件事是,此 AlgImplementation 实例为每个静态方法类执行此操作:
public String getAlgorithmClassName() {
String className = "";
try {
className = (String)algorithmClass.getDeclaredMethod("getAlgorithmClassName", new Class<?>[0]).invoke(null, new Object[0]);
} catch (Exception e) {
throw new UnsupportedOperationException("Required static method getAlgorithmClassName() not implemented on "+this.getClass().getName());
}
return className;
}
最后
所以,我的问题是:如果实际上声明了这些方法,那么对派生方法的检查怎么会失败?声明一个引用它定义的类的静态 AlgImplementation 字段是否存在问题(导致编译问题的一些奇怪顺序,或类似的东西)?
错误出现在 Derived 类的初始化期间,特别是在初始化静态 AlgImplementation 字段的行中,这就是为什么我认为在 Derived 类本身中这样做可能存在问题。