1

The next code

public class FilterState<T>{
   Map<String, Object> getProperties(){return null;} // null for shorteness
}

....
public test(FilterState filterState){
    Map<String, Object> map = filterState.getProperties();
}

produces warning

Unchecked assignment: 'java.util.Map' to 'java.util.Map<java.lang.String, java.lang.Object>

However if I replace procedure definition with

public <T> test(IFilterState<T> filterState)

warning disappears. I have no clue what class generic has to do with map assignment. Quick search through generic documentation didn't help. Any idea where to look, anyone? :)

Observer
  • 710
  • 10
  • 14

4 Answers4

5

If you abandon the generic type argument, the whole class is treated as using raw types, and all generic type information from that class is ignored. That's what's happening here.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thanks! May I ask for prooflink before I press "Accept" button? – Observer Jul 06 '12 at 13:36
  • 1
    http://stackoverflow.com/questions/5436656/why-does-the-java-compiler-complain-on-using-foreach-with-a-raw-type should do. In any event, you've just pretty much conclusively demonstrated this for yourself. – Louis Wasserman Jul 06 '12 at 13:43
1

I can remember that reification fails when you're using raw type, so it compiler is free to complain about properties/methods which are generic as well. But I may be wrong.

jdevelop
  • 12,176
  • 10
  • 56
  • 112
0

I'm not sure exactly what the problem is, but one place you might want to look is section 4.8 of the specification, Raw Types.

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types. To make sure that potential violations of the typing rules are always flagged, some accesses to members of a raw type will result in compile-time unchecked warnings.

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Just copy first code quote and take a look on warning. After that replace method definition with second and see that now there is no warning. The question is: why?? I did nothing with getProperties(). – Observer Jul 06 '12 at 12:48
-1

As per Class Generics the Type defined will be part of the class behavior, so here the method return type(Map) Generic has String. I think that's why we get this warning !!! Since you added the in later part it means that you are forced to use so the warning is not there...

You can suppress warnings using Annotation @suppresswarning

Antimony
  • 37,781
  • 10
  • 100
  • 107