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

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

问题描述

这是一个试图改进我的代码的问题。关于泛型和未经检查的警告;我确实知道 @suppresswarning 注释。问题是我想要编码来压制它。请拿下面的代码。

This is a Trying to improve my code question. About generics and the unchecked warning; I do know about the @suppresswarning 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;
    }

}

什么是编码方式这(不是注释)。在文档中我找到
unchecked一词表示编译器没有足够的类型信息来执行所有必需的类型检查,以确保类型安全。但是,不要它。这对我来说似乎没有问题:

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
}

额外。如果我想添加一个空的构造函数,是否有任何方法让元素保持ZERO?

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 code>扩展Number 。这可能是 Long Integer 等。但是一些特定的编译时间类型。

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

现在您有一个方法:

Now you have a method:

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

这需要一些数字任何类型 - 既不是在编译时定义 specfic ,而是将它强制为 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!

没有办法避免这个警告,因为编译器告诉你这个例子,你有一个unchecked它可以在运行时导致 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.


$ b

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

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

由于泛型在编译时被擦除,所以并没有真正的 修复该黑客的方法。如果你想保证运行时类型安全,你需要改变签名。如果你想采取任何旧的数字,那么你将不得不处理后果。

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中避免@supressWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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