2

抱歉标题含糊;我不完全确定问题是什么。

背景

简而言之:某个基类的子类必须定义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 类本身中这样做可能存在问题。

4

2 回答 2

1

我怀疑问题是由于类的静态最终字段的初始化顺序造成的。通过在类初始化期间使用反射,您可以在类完全初始化之前访问它。如果在派生类上,您有以下内容:

public static final String NAME;
static {
    NAME = "some name";
}

public static final AlgImplementation derivedAlg =
    new AlgImplementation("xyz", Derived.class, "Our XYZ derived class", "");

然后 AlgImplementation 将在初始化后检查 NAME 常量,并读取“some name”字符串。如果您颠倒顺序:

public static final AlgImplementation derivedAlg =
    new AlgImplementation("xyz", Derived.class, "Our XYZ derived class", "");

public static final String NAME;
static {
    NAME = "some name";
}

然后 AlgImplementation 将在分配之前读取常量,并改为读取null

我不确定如果 NAME 是由这样的编译时常量直接分配的,是否有可能发生这种情况:

public static final String NAME = "some name";

我猜想这会阻止这个问题,但也许不会。您关于“改组方法的顺序将其修复很长时间”的说法支持了问题是由于初始化顺序引起的想法。我建议derivedAlg在所有其他常量之后移动该字段以鼓励它最后初始化。

于 2013-10-31T15:57:27.110 回答
0

静态方法不参与类层次结构。您应该始终使用staticMethod()Class.staticMethod()代替。

于 2013-10-31T15:19:12.323 回答