Java 8 Incompatible Types -


here's simple code

import java.util.arraylist; import java.util.collections; import java.util.hashmap; import java.util.map;  public class simpletest {      public static void main(string[] args) {     final arraylist<map<string, object>> maps = newarraylist(         createmap("1", "a", collections.empty_map, collections.empty_map),         createmap("2", "b", collections.empty_map, collections.empty_map),         createmap("3", "c", collections.empty_map, collections.empty_map)     );       system.out.println(" maps = " + maps);     }      public static map<string, object> createmap(string value1, string value2, map<string, object> object1, map<string, object> object2) {        map<string, object> map = new hashmap<>();        map.put("value1", value1);        map.put("value1", value1);        map.put("object1", object1);        map.put("object2", object2);        return map;     }          public static <e> arraylist<e> newarraylist(e... elements) {     arraylist<e> list = new arraylist<e>(elements.length);     collections.addall(list, elements);     return list;     } } 

when java_home points jdk 8 , use javac -source 1.7 simpletest.java

simpletest.java:9: error: incompatible types: arraylist<map> cannot converted arraylist<map<string,object>>         final arraylist<map<string, object>> maps = newarraylist(                                                                 ^ 

when use -source 1.8 or no -source option works ok. now, when java_home points jdk 7 code compiles whether use -source 1.7 or not.

initially question piece of software pom file has <source> , <target> set 1.7 , build failing on jdk 8 ok on jdk 7.

now question - causes happen ? seems me major overlook of sort. why compiling on jdk 8 source set 1.7 fails ?

you can simplify code self-contained example doesn’t need 3rd-party libraries:

public class test2 {     @safevarargs     static <t> arraylist<t> newarraylist(t... arg) {         return new arraylist<t>(arrays.aslist(arg));     }     private final list<map<string, object>> maps = newarraylist(         createmap(null, collections.empty_map)      );      public static map<string, object> createmap(string id, map<string,string> m) {         return null;     } } 

this can compiled using javac jdk1.7, not javac jdk1.8 using -source 1.7.

the point here javac jdk1.8 still different compiler 1 included in previous version , option -source 1.7 doesn’t tell mimic old implementation’s behavior compatible java 7 specification. if old compiler has bug, newer compiler doesn’t have try reproduce bug.

since code uses collections.empty_map rather collections.<string,string>emptymap(), raw type map passed createmap, making unchecked invocation having raw result type map.

this mandated jls §15.12.2.6:

the result type of chosen method determined follows:

  • if chosen method declared return type of void, result void.

  • otherwise, if unchecked conversion necessary method applicable, result type erasure (§4.6) of method's declared return type.

it seems behavior has not been (fully) implemented in javac of jdk1.7. interestingly, correctly when adding type parameter like

public static <t> map<string, object> createmap(string id, map<string,string> m) 

making generic method. then, jdk1.7 produce same error. cited rule applies all method invocations.


in contrast, fact compiling java 8 compliance succeeds stems new target type inference, has entirely different rules.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -