2

I get "Unchecked assignment: java.util.List to java.util.List<java.lang.Integer>" at the line with the comment. The warning is cleared when the GetList parameter is changed to GetList<?>.

I don't understand why the GetList generic type would influence the outcome of getIntegerList(). Tried in both Intellij Idea and Eclipse. Is it normal or is the warning message wrong?

import java.util.ArrayList;
import java.util.List;

public class GetList<T> {
    private final List<Integer> integerList= new ArrayList<Integer>();

    public List<Integer> getIntegerList() {
        return integerList;
    }
}


import java.util.List;

public class Main {
    public Main(GetList getList) {
        List<Integer> rahan = getList.getIntegerList(); //this line shows the warning above
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vlad Topala
  • 896
  • 1
  • 8
  • 34
  • 2
    Using rawtypes always cause that warning. Btw do you really need the `` parameter in your `GetList` class? – Francesco Pitzalis Mar 15 '16 at 14:07
  • It has nothing to do with Java. I tried your code in Intellij and it did not show any warning. It must be your IDE who is misinterpreting – Radu Ionescu Mar 15 '16 at 14:07
  • 2
    @RaduIonescu or maybe it's your IDE, because I can reproduce using `javac -Xlint:warning`. – Andy Turner Mar 15 '16 at 14:09
  • 1
    @FrancescoPitzalis this is just a SSCCE for understanding the issue. The code I found this in does use the generic. – Vlad Topala Mar 15 '16 at 14:11
  • 2
    I can't find an obvious reason for this warning in JLS; however, as it says in [Section 4.8](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.8) "The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged"; so it is happy (?) coincidence that fixing one warning (seen via `-Xlint:rawtypes`) also fixes the one at hand. – Andy Turner Mar 15 '16 at 14:25
  • 1
    @AndyTurner the relevant paragraph is "The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C." So `GetList.getIntegerList()` returns `List`, and `GetList.getIntegerList()` returns `List`. – Roman Mar 15 '16 at 15:16
  • @Roman are you sure? I saw that, and actually tried defining the method in a non-raw base class, and still got the warning (let me try again...) – Andy Turner Mar 15 '16 at 15:18
  • @Roman of course, you are right - I was defining the method in the base class, but then overriding it in the `GetList` class again. Thanks for pointing it out! – Andy Turner Mar 15 '16 at 15:20
  • @AndyTurner keep in mind, if base class is also generic, the previous paragraph applies: "The superclasses (respectively, superinterfaces) of a raw type are the erasures of the superclasses (superinterfaces) of any of the parameterizations of the generic type." – Roman Mar 15 '16 at 15:22
  • Possible duplicate of [Java generic methods in generics classes](http://stackoverflow.com/questions/18001550/java-generic-methods-in-generics-classes) – dimo414 Apr 19 '16 at 00:40

1 Answers1

0

If you use raw types you should expect any number of strange warnings, and those warnings are not necessarily going to be consistent between compilers (you're entering "undefined behavior" territory).

As Andy Turner and Roman note in the comments this is spelled out in JLS §4.8 (emphasis mine):

The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.

In other words the compiler is allowed to discard all generic information about a class when it's being used as a raw type.

Here's another example (with a slightly clearer error message):

$ cat Demo.java
import java.util.*;

class Demo<T> {
  public List<Integer> ls() { return new ArrayList<>(); }

  public static void main(String[] args) {
    List<Integer> ls = new Demo().ls();
  }
}

$ javac -Xlint:unchecked Demo.java 
Demo.java:7: warning: [unchecked] unchecked conversion
    List<Integer> ls = new Demo().ls();
                                    ^
  required: List<Integer>
  found:    List
1 warning
dimo414
  • 47,227
  • 18
  • 148
  • 244