1

I cannot remove my warnings with unchecked cast. I believe this is very strange because I have added the @SuppressWarnings("unchecked") annotation on the method but the Javac still showing the warnings.

[unchecked] unchecked cast (List<Integer>) getObject(LIST);
    return (List<Integer>) getObject(LIST);
    required: List<Integer>
    found:    Object

I have create an Example, It throws 2 warnings. I'm using Oracle JDK 1.7 update 79.

package strange;
import java.util.List;

public class Strange implements Constants {

    //Throw Warning
    @SuppressWarnings("unchecked")
    protected List<Integer> getList() {
        return (List<Integer>) getObject(LIST);
    }

    @SuppressWarnings("unchecked")
    protected List<Integer> getList2() {
        return (List<Integer>) getObject(LIST);
    }

    //Throw Warning
    @SuppressWarnings("unchecked")
    protected List<Integer> getOtherList() {
        return (List<Integer>) getObject(OTHER);
    }

    protected Object getObject(String key) {
        return new Object();
    }
}

interface Constants {

    public static final String LIST = "list";
    public static final String OTHER = "other";
}

Thanks.

rvillablanca
  • 1,355
  • 3
  • 15
  • 32

1 Answers1

1

This looks like a javac bug. It doesn't reproduce in 1.8.0u45 though.

Did a quick search and it could be related to for instance JDK-8016099 or JDK-8022144. One could do a hg bisect to find the exact changeset that resolved the issue.

Possibly related:

Community
  • 1
  • 1
aioobe
  • 383,660
  • 99
  • 774
  • 796