2015-02-09 193 views
2

我對Scala非常陌生,我試圖將一些JSF控制器轉換爲Scala來做一些測試。 代碼如下:在編譯時Scala類型與泛型不匹配

abstract class BaseListBean[TENTITY <: AnyRef, TID <: AnyRef] extends Serializable { 

    @BeanProperty var current:TENTITY = _ 

    @BeanProperty var lazyDataModel: LazyDataModel[TENTITY] = _ 

    def initLazyDataModel(): LazyDataModel[TENTITY] = { 

    val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() { 

     override def load(first: Int, pageSize: Int, sortField: String, sortOrder: SortOrder, filters: util.Map[String, AnyRef]): util.List[TENTITY] = { 

     val paginationInfo:PaginationInfo = new PaginationInfo() 
     paginationInfo.setFromRecord(first) 
     paginationInfo.setPageSize(pageSize) 
     paginationInfo.setSortBy(Array(sortField)) 
     paginationInfo.setDirections(if (SortOrder.ASCENDING == sortOrder) Array(Direction.Asc) else Array(Direction.Desc)) 

     val advisoryPaginatedResult:PaginatedResult[TENTITY] = getBaseCRUD.read(paginationInfo) 
     setRowCount(advisoryPaginatedResult.getTotalItems.toInt) 

     advisoryPaginatedResult getItems 
     } 

     override def getRowKey(obj: TENTITY): TID = getEntityKey(obj) 

    } 

    return model; 

    } 



    def init() = { 
    setLazyDataModel(initLazyDataModel()) 

    } 

    def getEntityKey(obj: TENTITY) : TID 
    def getBaseCRUD() : BaseCRUD[TENTITY, TID] 
    def customInit() = { 

    } 

} 

的錯誤是:

Error:(24, 34) type mismatch;

found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]

required: org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]

val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() { 

我尋找一個解決方案,但問題仍然remaign。我嘗試用更簡單的代碼重現像這樣:

abstract class ExampleGenerics[T <: AnyRef, V <: AnyRef] { 

    @BeanProperty var list:util.AbstractMap[T, V] = _ 

    def initList() : util.AbstractMap[T, V] = { 
    val model:util.AbstractMap[T, V] = new util.AbstractMap[T, V]() { 
     override def entrySet(): util.Set[Entry[T, V]] = { 
     return null; 
     } 
    } 
    return model 
    } 

    def init() = { 
    setList(initList()); 
    } 

} 

但是,這將工作。有人可以幫忙嗎?由於

回答

0

我發現這個問題:

這是LazyDataModel<T>頭:

public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { /*...*/ } 

類繼承DataModel<T>

在我的pom.xml我有

<dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>javax.faces-api</artifactId> 
     <version>2.0</version> 
     <scope>provided</scope> 
    </dependency> 

<dependency> 
    <groupId>javaee</groupId> 
    <artifactId>javaee-api</artifactId> 
    <version>5</version> 
    <scope>provided</scope> 
</dependency> 

DataModel<T>是在JSF 2包,而且在JavaEE的-API有DataModel類(不通用) 。所以編譯器出錯了。 決議符合以下依存修改POM:

<dependency> 
    <groupId>javax.ejb</groupId> 
    <artifactId>ejb-api</artifactId> 
    <version>3.0</version> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>2.5</version> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>javax.faces</groupId> 
    <artifactId>javax.faces-api</artifactId> 
    <version>2.0</version> 
    <scope>provided</scope> 
</dependency> 

因爲我使用JEE5兼容應用服務器與JSF2.0升級我不能使用JEE6 API的依賴。