简体   繁体   English

Scala类型与泛型不匹配

[英]Scala type mismatch with generics

I'm really new to Scala and I'm try to convert some JSF controller to Scala to do some test. 我真的是Scala的新手,我尝试将一些JSF控制器转换为Scala进行测试。 The code is the following: 代码如下:

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() = {

  }

}

The error on compile time is: 编译时错误为:

Error:(24, 34) type mismatch; 错误:(24,34)类型不匹配;

found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY] 找到了:org.primefaces.model.org.primefaces.model.LazyDataModel [TENTITY]

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

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

I search for a solution but the problem still remaign. 我正在寻找解决方案,但问题仍然存在。 I try to reproduce with simpler code like this: 我尝试使用以下更简单的代码进行重现:

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());
  }

}

But this will work. 但这会起作用。 Someone can help? 有人可以帮忙吗? Thanks 谢谢

I found the problem: 我发现了问题:

This is the head of LazyDataModel<T> : 这是LazyDataModel<T>的头:

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

The class inherit from DataModel<T> . 该类继承自DataModel<T>

In my pom.xml I have 在我的pom.xml中

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

and also 并且

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

The class DataModel<T> is in the jsf 2 packages but also in javaee-api there is DataModel class (without generic). DataModel<T>类位于jsf 2软件包中,但在javaee-api中也有DataModel类(无通用)。 So the compiler go wrong. 因此编译器出错。 The resolution is to modify the pom with the following dependencies: 解决方案是使用以下依赖项来修改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>

I can't use jee6 api dependencies cause I'm using jee5 compatible app server with JSF2.0 upgrade. 我无法使用jee6 api依赖项,因为我正在将Jee5兼容的应用服务器与JSF2.0升级一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM