解决“未经检查的警告" Java中避免使用@suppressWarnings [英] Solve "unchecked warning" in Java avoiding @suppressWarnings

查看:170
本文介绍了解决“未经检查的警告" Java中避免使用@suppressWarnings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个试图改善我的代码的问题.关于泛型和未经检查的警告;我确实了解@suppresswarnings批注.问题是我应该编写什么代码来抑制它.请使用以下代码.

This is a Trying to improve my code question. About generics and the unchecked warning; I do know about the @suppresswarnings annotation. The question is what am I suppose to code to suppress it. Take, please, the following code.

public class NumericBox<T extends Number> implements Box<T> {

    private T element;


    public NumericBox(T element) {
        this.element = element;
    }

    @Override public T getElement() {
        return element;
    }

    public T insert(Number newElement) {
        T oldElement = this.element;
        this.element = (T) newElement; // warning("unchecked")
        return oldElement;
    }
}

执行此操作的编码方式是什么(不是注释).在我发现的文档中 未检查"一词意味着编译器没有足够的类型信息来执行所有必要的类型检查,以确保类型安全.但是,不要这么做.这对我来说似乎可以:

What is the coding way to do this (not the annotation). In the docs I've found The term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety. But, don't get it. This seems ok for me:

void foo(Number n) {
    Short asShort = (Short) n; //OK
    T  asTypeWhichExtendsNumber = (T) n; // warning
}

额外.如果我想添加一个空的构造函数,有什么办法在元素中保留零"?

Extra. if I want to add an empty constructor, is any way to keep a "ZERO" in element?

NumeircBox<Short> o = new NumericBox<>(); // expectes 0 in the element

推荐答案

让我们看看有问题的代码,即您的class声明:

Lets look at the code in question, your class declaration:

public class NumericBox<T extends Number> implements Box<T>

所以我们有一些T类型,即extends Number.可能是LongInteger等.但是某些特定在编译时定义的类型.

So we have some type T that extends Number. This might be Long, Integer etc. But some specific and defined at compile time type.

现在您有了一种方法:

public T insert(Number newElement) {
    T oldElement = this.element;
    this.element = (T) newElement;
    return oldElement;
}

使用某些任何类型的Number-既没有特定也没有在编译时定义,然后将其强制为T. 这是一个等待发生的ClassCastException .

Which takes some Number of any type - neither specfic nor defined at compile time and you coerce it into a T. This is a ClassCastException waiting to happen.

final NumericBox<BigDecimal> box = new NumericBox<>();
box.insert(3);
final BigDecimal val = box.getElement();  <-- OOPS!

无法避免此警告,因为编译器在告诉您示例,您有一个未经检查的强制转换,该强制转换可能在运行时导致ClassCastException.编译器正在洗手.

There is no way to avoid this warning because the compiler is telling you example this, you have an unchecked cast that could, at runtime, cause a ClassCastException. The compiler is washing its hands of that line.

因此,要解决您的问题,请将代码更改为:

So, to solve your problem, change the code to:

public T insert(T newElement) {
    T oldElement = this.element;
    this.element = (T) newElement;
    return oldElement;
}

由于泛型是在编译时删除的,因此实际上并没有任何方法来修复该hack.如果要保证运行时类型的安全性,则需要更改签名.如果您想使用任何旧的Number",则必须处理后果.

As generics are erased at compile time, there is not really any way of fixing that hack. If you want to guarantee runtime type safety you need to change the signature. If you want to take "any old Number" then you will have to deal with the consequences.

这篇关于解决“未经检查的警告" Java中避免使用@suppressWarnings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆