Warning: [unchecked] unchecked conversion

I ran into a problem with generics. Can someone please tell me what I missed in the statements below?

1.

warning: [unchecked] unchecked conversion List<Question> qList = (List) session.getAttribute("qList"); ^ required: List<Question> found: List 

2.

 warning: [unchecked] unchecked conversion List<ExamSchedule> eList = new <ExamSchedule>ArrayList(); required: List<ExamSchedule> found: ArrayList 

I do not want to suppress warnings. Any suggestions would be appreciated.

+5
source share
4 answers
 List<Question> qList = (List) session.getAttribute("qList"); 
  • session.getAttribute("qList"); will return an instance of type Object . Therefore, you need to explicitly quit it.

  • (List) is just a raw type, List<String> is a generic type, so trying to pass the original type to a generic link type gives a warning.

Now if you do this:

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

A cast is a check of the runtime, but the type will be erased at runtime, so there really is no difference between List<String> and List<Foo> , etc. If you get this error. So try (List<?> list) conversion of this type checks that the object is a List , without worrying about the types inside.

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

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

Suggestions:

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

Answer 1:

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

Answer 2:

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

First understand the idea of generalization .

As for the first answer, if you use HttpSession , there is no way to reassure the warnings without commenting on your @SuppressWarnings like this:

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

This is due to the Servlet API, which returns an Object from HttpSession.getAttribute() . Otherwise, the compiler will warn you about type safety (unchecked casts from Object to List<Question> ).

+4
source

Java does not have repeated generics; a list does not carry the type of its elements at runtime. Thus, attempting to throw a restricted type will give you a warning; you might think you know the type, but the compiler reminds you what might be wrong.

You can attribute it to an unrelated collection, and then check the type of each individual item.

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

If you need to pass the List<Question> to any API, you can copy the elements to a new, correctly declared list inside the loop. Obviously, this is a lot of a mess that you must consider in the utility method. But to make it flexible, you probably want to use dynamic types.

 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.Collections that do almost what you need; Unfortunately, they do not check the type of items in the original packaged collection. In addition, since they wrap the base collection, rather than creating a new independent collection, they can still generate type errors.

Fortunately, the second question is simple:

 List<ExamSchedule> eList = new ArrayList<>(); 
+3
source

This can help:

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

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

what happened in the code that I had

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

In this case, the right extract is not guaranteed to return the Product List

So I changed it by applying the following method

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

and got another compilation error! at eclipse this is a warning. state unverified cast

I am using Java 12 and still not sure why this is an error instead of warning

Decision:

Finally I have to use the Wilcard type

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

and add simplicity to list items like this

 for(Object obj: productList){ Product product = (Product)obj; // do something on product } 
0
source

Source: https://habr.com/ru/post/1480041/


All Articles