什么是 Java 中的 SuppressWarnings(“未选中”)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1129795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:04:46  来源:igfitidea点击:

What is SuppressWarnings ("unchecked") in Java?

javagenericsuncheckedsuppress-warnings

提问by jojo

Sometime when looking through code, I see many methods specify an annotation:

有时在查看代码时,我看到许多方法指定了一个注释:

@SuppressWarnings("unchecked")

What does this mean?

这是什么意思?

采纳答案by Jon Skeet

Sometimes Java generics just doesn't let you do what you want to, and you need to effectively tell the compiler that what you're doing really willbe legal at execution time.

有时Java泛型只是没有让你做你想要什么,你需要有效地告诉编译器,你在做什么真的是在执行时的法律。

I usually find this a pain when I'm mocking a generic interface, but there are other examples too. It's usually worth trying to work out a way of avoiding the warning rather than suppressing it (the Java Generics FAQhelps here) but sometimes even if it ispossible, it bends the code out of shape so much that suppressing the warning is neater. Always add an explanatory comment in that case!

当我模拟通用接口时,我通常会觉得这很痛苦,但也有其他示例。它通常是值得尝试找出避免了警告,而不是抑制它的方法(在Java泛型常见问题帮助这里),但有时即使可能的,它弯曲的代码变形,以至于抑制警告整洁。在这种情况下,请务必添加解释性注释!

The same generics FAQ has several sections on this topic, starting with "What is an "unchecked" warning?"- it's well worth a read.

相同的泛型常见问题有关于这个主题的几个部分,从“什么是“未经检查的”警告开始?- 非常值得一读。

回答by dreadwail

It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions), such as casts. It essentially implies that the programmer did not wish to be notified about these which he is already aware of when compiling a particular bit of code.

这是一个注释,用于抑制有关未经检查的通用操作(不是异常)的编译警告,例如强制转换。它本质上意味着程序员不希望收到有关他在编译特定代码位时已经知道的这些信息的通知。

You can read more on this specific annotation here:

您可以在此处阅读有关此特定注释的更多信息:

SuppressWarnings

禁止警告

Additionally, Oracle provides some tutorial documentation on the usage of annotations here:

此外,Oracle 在此处提供了一些有关注释使用的教程文档:

Annotations

注释

As they put it,

正如他们所说,

"The 'unchecked' warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled Generics)."

“在与泛型出现之前编写的遗留代码交互时,可能会出现‘未经检查’警告(在题为泛型的课程中讨论)。”

回答by Brandon E Taylor

The SuppressWarningannotation is used to suppress compiler warnings for the annotated element. Specifically, the uncheckedcategory allows suppression of compiler warnings generated as a result of unchecked type casts.

SuppressWarning注释用于抑制编译器警告的注解元素。具体来说,该unchecked类别允许抑制由于未经检查的类型转换而生成的编译器警告。

回答by BakerTheHacker

As far I know, for now it has to do with suppressing warnings about generics; generics are a new programming construct not supported in JDK versions earlier than JDK 5, so any mixes of the old constructs with the new ones might pose some unexpected results.

据我所知,现在它与抑制关于泛型的警告有关;泛型是 JDK 5 之前的 JDK 版本不支持的新编程结构,因此旧结构与新结构的任何混合都可能会导致一些意想不到的结果。

The compiler warns the programmer about it, but if the programmer already knows, they can turn those dreaded warnings off using SuppressWarnings.

编译器会警告程序员,但如果程序员已经知道,他们可以使用 SuppressWarnings 关闭那些可怕的警告。

回答by akarnokd

It could also mean that the current Java type system version isn't good enough for your case. There were several JSR propositions/ hacks to fix this: Type tokens, Super Type Tokens, Class.cast().

这也可能意味着当前的 Java 类型系统版本对您的情况来说不够好。有几个JSR 提议/技巧可以解决这个问题:类型标记、超级类型标记、Class.cast()。

If you really need this supression, narrow it down as much as possible (e.g. don't put it onto the class itself or onto a long method). An example:

如果您确实需要这种抑制,请尽可能缩小范围(例如,不要将其放在类本身或长方法上)。一个例子:

public List<String> getALegacyListReversed() {
   @SuppressWarnings("unchecked") List<String> list =
       (List<String>)legacyLibrary.getStringList();

   Collections.reverse(list);
   return list;
}

回答by s.k.sumaprakash

You can suppress the compiler warnings and tell the generics that the code which you had written is legal according to it.

您可以禁止编译器警告并告诉泛型您编写的代码根据它是合法的。

Example:

例子:

@SuppressWarnings("unchecked")
public List<ReservationMealPlan> retreiveMealPlan() {
     List<ReservationMealPlan> list=new ArrayList<ReservationMealPlan>();
    TestMenuService testMenuService=new TestMenuService(em, this.selectedInstance);
    list = testMenuService.getMeal(reservationMealPlan);
    return list;
 }

回答by Brian Edwards

One trick is to create an interface that extends a generic base interface...

一个技巧是创建一个扩展通用基本接口的接口......

public interface LoadFutures extends Map<UUID, Future<LoadResult>> {}

Then you can check it with instanceof before the cast...

然后你可以在演员之前用 instanceof 检查它......

Object obj = context.getAttribute(FUTURES);
if (!(obj instanceof LoadFutures)) {
    String format = "Servlet context attribute \"%s\" is not of type "
            + "LoadFutures. Its type is %s.";
    String msg = String.format(format, FUTURES, obj.getClass());
    throw new RuntimeException(msg);
}
return (LoadFutures) obj;

回答by Daniel Perník

Simply: It's a warning by which the compiler indicates that it cannot ensure type safety.

简单地说:这是一个警告,编译器表明它无法确保类型安全。

JPA service method for example:

JPA服务方法例如:

@SuppressWarnings("unchecked")
public List<User> findAllUsers(){
    Query query = entitymanager.createQuery("SELECT u FROM User u");
    return (List<User>)query.getResultList();
}

If I didn'n anotate the @SuppressWarnings("unchecked") here, it would have a problem with line, where I want to return my ResultList.

如果我没有在这里对 @SuppressWarnings("unchecked") 进行注释,那么我想返回我的 ResultList 的行会出现问题。

In shortcut type-safety means: A program is considered type-safe if it compiles without errors and warnings and does not raise any unexpected ClassCastException s at runtime.

在快捷方式中,类型安全意味着:如果程序在编译时没有错误和警告,并且在运行时没有引发任何意外的 ClassCastException ,则该程序被认为是类型安全的。

I build on http://www.angelikalanger.com/GenericsFAQ/FAQSections/Fundamentals.html

我建立在http://www.angelikalanger.com/GenericsFAQ/FAQSections/Fundamentals.html

回答by Konrad Borowski

In Java, generics are implemented by means of type erasure. For instance, the following code.

在 Java 中,泛型是通过类型擦除来实现的。例如,下面的代码。

List<String> hello = List.of("a", "b");
String example = hello.get(0);

Is compiled to the following.

编译如下。

List hello = List.of("a", "b");
String example = (String) hello.get(0);

And List.ofis defined as.

并且List.of定义为。

static <E> List<E> of(E e1, E e2);

Which after type erasure becomes.

类型擦除后成为。

static List of(Object e1, Object e2);

The compiler has no idea what are generic types at runtime, so if you write something like this.

编译器在运行时不知道什么是泛型类型,所以如果你写这样的东西。

Object list = List.of("a", "b");
List<Integer> actualList = (List<Integer>) list;

Java Virtual Machine has no idea what generic types are while running a program, so this compiles and runs, as for Java Virtual Machine, this is a cast to Listtype (this is the only thing it can verify, so it verifies only that).

Java 虚拟机在运行程序时不知道什么是泛型类型,所以它编译并运行,对于 Java 虚拟机,这是一个List类型转换(这是它唯一可以验证的东西,所以它只验证那个)。

But now add this line.

但现在添加这一行。

Integer hello = actualList.get(0);

And JVM will throw an unexpected ClassCastException, as Java compiler inserted an implicit cast.

并且 JVM 会抛出一个意外的ClassCastException,因为 Java 编译器插入了一个隐式强制转换。

java.lang.ClassCastException: java.base/java.lang.String cannot be cast to java.base/java.lang.Integer

An uncheckedwarning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked")tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

一个unchecked警告讲述了一个程序员,一个投可能导致程序抛出一个异常别处。用 with 抑制警告@SuppressWarnings("unchecked")告诉编译器程序员认为代码是安全的并且不会导致意外异常。

Why would you want to do that? Java type system isn't good enough to represent all possible type usage patterns. Sometimes you may know that a cast is safe, but Java doesn't provide a way to say so - to hide warnings like this, @SupressWarnings("unchecked")can be used, so that a programmer can focus on real warnings. For instance, Optional.empty()returns a singleton to avoid allocation of empty optionals that don't store a value.

你为什么想这么做?Java 类型系统不足以表示所有可能的类型使用模式。有时您可能知道强制转换是安全的,但 Java 并没有提供这样的表达方式 -@SupressWarnings("unchecked")可以使用这样隐藏警告,以便程序员可以专注于真正的警告。例如,Optional.empty()返回一个单例以避免分配不存储值的空选项。

private static final Optional<?> EMPTY = new Optional<>();
public static<T> Optional<T> empty() {
    @SuppressWarnings("unchecked")
    Optional<T> t = (Optional<T>) EMPTY;
    return t;
}

This cast is safe, as the value stored in an empty optional cannot be retrieved so there is no risk of unexpected class cast exceptions.

此转换是安全的,因为无法检索存储在空可选项中的值,因此不存在意外类转换异常的风险。

回答by Raghu K Nair

A warning by which the compiler indicates that it cannot ensure type safety. The term "unchecked" warning is misleading. It does not mean that the warning is unchecked in any way. The term "unchecked" refers to the fact that the compiler and the runtime system do not have enough type information to perform all type checks that would be necessary to ensure type safety. In this sense, certain operations are "unchecked".

编译器指示它无法确保类型安全的警告。术语“未经检查的”警告具有误导性。这并不意味着以任何方式取消检查警告。术语“未检查”是指编译器和运行时系统没有足够的类型信息来执行确保类型安全所必需的所有类型检查。从这个意义上说,某些操作是“未经检查的”。

The most common source of "unchecked" warnings is the use of raw types. "unchecked" warnings are issued when an object is accessed through a raw type variable, because the raw type does not provide enough type information to perform all necessary type checks.

“未经检查”警告的最常见来源是使用原始类型。通过原始类型变量访问对象时会发出“未检查”警告,因为原始类型没有提供足够的类型信息来执行所有必要的类型检查。

Example (of unchecked warning in conjunction with raw types):

示例(未经检查的警告与原始类型结合):

TreeSet set = new TreeSet(); 
set.add("abc");        // unchecked warning 
set.remove("abc");
warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.TreeSet 
               set.add("abc");  
                      ^

When the add method is invoked the compiler does not know whether it is safe to add a String object to the collection. If the TreeSet is a collection that contains String s (or a supertype thereof), then it would be safe. But from the type information provided by the raw type TreeSet the compiler cannot tell. Hence the call is potentially unsafe and an "unchecked" warning is issued.

当调用 add 方法时,编译器不知道将 String 对象添加到集合中是否安全。如果 TreeSet 是一个包含 String (或其超类型)的集合,那么它将是安全的。但是编译器无法从原始类型 TreeSet 提供的类型信息中分辨出来。因此,该调用可能不安全,并发出“未检查”警告。

"unchecked" warnings are also reported when the compiler finds a cast whose target type is either a parameterized type or a type parameter.

当编译器发现目标类型为参数化类型或类型参数的强制转换时,也会报告“unchecked”警告。

Example (of an unchecked warning in conjunction with a cast to a parameterized type or type variable):

示例(未经检查的警告以及对参数化类型或类型变量的强制转换):

  class Wrapper<T> { 
  private T wrapped ; 
  public Wrapper (T arg) {wrapped = arg;} 
  ... 
  public Wrapper <T> clone() { 
    Wrapper<T> clon = null; 
     try {  
       clon = (Wrapper<T>) super.clone(); // unchecked warning 
     } catch (CloneNotSupportedException e) {  
       throw new InternalError();  
     } 
     try {  
       Class<?> clzz = this.wrapped.getClass(); 
       Method   meth = clzz.getMethod("clone", new Class[0]); 
       Object   dupl = meth.invoke(this.wrapped, new Object[0]); 
       clon.wrapped = (T) dupl; // unchecked warning 
     } catch (Exception e) {} 
     return clon; 
  } 
} 
warning: [unchecked] unchecked cast 
found   : java.lang.Object 
required: Wrapper <T> 
                  clon = ( Wrapper <T>)super.clone();  
                                                ^ 
warning: [unchecked] unchecked cast 
found   : java.lang.Object 
required: T 
                  clon. wrapped = (T)dupl;

A cast whose target type is either a (concrete or bounded wildcard) parameterized type or a type parameter is unsafe, if a dynamic type check at runtime is involved. At runtime, only the type erasure is available, not the exact static type that is visible in the source code. As a result, the runtime part of the cast is performed based on the type erasure, not on the exact static type.

如果涉及运行时的动态类型检查,其目标类型是(具体或有界通配符)参数化类型或类型参数的强制转换是不安全的。在运行时,只有类型擦除可用,而不是源代码中可见的确切静态类型。因此,强制转换的运行时部分是基于类型擦除而不是确切的静态类型来执行的。

In the example, the cast to Wrapper would check whether the object returned from super.clone is a Wrapper , not whether it is a wrapper with a particular type of members. Similarly, the casts to the type parameter T are cast to type Object at runtime, and probably optimized away altogether. Due to type erasure, the runtime system is unable to perform more useful type checks at runtime.

在示例中,转换到 Wrapper 将检查从 super.clone 返回的对象是否是 Wrapper ,而不是它是否是具有特定类型成员的包装器。类似地,对类型参数 T 的强制转换在运行时被强制转换为 Object 类型,并且可能完全被优化掉。由于类型擦除,运行时系统无法在运行时执行更有用的类型检查。

In a way, the source code is misleading, because it suggests that a cast to the respective target type is performed, while in fact the dynamic part of the cast only checks against the type erasure of the target type. The "unchecked" warning is issued to draw the programmer's attention to this mismatch between the static and dynamic aspect of the cast.

在某种程度上,源代码具有误导性,因为它表明对相应目标类型执行了强制转换,而实际上,强制转换的动态部分仅检查目标类型的类型擦除。发出“unchecked”警告是为了提请程序员注意演员表的静态和动态方面之间的这种不匹配。

Please refer: What is an "unchecked" warning?

请参考:什么是“未检查”警告?