关于 @SuppressWarnings

Java compilers are increasingly capable of issuing helpful “lint-like” warnings. To encourage the use of such warnings, there should be some way to disable a warning in a part of the program when the programmer knows that the warning is inappropriate.

The annotation type SuppressWarnings supports programmer control over warnings otherwise issued by a Java compiler. It contains a single element that is an array of String.

If a program declaration is annotated with the annotation @SuppressWarnings(value = {S1, ..., Sk}), then a Java compiler must not report any warning identified by one of S1 … Sk if that warning would have been generated as a result of the annotated declaration or any of its parts.

Unchecked warnings are identified by the string “unchecked“.

Compiler vendors should document the warning names they support in conjunction with this annotation type. Vendors are encouraged to cooperate to ensure that the same names work across multiple compilers.

  1. @SuppressWarnings 的值是字符串数组
  2. 使用@SuppressWarnings注解后,注解中设置的任一种告警都不会再提示
  3. 如果使用了被@Deprecated注解注释的类型、方法、字段或者构造器,那么可以使用 @SuppressWarnings("deprecation")来避免告警提示
  4. 在命令行下可以执行javac -X 来查看当前jdk支持哪些值

Leave a Reply

Your email address will not be published. Required fields are marked *