`

@SuppressWarnings的使用、作用、用法

    博客分类:
  • Java
 
阅读更多

在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上

@SuppressWarnings(“XXXX”) 来解决

例如:@SuppressWarnings("deprecation")表示不显示使用了不赞成使用的类或方法时的警告

具体的XXXX的意义可以参考博文

http://www.thebuzzmedia.com/supported-values-for-suppresswarnings/

 

Update #1All these annotations are still valid in Eclipse 3.4 and 3.5, there have been no new SuppressWarning arguments added in those versions of the JDT compiler.

If you are a Java developer and use the new @SuppressWarnings annotation in your code from time-to-time to suppress compiler warnings you, like me, have wondered probably about a million times alreadyjust exactly what are the supported values that can be used with this annotation.

The reason the list isn’t easy to find is because it’s compiler specific, which means Sun may have a different set of supported values than say IBM, GCJ or Apache Harmony.

Fortunately for us, the Eclipse folks have documented the values they support (As of Eclipse 3.3), here they are for reference:

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch statements
  • finally to suppress warnings relative to finally block that don’t return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • rawtypes to suppress warnings relative to un-specific types when using generics on class params
  • restriction to suppress warnings relative to usage of discouraged or forbidden references
  • serial to suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access to suppress warnings relative to incorrect static access
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field access unqualified
  • unused to suppress warnings relative to unused code
分享到:
评论

相关推荐

    @SuppressWarnings

    - **抑制特定类型的警告**:例如,当你明确知道某个类或方法使用了过时的方法(`@Deprecated`),但又必须使用它时,可以通过`@SuppressWarnings("deprecation")`来抑制这类警告。 - **代码审查后的选择性忽略**:在...

    关于@SuppressWarnings("uncheck ")(转)

    - **明确指定**:在使用 `@SuppressWarnings("unchecked")` 时,最好明确指定要抑制哪种类型的警告,而不是笼统地使用 `@SuppressWarnings("all")`,这样可以帮助其他开发者理解为何此处需要抑制警告。 - **与工具...

    @SuppressWarnings简介

    虽然 `@SuppressWarnings` 可以帮助减少警告,但过度使用或不恰当的使用可能会掩盖实际的问题。因此,最好只在必要时使用,并且理解为何需要忽略特定的警告。此外,当新的编译器版本增加新的警告类型时,已有的 `@...

    suppressWarnings注解参数介绍

    如果由于兼容性或暂时无法重构的原因,必须在代码中继续使用这些过时的方法或类,可以使用此注解来消除编译器警告。例如: ```java @SuppressWarnings("deprecation") public void someMethod() { java.util.Date ...

    java SuppressWarnings

    在此示例中,`foo`方法被标记为过时,但在`main`方法中仍然调用了它,通过使用`@SuppressWarnings("deprecation")`注解,我们可以选择性地抑制这个警告。 ##### 3.2 未检查的转换(Unchecked) 当使用非泛型容器...

    java中的SuppressWarnings(xxx).doc

    例如,使用 @SuppressWarnings("unchecked") 可以抑制unchecked警告信息,使用 @SuppressWarnings("deprecation") 可以抑制废弃警告信息。 三、serialVersionUID 的作用 serialVersionUID 是一个私有静态常量,...

    java中使用list会出现黄色警告图标如何去除参考.pdf

    解决方法是使用泛型,例如 `List<String> list = new ArrayList();`,这样可以明确 List 中保存的对象类型。 在 Eclipse 中,如果出现黄色警告图标,可以点击编辑器中的警告图标,选择添加 `@SuppressWarnings(...

    java除去类里面的黄色警告

    为了抑制这种警告,可以使用`@SuppressWarnings("unchecked")`注解,但需谨慎使用,因为这可能隐藏了潜在的类型安全问题。 ### `@SuppressWarnings("deprecation")` 当一个方法、类或构造函数被标记为过时...

    flume-plugin-maven-plugin-1.0.zip

    例如,如果某个方法的参数类型在编译时无法确定,编译器可能会发出警告,但如果我们确定在运行时不会出现问题,就可以使用@SuppressWarnings("rawtypes")来抑制这个警告。 在"suppressWarnings-master"目录下,我们...

    注解annotation的详细介绍

    注解(Annotation)是Java语言中的一个重要特性...通过使用`@Override`来确保方法重写正确,`@Deprecated`来标记不再推荐使用的代码,以及`@SuppressWarnings`来控制编译警告,开发者能够更好地管理和优化他们的代码。

    Java JDK 6学习笔记——ppt简体版 第17章.ppt

    同样,可以使用`@SuppressWarnings("deprecation")`来抑制与使用`@Deprecated`方法相关的警告。此外,可以使用`value`参数来指定多个要抑制的警告类型,如`@SuppressWarnings(value={"unchecked", "deprecation"})`...

    Java中的注解Annotationsnava)1

    在给定的例子中,类`Sub1`的`m()`方法正确地使用了`@Override`,而类`Sub2`的`m()`方法没有使用,尽管它也覆盖了`Super`类的`m()`方法。通过CodeQL查询,我们可以识别出这类情况,从而帮助开发者改进代码质量。 总...

    SuppressWarning:添加SuppressWarning批注的简单应用程序

    本文将深入探讨`@ SuppressWarnings `注解的使用、作用以及如何在实际项目中应用。 `@ SuppressWarnings `注解的基本语法如下: ```java @SuppressWarnings("warning_type") ``` 其中,`warning_type`是你要抑制...

    java学习笔记JDK6课件之十七

    当在子类中使用此注解时,编译器会检查该方法是否真的覆盖了父类的方法,如果没有,则会抛出错误。例如,在`CustomClass`的例子中,编译器发现`toString()`方法并没有覆盖父类的方法,因此报错。 2. **`@Deprecated...

    Java常用内置注解用法分析

    在本文中,我们将详细介绍Java常用内置注解的用法,并通过实例形式分析了Java使用@SuppressWarnings关闭警告信息以及@Depreca标注的元素不使用两种注解使用方法。 一、 @SuppressWarnings注解 @SuppressWarnings...

    jdk注释讲解

    如果子类方法与父类或接口中的方法签名完全匹配,则使用此注解可以提高代码的可读性和安全性,避免意外地重写错误的方法。例如,当重写`equals`方法时: ```java public boolean equals(Point pt) { // 实现逻辑.....

    Annotation百度百科

    它的作用是确保该方法确实是父类方法的重写版本,而不是新方法。使用此注解可以帮助避免错误地覆盖了父类的方法,从而提高代码的可读性和维护性。 示例: ```java @Override public Hay getPreferredFood() { ...

    ibatis例子=》包含了常用方法介绍

    包含SqlMapClient里全部方法的介绍,附带了数据库,绝对的原创,本资源绝对的免费 /* * 带参数的queryForMap用法 */ @SuppressWarnings("unchecked") public Map queryForMap(String sql_name, Object ...

    annotation 详解

    当使用`@Override`标注一个方法时,如果该方法实际上并未重写父类中的方法,编译器会报错。 - **示例** 假设我们有一个子类`CustomClass`,并希望重写父类的`toString()`方法: ```java public class Custom...

Global site tag (gtag.js) - Google Analytics