How to remove SuppressWarnings ("unchecked")

Is there any way I can change this code so that I can remove the unverified warning

ArrayList<String> keys = new ArrayList<String>();
// do some stuff then
// save keys in session
HttpServletRequest request = getThreadLocalRequest();
HttpSession session = request.getSession(true);
session.setAttribute(uuid, keys);

// get keys from session sometime later
@SuppressWarnings("unchecked")
ArrayList<String> keys = (ArrayList<String>) session.getAttribute(uuid);
+3
source share
2 answers

You can not. session.getAttribute()unsafe.

You can create a wrapper method, so you only have @SuppressWarningsone place:

public final class SessionUtils {
    @SuppressWarnings("unchecked")
    public static <T> T getSessionAttribute(HttpSession session, String name) {
        return (T) session.getAttribute(name);
    }
}

Then you can use without warning:

List<String> keys = SessionUtils.getAttribute(session, uuid);
+7
source

Well, you need to choose between:

  • Warning for source type
  • Unverified Conversion Warning
  • Annotation to remove a warning for the source type
  • Annotation to remove a warning for an unverified conversion

, getAttribute() Object, , Object ArrayList<String>, , , Object a ArrayList<String>.

ArrayList, List - ( , ), .

+2

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


All Articles