riddle_me_this
riddle_me_this

Reputation: 9145

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

Upvotes: 1

Views: 1205

Answers (2)

Tarik
Tarik

Reputation: 5021

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.

Upvotes: 1

riddle_me_this
riddle_me_this

Reputation: 9145

TL; DR: Declare the evaluated static String before the method as a workaround.

I am able to clear the warnings in a rather unorthodox way and it may be a bug - or some ridiculous "feature" :).

The examples that follow describe the behavior. Each one uses the @SuppressWarnings annotation.

If I use the evaluated String as in (a), no warning is shown. If I declare the String prior to the method call as in (b), no warning is shown.

Making the string a private member in the same class has no effect and still produces the compiler warning (b).

some examples

If we evaluate the constant first, it produces no warning, as in (d):

workaround

Upvotes: 0

Related Questions