@SuppressWarnings注解的功能简介说明

欣喜 Java经验 发布时间:2025-03-28 11:05:05 阅读数:7140 1
下文笔者讲述"@SuppressWarnings"注解的功能简介说明,如下所示

@SuppressWarnings注解简介

`@SuppressWarnings`是Java中一个注解
  用于告诉编译器忽略特定的警告信息
    在一些特殊的场景,这个注解非常有用, 
	  如:在确定代码是正确的并且不希望被警告干扰的情况下 
===========================================================
@SuppressWarnings注解常见的警告类型:
    包括 `all`、`unchecked`、`deprecation`、`rawtypes`、`unused`、`serial`、`resource` 等 
    -`all`:
	    抑制所有警告。
    -`unchecked`:
	    抑制未经检查的类型转换警告。
    -`deprecation`:
	    抑制过时方法或类的警告。
    -`rawtypes`:
	    抑制原始类型使用警告。
    -`unused`:
	    抑制未使用代码的警告。
    -`serial`:
	    抑制缺少 `serialVersionUID` 的警告。
    -`resource`:
	    抑制资源管理相关的警告(Java 7 及以上)。
    -`finally`:
	    抑制 `finally` 块中可能丢失的异常警告。
    -`fallthrough`:
	    抑制 `switch` 语句中缺少 `break` 的警告(Java 7 及以上)。

-作用范围:
    可以应用于类、方法、字段、局部变量等。
-警告类型:
   可以指定要抑制的具体警告类型。

抑制所有警告

@SuppressWarnings("all")
public class SuppressAllWarnings {
    public void someMethod() {
        // 可能会产生警告的代码
        list list = new ArrayList();
        list.add("Hello");
        System.out.println(list.get(0));
    }
}

3.2.抑制未经检查的类型转换警告

import java.util.ArrayList;
import java.util.List;

public class SuppressUncheckedWarnings {
    @SuppressWarnings("unchecked")
    public void someMethod() {
        // 抑制未经检查的类型转换警告
        List list = new ArrayList();
        list.add("Hello");
        String str = (String) list.get(0);
        System.out.println(str);
    }
}

3.3.抑制过时方法或类的警告

@Deprecated
public class DeprecatedClass {
    @Deprecated
    public void deprecatedMethod() {
        System.out.println("This method is deprecated.");
    }
}

public class SuppressDeprecationWarnings {
    @SuppressWarnings("deprecation")
    public void someMethod() {
        DeprecatedClass obj = new DeprecatedClass();
        obj.deprecatedMethod();
    }
}

3.4.抑制原始类型使用警告

import java.util.ArrayList;
import java.util.List;

public class SuppressRawtypesWarnings {
    @SuppressWarnings("rawtypes")
    public void someMethod() {
        // 抑制原始类型使用警告
        List list = new ArrayList();
        list.add("Hello");
        System.out.println(list.get(0));
    }
}

3.5.抑制未使用代码的警告

public class SuppressUnusedWarnings {
    @SuppressWarnings("unused")
    public void someMethod() {
        int unusedVariable = 10; // 抑制未使用变量的警告
    }
}

3.6.抑制缺少 `serialVersionUID`警告

import java.io.Serializable;

@SuppressWarnings("serial")
public class SuppressSerialWarnings implements Serializable {
    private int value;

    public SuppressSerialWarnings(int value) {
        this.value = value;
    }
}

3.7.抑制资源管理相关的警告

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SuppressResourceWarnings {
    @SuppressWarnings("resource")
    public void someMethod() {
        // 抑制资源管理相关的警告
        BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

`@SuppressWarnings` 注解可以应用于不同的代码元素
包括类、方法、字段、局部变量等。

4.1.应用于类

@SuppressWarnings("unchecked")
public class SuppressClassWarnings {
    public void someMethod() {
        List list = new ArrayList();
        list.add("Hello");
        System.out.println(list.get(0));
    }
}

4.2.应用于方法

public class SuppressMethodWarnings {
    @SuppressWarnings("unchecked")
    public void someMethod() {
        List list = new ArrayList();
        list.add("Hello");
        System.out.println(list.get(0));
    }
}

4.3.应用于字段

public class SuppressFieldWarnings {
    @SuppressWarnings("unused")
    private int unusedField;

    public void someMethod() {
        // 方法体
    }
}

4.4.应用于局部变量

public class SuppressLocalVariableWarnings {
    public void someMethod() {
        @SuppressWarnings("unused")
        int unusedVariable = 10;
    }
}

综合示例

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
class SuppressSerialWarnings implements java.io.Serializable {
    private int value;

    public SuppressSerialWarnings(int value) {
        this.value = value;
    }
}

public class SuppressWarningsExample {

    @SuppressWarnings("unchecked")
    public void someMethod() {
        // 抑制未经检查的类型转换警告
        List list = new ArrayList();
        list.add("Hello");
        String str = (String) list.get(0);
        System.out.println(str);
    }

    @SuppressWarnings("deprecation")
    public void deprecatedMethod() {
        // 抑制过时方法的警告
        DeprecatedClass obj = new DeprecatedClass();
        obj.deprecatedMethod();
    }

    @SuppressWarnings("rawtypes")
    public void rawtypesMethod() {
        // 抑制原始类型使用警告
        List list = new ArrayList();
        list.add("Hello");
        System.out.println(list.get(0));
    }

    @SuppressWarnings("unused")
    public void unusedMethod() {
        // 抑制未使用变量的警告
        int unusedVariable = 10;
    }

    @SuppressWarnings("resource")
    public void resourceMethod() {
        // 抑制资源管理相关的警告
        BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SuppressWarningsExample example = new SuppressWarningsExample();
        example.someMethod();
        example.deprecatedMethod();
        example.rawtypesMethod();
        example.unusedMethod();
        example.resourceMethod();
    }
}

@Deprecated
class DeprecatedClass {
    @Deprecated
    public void deprecatedMethod() {
        System.out.println("This method is deprecated.");
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202503/17431321088412.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者