java 警告:[unchecked] 未经检查的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16475436/
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-10-31 23:04:31  来源:igfitidea点击:

Warning: [unchecked] unchecked conversion

java

提问by Sharad Tank

I am facing an issue with generics here. Can anyone please point me what I am missing over in the below statements?

我在这里面临泛型问题。任何人都可以指出我在以下陈述中遗漏了什么吗?

1.

1.

warning: [unchecked] unchecked conversion

List<Question> qList = (List) session.getAttribute("qList");
                                       ^
  required: List<Question>
  found:    List

2.

2.

warning: [unchecked] unchecked conversion

List<ExamSchedule> eList = new <ExamSchedule>ArrayList();

required: List<ExamSchedule>
found:    ArrayList

I don't want to supress the warnings. Any suggestions will be appreciated.

我不想压制警告。任何建议将不胜感激。

回答by NINCOMPOOP

List<Question> qList = (List) session.getAttribute("qList");
  1. session.getAttribute("qList");will return instance of type Object. So you need to explicitly cast it.

  2. (List)is just raw type, List<String>is generic type , so trying to cast raw type to generic type reference gives a warning.

  1. session.getAttribute("qList");将返回类型的实例Object。所以你需要明确地投射它。

  2. (List)只是原始类型,List<String>是泛型类型,因此尝试将原始类型转换为泛型类型引用会给出警告。

Now, if you do this:

现在,如果你这样做:

List<Question> qList = (List<Question>) session.getAttribute("qList");

List<Question> qList = (List<Question>) session.getAttribute("qList");

The cast is a runtime check but there will be a type erasure at runtime, so there's actually no difference between a List<String>and List<Foo>etc.Hence you get that error. So try (List<?> list)This type conversion verifies that the object is a Listwithout caring about the types held within.

强制转换是运行时检查,但在运行时会进行类型擦除,因此 aList<String>List<Foo>etc之间实际上没有区别。因此您会收到该错误。所以尝试(List<?> list)这种类型转换验证对象是 aList而不关心其中包含的类型。

List<ExamSchedule> eList = new <ExamSchedule>ArrayList();

That is a syntax error.It should be ArrayList<ExamSchedule>, not <ExamSchedule>ArrayList.

那是语法错误。应该是ArrayList<ExamSchedule>,不是<ExamSchedule>ArrayList

Suggestions :

建议:

List<?> qList = (List<?>) session.getAttribute("qList");
List<ExamSchedule> eList = new ArrayList<ExamSchedule>();

回答by Buhake Sindi

Answer 1:

答案 1:

List<Question> qList = (List<Question>) session.getAttribute("qList");

Answer 2:

答案 2:

List<ExamSchedule> eList = new ArrayList<ExamSchedule>();

Grasp first the idea of Generics.

首先掌握泛型的概念。

As for the first answer, if you're using HttpSession, there is no chance of calming the warnings without annotating your statement with @SuppressWarningslike so:

至于第一个答案,如果您正在使用HttpSession,则没有机会在没有@SuppressWarnings像这样注释您的语句的情况下平息警告:

@SuppressWarnings("unchecked")
List<Question> qList = (List<Question>) session.getAttribute("qList");

This is due to the Servlet API which returns an Objectfrom HttpSession.getAttribute(). The compiler will warn you of type-safety (unchecked cast from Objectto List<Question>) otherwise.

这是由于 Servlet API 返回一个Objectfrom HttpSession.getAttribute()。否则编译器会警告您类型安全(未经检查的从Objectto强制转换List<Question>)。

回答by erickson

Java doesn't have reified generics; the list doesn't carry its elements' type at runtime. So, an attempt to cast to a bounded type will give you a warning; you might think you know the type, but the compiler reminds you that might be wrong.

Java 没有具体化的泛型;该列表在运行时不携带其元素的类型。因此,尝试强制转换为有界类型会给您一个警告;您可能认为您知道类型,但编译器会提醒您这可能是错误的。

You cancast to an unbound collection, then check the type of each individual element.

可以转换为未绑定的集合,然后检查每个单独元素的类型。

List<?> tmp = (List<?>) session.getAttribute("qList");
if (tmp != null) {
  for (Object o : tmp) {
    Question q = (Question) o;
    /* Use q ... */
  }
}

If you need a List<Question>to pass to some API, you can copy the elements to a new, correctly declared list inside the loop. Obviously, this is a lot of clutter that you should factor into a utility method. But to make it flexible, you'd probably want to use dynamic type casts.

如果您需要将 aList<Question>传递给某个 API,您可以将元素复制到循环内一个新的、正确声明的列表中。显然,这是您应该将其考虑到实用方法中的大量混乱。但是为了使其灵活,您可能需要使用动态类型转换。

public static <T> List<T> asList(Collection<?> c, Class<? extends T> type) {
  if (c == null)
    return null;
  List<T> list = new ArrayList(c.size());
  for (Object o : c)
    list.add(type.cast(o));
  return list;
}

List<Question> qList = asList((Collection<?>) session.getAttribute("qList"), Question.class);

There are methods in java.util.Collectionsthat do almostwhat you need; unfortunately, they don't check the type of elements in the original wrapped collection. Also, since they wrap the underlying collection instead of creating a new, independent collection, they could still create type errors.

有方法java.util.Collections是做几乎你所需要的; 不幸的是,他们不检查原始包装集合中元素的类型。此外,由于它们包装了底层集合而不是创建一个新的独立集合,因此它们仍然可能会产生类型错误。

Luckily, the second question is easy:

幸运的是,第二个问题很简单:

List<ExamSchedule> eList = new ArrayList<>();

回答by Tadele Ayelegn

This could help:-

这可能会有所帮助:-

/Users/tadtab/src/main/java/com/tadtab/dao/ProductDAO.java:[73,66] [unchecked] unchecked conversion

/Users/tadtab/src/main/java/com/tadtab/dao/ProductDAO.java:[73,66] [unchecked] 未经检查的转换

[ERROR]   required: List<Product>
[ERROR]   found:    List

what happen was in the code I had

发生了什么在我的代码中

List<Product> productList = session.createQuery("from Product").list());

In this case the right hand statement is not guaranteed that it will return List Products

在这种情况下,不能保证右手语句会返回列表产品

So I modified it by casting in the following way

所以我通过以下方式进行了修改

 List<Product> productList = (List<Product>)session.createQuery("from Product").list());

and got another Got compilation error! on eclipse it is a warning. stating unchecked cast

并得到另一个编译错误!在日食时,这是一个警告。说明未经检查的演员表

I am using java 12 and still not sure why it is an error instead of warning

我正在使用 java 12,但仍然不确定为什么它是错误而不是警告

Solution:

解决方案:

Finally I have to use Wilcard type

最后我必须使用通配符类型

List<?> productList = session.createQuery("from Product").list());

and add explicity casting on the elements of the List like this

并像这样在 List 的元素上添加显式转换

for(Object obj: productList){

    Product product = (Product)obj;
  // do something on product
}