1. Overview
1.概述
In this tutorial, we’ll take a look at the different warning names that work with the @SuppressWarnings Java annotation, which allows us to suppress compiler warnings. These warning names allow us to suppress particular warnings. The warning names available will depend on our IDE or Java compiler. The Eclipse IDE is our reference for this article.
在本教程中,我们将看看与@SuppressWarnings Java注解配合使用的不同警告名称,它允许我们抑制编译器的警告。这些警告名称允许我们抑制特定的警告。可用的警告名称将取决于我们的IDE或Java编译器。Eclipse IDE是我们在本文中的参考。
2. Warning Names
2.警告名称
Below is a list of valid warning names available in the @SuppressWarnings annotation:
下面是@SuppressWarnings注解中可用的有效警告名称的列表。
- all: this is sort of a wildcard that suppresses all warnings
- boxing: suppresses warnings related to boxing/unboxing operations
- unused: suppresses warnings of unused code
- cast: suppresses warnings related to object cast operations
- deprecation: suppresses warnings related to deprecation, such as a deprecated class or method
- restriction: suppresses warnings related to the usage of discouraged or forbidden references
- dep-ann: suppresses warnings relative to deprecated annotations
- fallthrough: suppresses warnings related to missing break statements in switch statements
- finally: suppresses warnings related to finally blocks that don’t return
- hiding: suppresses warnings relative to locals that hide variables
- incomplete-switch: suppresses warnings relative to missing entries in a switch statement (enum case)
- nls: suppresses warnings related to non-nls string literals
- null: suppresses warnings related to null analysis
- serial: suppresses warnings related to the missing serialVersionUID field, which is typically found in a Serializable class
- static-access: suppresses warnings related to incorrect static variable access
- synthetic-access: suppresses warnings related to unoptimized access from inner classes
- unchecked: suppresses warnings related to unchecked operations
- unqualified-field-access: suppresses warnings related to unqualified field access
- javadoc: suppresses warnings related to Javadoc
- rawtypes: suppresses warnings related to the usage of raw types
- resource: suppresses warnings related to the usage of resources of type Closeable
- super: suppresses warnings related to overriding a method without super invocations
- sync-override: suppresses warnings due to missing synchronize when overriding a synchronized method
3. Using Warning Names
3.使用警告名称
This section will show examples of the use of different warning names.
本节将展示不同警告名称的使用实例。
3.1. @SuppressWarnings(“unused”)
3.1.@SuppressWarnings(“unused”)
In the example below, the warning name suppresses the warning of the unusedVal in the method:
在下面的例子中,警告名称抑制了方法中的unusedVal的警告。
@SuppressWarnings("unused")
void suppressUnusedWarning() {
int usedVal = 5;
int unusedVal = 10; // no warning here
List<Integer> list = new ArrayList<>();
list.add(usedVal);
}
3.2. @SuppressWarnings(“deprecated”)
3.2.@SuppressWarnings(“deprecated”)
In the example below, the warning name suppresses the warning of the usage of the @deprecated method:
在下面的例子中,警告名称抑制了对使用@deprecated方法的警告。
@SuppressWarnings("deprecated")
void suppressDeprecatedWarning() {
ClassWithSuppressWarningsNames cls = new ClassWithSuppressWarningsNames();
cls.deprecatedMethod(); // no warning here
}
@Deprecated
String deprecatedMethod() {
return "deprecated method";
}
3.3. @SuppressWarnings(“fallthrough”)
3.3.@SuppressWarnings(“fallthrough”)
In the example below, the warning name suppresses the warning of the missing break statements — we’ve included them here, commented out, to show where we would otherwise get the warning:
在下面的例子中,警告名称抑制了缺少break语句的警告–我们把它们包括在这里,并加以注释,以显示我们在其他地方会得到警告。
@SuppressWarnings("fallthrough")
String suppressFallthroughWarning() {
int day = 5;
switch (day) {
case 5:
return "This is day 5";
// break; // no warning here
case 10:
return "This is day 10";
// break; // no warning here
default:
return "This default day";
}
}
3.4. @SuppressWarnings(“serial”)
3.4.@SuppressWarnings(“serial”)
This warning name is placed at the class level. In the example below, the warning name suppresses the warning of the missing serialVersionUID (which we’ve commented out) in a Serializable class:
这个警告名被放置在类的层次上。在下面的例子中,这个警告名抑制了serialVersionUID(我们已经注释掉了)在一个Serializable类中缺失的警告。
@SuppressWarnings("serial")
public class ClassWithSuppressWarningsNames implements Serializable {
// private static final long serialVersionUID = -1166032307853492833L; // no warning even though this is commented
4. Combining Multiple Warning Names
4.合并多个警告名称
The @SuppressWarnings annotation expects an array of Strings, so we can combine multiple warning names:
@SuppressWarnings注解期望一个Strings数组,所以我们可以组合多个警告名称。
@SuppressWarnings({"serial", "unchecked"})
5. Conclusion
5.总结
This article provides a list of valid @SuppressWarnings warning names. As usual, all code samples shown in this tutorial are available over on GitHub.
本文提供了一个有效的@SuppressWarnings警告名称列表。像往常一样,本教程中显示的所有代码样本都可以在GitHub上获得。