1

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

Tarik
  • 4,797
  • 3
  • 34
  • 67
vphilipnyc
  • 7,433
  • 7
  • 51
  • 75
  • 8
    This may be an obvious question, but are you sure that warning is on the same line as the suppression? Could it be complaining about `return list;`? After all, `List` isn't covariant with `List`. – Andy Turner Dec 21 '15 at 23:35
  • What version of Java? – bmargulies Dec 22 '15 at 00:08
  • @AndyTurner - that was a typo. Updated question with the actual return type. Can someone explain the downvote? – vphilipnyc Dec 22 '15 at 00:37
  • @SotiriosDelimanolis please take a look at my answer. – vphilipnyc Dec 22 '15 at 02:31
  • @bphilipnyc could you check tha Java version you are using to compile? you can use : `javap -verbose MyClass | findstr "major"` on the command line as explained here: http://stackoverflow.com/questions/1096148/how-to-check-the-jdk-version-used-to-compile-a-class-file – Tarik Dec 22 '15 at 08:55
  • `javap -verbose LeaseDaoImpl.class | grep major` yields `major version: 51` – vphilipnyc Dec 22 '15 at 14:16
  • @bphilipnyc does any answer helped you? – Tarik Jan 06 '16 at 20:27

2 Answers2

1

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.

vphilipnyc
  • 7,433
  • 7
  • 51
  • 75
Tarik
  • 4,797
  • 3
  • 34
  • 67
0

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

vphilipnyc
  • 7,433
  • 7
  • 51
  • 75