Understanding “Raw type. References to generic types should be parameterized” Error – 理解 “原始类型 对通用类型的引用应参数化” 错误

最后修改: 2024年 2月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Raw types are an advanced topic in Java. It required a good understanding of parametrized classes but might still be confusing. Luckily, IDEs can help us when we get things wrong. In particular, the Eclipse IDE produces a warning to notify us about this.

原始类型是 Java 中的高级主题。它需要对 参数化类有很好的理解,但可能仍然会让人感到困惑。幸运的是,集成开发环境可以在我们出错时帮助我们。特别是,Eclipse IDE 会生成一个警告来通知我们。

In this tutorial, we’ll check the warning and the steps to mitigate the issue.

在本教程中,我们将检查警告和缓解问题的步骤。

2. Raw Types

2.原始类型

Let’s consider the following code:

让我们来看看下面的代码:

List strings = new ArrayList();

The List and, subsequently, ArrayList are parametrized types. We can see it in the class declaration:

List 以及 ArrayList 都是参数化类型。我们可以从类的声明中看到这一点:

public interface List<E> extends Collection<E> {
    // class body
}

However, it’s called raw types when we use parameterized types without parametrization. This not only reduces the flexibility of our code but also might introduce subtle bugs. Although in some cases, we’re forced to use raw types, mainly for backward compatibility, in general, it’s considered a bad practice.

然而,当我们使用不带参数化的参数化类型时,我们称之为原始类型这不仅会降低代码的灵活性,而且可能会引入微妙的错误。虽然在某些情况下,我们不得不使用原始类型,这主要是为了向后兼容性,但总的来说,这是一种不好的做法。

3. Eclipse Static Analysis

3.Eclipse 静态分析

Eclipse IDE complains about raw types and highlights the problematic parts of the code:

Eclipse IDE 会抱怨原始类型,并高亮显示代码中有问题的部分:

Eclipse-Highlighs-2

Eclipse-Highlighs-2

If we hover a cursor over the highlighted code, we’ll see the following popup:

如果我们将光标悬停在高亮显示的代码上,就会看到下面的弹出窗口:

References-to-generic-types-should-be-parameterized

References-to-generic-types-should-be-parameterized </p

This way, Eclipse helps us to ensure that the code we’re writing doesn’t contain mistakes. It’s especially useful at the beginning of a career. Additionally, it provides a menu with quick fixes. This way, we can easily resolve the problems.

通过这种方式,Eclipse 可以帮助我们确保正在编写的代码不包含错误。此外,它还提供了一个快速修复菜单。这样,我们就能轻松解决问题。

Let’s parametrize the list to avoid the warning:

让我们对列表进行参数化处理,以避免警告:

Eclipse-Highligh-Fix

Eclipse-Highligh-Fix

From Java 5, we don’t need to add parametrization on both sides, and we can use a diamond operator. This is especially useful for long names and parametrizing with several types.

从 Java 5 开始,我们不需要在两侧添加参数,而是可以使用 钻石操作符。这对于长名称和使用多种类型的参数化尤其有用。

4. Conclusion

4.结论

In this article, we discussed the Eclipse IDE process of issuing a “Raw type” popup to draw our attention to the incorrect use of parametrized classes. This popup offers quick fixes to the problem, which can help us resolve the issues faster.

在本文中,我们讨论了 Eclipse IDE 弹出 “Raw type”(原始类型)以提醒我们注意参数化类的错误使用的过程。该弹出窗口提供了快速解决问题的方法,可以帮助我们更快地解决问题。

IDEs and static analysis tools help us write cleaner code and avoid obvious pitfalls. Generics is one of the more advanced topics, and IDEs help identify subtle issues.

IDE 和静态分析工具可帮助我们编写更简洁的代码并避免明显的缺陷。