【问题标题】:Java Vaadin combobox warning Intellij : unclear if a varargs or non-varargs call is desiredJava Vaadin 组合框警告 Intellij:不清楚是否需要可变参数或非可变参数调用
【发布时间】:2020-06-10 14:19:23
【问题描述】:

我的代码正在运行,但我收到了来自 Intelij 的警告(代码被突出显示):unclear if a varargs or non-varargs call is desired。 但是代码确实完善了我想要的或我期望的,用值填充组合。 当我单击组合中的项目时,它会返回给我正确的枚举 所以任何人都可以帮我解决这个警告。

  ComboBox comboStatus = new ComboBox();
  comboStatus.addItems(BatchStatusCode.values());

警告在第二行 其中 BatchStatusCode 是一个相对简单的枚举

public enum BatchStatusCode {

    RUNNING("R","RUNNING"),
    FINISHED("F","FINISHED"),
    CANCELED("C","CANCELED");
    .... some code

    BatchStatusCode(final String code,final String fullName) {
        this.code = code;this.fullName = fullName;
    }

    public String getCode() {
        return code;
    }
    public String getFullName() { return fullName;}
    .... some code

【问题讨论】:

  • 感谢您指出,它在我的复制粘贴中被复制了。我编辑了我的帖子。警告仍然存在。
  • 没问题。我会检查框架中的this 错误。如果您使用的是受影响的 vaadin 版本(注意:非常旧),则有两种方法,一种采用 Object,另一种采用可变参数。我相信警告是在说明您在这种情况下调用的是哪一个尚不清楚。如果您在通话中明确转换为 Object[],警告会消失吗?
  • 我们在谈论什么 Vaadin 版本?
  • 您好,我是 Vaadin 7,感谢您的提问。
  • 当我将它转换为 Object 时,我只得到了 ComboBox 中枚举类的名称

标签: java intellij-idea combobox vaadin


【解决方案1】:

免责声明:Vaadin 7 似乎是唯一具有此方法的版本,所以我的假设是您正在使用它。如果我错了,请纠正我

错误表明编译器不确定您究竟想使用什么方法。是采用可变数量的参数还是只采用一个。

addItemsAbstractSelect 类中有两个重载方法:

  1. public void addItems(Collection<?> itemIds) throws UnsupportedOperationException
  2. public void addItems(Object... itemId) throws UnsupportedOperationException

因此,解决方案可以简单地忽略警告或向编译器明确说明您想使用什么方法。例如,像这样: comboBox.addItems(Arrays.asList(BatchStatusCode.values()));


编辑:实际上,这不是原因。但我会把它留在这里

Enumvalues()问题是它是由编译器when it creates an enum 生成的。

The compiler automatically adds some special methods when it creates an enum.
For example, they have a static values method that returns an array containing all
of the values of the enum in the order they are declared. 

【讨论】:

  • 谢谢,Arrays.asList 的解决方案有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2015-05-08
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多