java lambda expression with multiple generic parameters not compiled -


i'm coding excel import logic:

public class excelimporter {     //...ignore constructor , other methods ...     public <r> builder<r> sheet(string sheetname, rowconsumer<r> rowconsumer) {         return new builder<r>(sheetname, rowconsumer);     }     public class builder<r> {         //...ignore other method         public <f> builder header(string name, cellconsumer<r, f> cellconsumer) {             sheetreader.header(new defaultheader<>(name, cellconsumer));             return this;         }         public <f> builder header(string name, class<f> fieldtype, cellconsumer<r, string> cellconsumer) {             return header(name,cellconsumer);         }     } } 

and on test code got compile error:

@test public void processsmallexcelwithconsumer() throws exception {     try (inputstream = getclass().getclassloader().getresourceasstream("工作簿1.xls")) {         excelimporter excelimporter = new excelimporter(is, "application/vnd.ms-excel")                 .sheet("sheet1", () -> new rowbean())                 .header("姓名",string.class, (cell, row) -> row.setname(cell)) // no error                 .header("性别",string.class, (cell, row) -> row.setsex(cell)) // "setsex" got error , `row` evaluate `object` ? why !?                 .build(); 

setsex() can not compile,and row evaluate object, i'm confusion works @ first time fails next time?

this cellconsumer:

@functionalinterface public interface cellconsumer<r,f> {     void read(f cell,r row); } 

and rowconsumer:

@functionalinterface public interface rowconsumer<r> {     r newrow(); } 

and error:

[info] build failure [info] ------------------------------------------------------------------------ [info] total time: 1.797 s [info] finished at: 2017-07-28t23:56:26+08:00 [info] final memory: 17m/166m [info] ------------------------------------------------------------------------ [error] failed execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:testcompile (default-testcompile) on project graceful-excel: compilation failure [error] /home/terrason/workspace/maven/cnx/graceful-excel/src/test/java/cn/lenyar/excel/excelimportertest.java:[81,66] 找不到符号 [error] 符号:   方法 setsex(java.lang.object) [error] 位置: 类型为java.lang.object的变量 row [error] -> [help 1] 

help me please!

the problem aren't specifying generic type of returned builder in header methods:

public <f> builder header(...) //this returns builder same builder<?> //all java can infer builder<?> generic type object. //which makes row in second call object 

try returning builder<r> in header methods:

public class excelimporter {   //...ignore constructor , other methods ...   public <r> builder<r> sheet(string sheetname, rowconsumer<r> rowconsumer) {     return new builder<r>(sheetname, rowconsumer);   }    private static class rowbean {     private string name;     private string sex;      public void setname(string name) {       this.name = name;     }      public void setsex(string sex) {       this.sex = sex;     }   }    public class builder<r> {     public builder(string sheetname, rowconsumer<r> rowconsumer) {      }      //...ignore other method     public <f> builder<r> header(string name, cellconsumer<r, f> cellconsumer) {       return this;     }      public <f> builder<r> header(string name, class<f> fieldtype, cellconsumer<r, string> cellconsumer) {       return header(name, cellconsumer);     }      public excelimporter build() {       return null;     }   }    @functionalinterface   public interface cellconsumer<r, f> {     void read(f cell, r row);   }    @functionalinterface   public interface rowconsumer<r> {     r newrow();   }    public static void main(string[] args) {     excelimporter excelimporter = new excelimporter()         .sheet("sheet1", () -> new rowbean())         .header("姓名", string.class, (cell, row) -> row.setname(cell)) // no error         .header("性别", string.class, (cell, row) -> row.setsex(cell)) // no error!         .build();   } } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -