Java--@SuppressWarnings 清疚 2022-08-30 04:12 116阅读 0赞 原文网址:[Java--@SuppressWarnings\_IT利刃出鞘的博客-CSDN博客][Java--_SuppressWarnings_IT_-CSDN] # 其他网址 # [@SuppressWarnings使用的正确姿势\_明明如月的专栏-CSDN博客][SuppressWarnings_-CSDN] # 简介 # **说明** > 该注解的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。 # 使用 # ## **使用位置** ## > 支持在类、属性、方法、参数、构造方法、本地变量上使用。 > > 可以通过源码看出: > > @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) > @Retention(RetentionPolicy.SOURCE) > public @interface SuppressWarnings { > /** > * The set of warnings that are to be suppressed by the compiler in the > * annotated element. Duplicate names are permitted. The second and > * successive occurrences of a name are ignored. The presence of > * unrecognized warning names is <i>not</i> an error: Compilers must > * ignore any warning names they do not recognize. They are, however, > * free to emit a warning if an annotation contains an unrecognized > * warning name. > * > * <p> The string {@code "unchecked"} is used to suppress > * unchecked warnings. Compiler vendors should document the > * additional warning names they support in conjunction with this > * annotation type. They are encouraged to cooperate to ensure > * that the same names work across multiple compilers. > * @return the set of warnings to be suppressed > */ > String[] value(); > } ## 抑制的种类 ## @SuppressWarnings注解的使用有三种: 1. @SuppressWarnings("unchecked") \[抑制单类型的警告\] 2. @SuppressWarnings("unchecked","rawtypes") \[抑制多类型的警告\] 3. @SuppressWarnings("all") \[ 抑制所有类型的警告\] ## 大全 ## <table> <thead> <tr> <th style="width:142px;"><strong>关键字</strong></th> <th style="width:547px;"><strong>用途</strong></th> </tr> </thead> <tbody> <tr> <td style="width:142px;">all</td> <td style="width:547px;"> <p>to suppress all warnings</p> <p>(抑制所有警告)</p> </td> </tr> <tr> <td style="width:142px;">boxing</td> <td style="width:547px;"> <p>to suppress warnings relative to boxing/unboxing operations</p> <p>(抑制装箱、拆箱操作时候的警告)</p> </td> </tr> <tr> <td style="width:142px;">cast</td> <td style="width:547px;"> <p>to suppress warnings relative to cast operations</p> <p>(抑制映射相关的警告)</p> </td> </tr> <tr> <td style="width:142px;">dep-ann</td> <td style="width:547px;"> <p>to suppress warnings relative to deprecated annotation</p> <p>(抑制启用注释的警告)</p> </td> </tr> <tr> <td style="width:142px;">deprecation</td> <td style="width:547px;"> <p>to suppress warnings relative to deprecation</p> <p>(抑制过期方法警告)</p> </td> </tr> <tr> <td style="width:142px;">fallthrough</td> <td style="width:547px;"> <p>to suppress warnings relative to missing breaks in switch statements</p> <p>(抑制确在switch中缺失breaks的警告)</p> </td> </tr> <tr> <td style="width:142px;">finally</td> <td style="width:547px;"> <p>to suppress warnings relative to finally block that don’t return</p> <p>(抑制finally模块没有返回的警告)</p> </td> </tr> <tr> <td style="width:142px;">hiding</td> <td style="width:547px;"> <p>to suppress warnings relative to locals that hide variable</p> <p>(抑制相对于隐藏变量的局部变量的警告)</p> </td> </tr> <tr> <td style="width:142px;">incomplete-switch</td> <td style="width:547px;"> <p>to suppress warnings relative to missing entries in a switch statement (enum case)</p> <p>(忽略没有完整的switch语句)</p> </td> </tr> <tr> <td style="width:142px;">nls</td> <td style="width:547px;"> <p>to suppress warnings relative to non-nls string literals</p> <p>( 忽略非nls格式的字符)</p> </td> </tr> <tr> <td style="width:142px;">null</td> <td style="width:547px;"> <p>to suppress warnings relative to null analysis</p> <p>( 忽略对null的操作)</p> </td> </tr> <tr> <td style="width:142px;">rawtypes</td> <td style="width:547px;"> <p>to suppress warnings relative to un-specific types when using generics on class params</p> <p>( 使用generics时忽略没有指定相应的类型)</p> </td> </tr> <tr> <td style="width:142px;">restriction</td> <td style="width:547px;"> <p>to suppress warnings relative to usage of discouraged or forbidden references</p> <p>( 抑制禁止使用劝阻或禁止引用的警告)</p> </td> </tr> <tr> <td style="width:142px;">serial</td> <td style="width:547px;"> <p>to suppress warnings relative to missing serialVersionUID field for a serializable class</p> <p>( 忽略在serializable类中没有声明serialVersionUID变量)</p> </td> </tr> <tr> <td style="width:142px;">static-access</td> <td style="width:547px;"> <p>to suppress warnings relative to incorrect static access</p> <p>( 抑制不正确的静态访问方式警告)</p> </td> </tr> <tr> <td style="width:142px;">synthetic-access</td> <td style="width:547px;"> <p>to suppress warnings relative to unoptimized access from inner classes</p> <p>( 抑制子类没有按最优方法访问内部类的警告)</p> </td> </tr> <tr> <td style="width:142px;">unchecked</td> <td style="width:547px;"> <p>to suppress warnings relative to unchecked operations</p> <p>( 抑制没有进行类型检查操作的警告)</p> </td> </tr> <tr> <td style="width:142px;">unqualified-field-access</td> <td style="width:547px;"> <p>to suppress warnings relative to field access unqualified</p> <p>( 抑制没有权限访问的域的警告)</p> </td> </tr> <tr> <td style="width:142px;">unused</td> <td style="width:547px;"> <p>to suppress warnings relative to unused code(</p> <p>抑制没被使用过的代码的警告)</p> </td> </tr> </tbody> </table> [Java--_SuppressWarnings_IT_-CSDN]: https://knife.blog.csdn.net/article/details/118883760 [SuppressWarnings_-CSDN]: https://blog.csdn.net/w605283073/article/details/85704202
还没有评论,来说两句吧...