构造函数未在编译时发现错误

时间:2013-07-26 08:30:37

标签: java constructor compiler-errors gwt-compiler

我有一个奇怪的问题,我无法深究。我有一个课程如下:

public class I18nTextBox extends I18nAbstractTextBox<String> {
    public I18nTextBox() {
        this("", Consts.MAXL_BASIC);
    }
    public I18nTextBox(String keyTitle) {
        this(keyTitle, Consts.MAXL_BASIC);
    }

    public I18nTextBox(String keyTitle, int maxLength, String ... substitutions) {
        super(keyTitle, maxLength, substitutions);
    }
    // ...
}

其超类定义如下:

public abstract class I18nAbstractTextBox<T> extends TextBox implements IRefreshableDisplay, IModelDataField<T> {
    // some properties ...

    public I18nAbstractTextBox() {
        this("", Consts.MAXL_BASIC);
    }

    public I18nAbstractTextBox(String keyTitle) {
        this(keyTitle, Consts.MAXL_BASIC);
    }

    public I18nAbstractTextBox(String keyTitle, int maxLength, String ... substitutions) {
        // do some logic
    }
    //...
}

Eclipse没有显示任何编译器错误,但是当我运行GWT的调试模式时,一旦它尝试加载应用程序,我就会得到一大堆构造函数I18nTextBox()未定义错误,每个时间我实例化第一个类(I18nTextBox)。

以前只有一个类I18nTextBox,但是我们接受了它,使它成为抽象的并创建I18nTextBox来扩展它,因为我们需要一种特殊的类型,所以我知道这个课程本身就是有效的。

总结:

  • 构造函数在子类和超类中定义。
  • Eclipse没有看错。
  • 当项目由GWT编译器编译时,我会收到“未找到构造函数”错误。

在工作中没有人似乎能够看到问题,是否有人知道可能会发生什么?

更新

所以有人指出缺少2参数构造函数,但有两点要说:

  • 参数String ... substitutions是一个可选参数,因此如果未指定,则默认为null。
  • 之前只有I18nTextBox且没有抽象超类(我们如上所述抽象化了)。

如果我继续并指定另一个构造函数,如下所示:

public I18nTextBox(String keyTitle, int maxLength) {
    this(keyTitle, maxLength, "");
}

然后我修复了I18nTextBox中的错误,但为I18nIntegerTextBox获得的错误完全相同:

public class I18nIntegerTextBox extends I18nAbstractTextBox<Integer> {
    public I18nIntegerTextBox() {
        this("", Consts.MAXL_BASIC);
    }
    public I18nIntegerTextBox(String keyTitle) {
        this(keyTitle, Consts.MAXL_BASIC);
    }
    public I18nIntegerTextBox(String keyTitle, int maxLength) {
        this(keyTitle, maxLength, "");
    }
    public I18nIntegerTextBox(String keyTitle, int maxLength, String ... substitutions) {
        super(keyTitle, maxLength, substitutions);
    }
    // ...
}

所以我只是不明白出了什么问题!

1 个答案:

答案 0 :(得分:0)

好的,我们发现了这个问题。基本上this()调用使GWT编译器混乱。这不是一个javac错误,但我有一种感觉,当GWT将Java翻译成Javascript时,我们对于我们所指的this()(超类或子类)感到困惑。错误有点神秘,所以我不完全确定,但我将构造函数更改为所有调用super()(但是有许多参数是合适的)。代码现在看起来像这样:

public class I18nTextBox extends I18nAbstractTextBox<String> {
    public I18nTextBox() {
        super();
    }
    public I18nTextBox(String keyTitle) {
        super(keyTitle);
    }
    public I18nTextBox(String keyTitle, int maxLength, String ... substitutions) {
        super(keyTitle, maxLength, substitutions);
    }
    //...
}

这解决了这个问题,奇怪。