Java的 @SuppressWarnings 注释
注解是Java在现代技术中非常重要的一部分,Hibernate、Spring、Spring Boot、JPA等大部分技术都在使用注解,让开发者的生活变得轻松很多。在Java,内置的通用注解是——
- @覆盖
- @已弃用
- @功能接口
- @SuppressWarnings
语法: Java @SuppressWarnings 注解的签名如下:
@Retention(value=SOURCE)
@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })
public @interface SuppressWarnings {
String[] value;
}
我们可以看到,上面的签名只有一个元素,它是字符串数组,有多个可能的值。
所有注释都有两个属性:
- Target (@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })) – 几乎所有东西都可以使用它,无论你想抑制警告的地方。
- Retention (@Retention(value=SOURCE)):功能接口“SOURCE”的保留策略,这意味着注释不会到编译器去。
插图:
使用@SuppressWarnings 是为了抑制或忽略来自编译器的警告,即编译器将忽略该代码段的警告(如果有)。
1. @SuppressWarnings("unchecked")
public class Calculator {
}
- Here, it will ignore all unchecked warnings coming from that class. (All methods, variables, constructors).
2. public class Calculator {
@SuppressWarnings("unchecked")
public int sum(x,y) {
.
}
}
- It will stop warning from that function only, and not from other functions of Calculator class.
这个注解是危险的,因为警告是代码中潜在的错误。因此,如果我们收到任何警告,第一种方法应该是解决这些错误。但是如果我们要压制任何警告,我们必须有一些充分的理由。每次使用时都应该在注释附近注释原因。
@SuppressWarnings 注释元素内的可能值如下:Values
Description All It will suppress all warnings. Cast Suppress the warning while casting from a generic type to a nonqualified type or the other way around. Deprecation Ignores when we’re using a deprecated(no longer important) method or type. divzero Suppresses division by zero warning. empty Ignores warning of a statement with an empty body. unchecked It doesn’t check if the data type is Object or primitive. fallthrough Ignores fall-through on switch statements usually (if “break” is missing). hiding It suppresses warnings relative to locals that hide variable serial It makes the compiler shut up about a missing serialVersionUID. finally Avoids warnings relative to finally block that doesn’t return. unused To suppress warnings relative to unused code.
Note: The primary and most important benefit of using @SuppressWarnings Annotation is that if we stuck because of some known warning, then this will ignore the warning and move ahead. E.g. – deprecated and unchecked warnings.
例子:
Java
// Java Program to demonstrate Use of @SuppressWarnings
// Annotation
// Importing required packages
import java.io.*;
import java.lang.*;
import java.util.*;
// Class 1
// Helper class
class Addition {
// Method 1
public static int sum(int n1, int n2)
{
// Return the final sum
return n1 + n2;
}
// Method 2
public static int sum(int... nums)
{
int sum = 0;
for (int i : nums) {
sum += i;
}
// Return the final sum
return sum;
}
}
// Class 2
// Main class
// To test suppress warnings
public class GFG {
// Does not check if data type is Object or primitve
@SuppressWarnings("unchecked")
// Main driver method
public static void main(String[] args)
{
// Creating an object of above class in main()
// method
Addition add = new Addition();
// Ignore when we're using a deprecated
// (no longer important) method or type
@SuppressWarnings("deprecation")
int sum = Addition.sum(10, 20);
// Print and display the sum
System.out.println("Sum of 10 and 20 : " + sum);
@SuppressWarnings("rawtypes")
// Raw data type being used instead of generic
List list = new ArrayList();
// Custom input entries
list.add(12);
list.add(120);
// Print and display List elements
System.out.println("List items : " + list);
}
}
Sum of 10 and 20 : 30
List items : [12, 120]