Search code examples
javajavacsuppress-warnings

Why is my @SuppressWarnings("unchecked") ignored?


Why is Javac ignoring my @SuppressWarnings annotation in the code below?

@Override
@Transactional(readOnly = true, timeout = 10)
public List<Item> findAllDue() {
    Query query = getSession().createQuery("from Item item where item.status = :status");
    @SuppressWarnings("unchecked")
    List<Item> list = query.setString("status", ItemState.MY_STRING_STATUS).list(); //This is line 43.  yes, I know it can be an Enum instead
    return list;
}

After compiling with Xlint, here is the warning:

Warning:(43, 106) java: unchecked cast
  required: java.util.List<com.company.domain.Item>
  found:    java.util.List

The code builds successfully with this warning. It probably doesn't matter, but this is Spring 4 and Hibernate 4.

Update, based on comments:

I attempted to edit my example for a general case rather than providing company specific code. For those quick to downvote, as supporting evidence: here is the actual output from my editor and the actual code, using Java 7 with the company name redacted.

editor image

Below is the terminal output, without using an editor, but javac with ant:

terminal output


Solution

  • Update:

    This is a javac bug that affects the versions: 6u51, 7u21, 8. That was reported and fixed here:JDK-8016099.

    Other related reports are: JDK-8022144 and JDK-8016636 but they are all marked as duplicate to the first one.

    That was fixed in 1.8.0u65.