从 Scala 实现具有 Raw 类型的 Java 接口



我正在尝试使用Scala为Sonar构建一个扩展。我需要扩展以下 Java 接口:

public interface Decorator extends BatchExtension, CheckProject {
    void decorate(Resource resource, DecoratorContext context);
}

资源类型实际上是这样定义的:

public abstract class Resource<PARENT extends Resource>

我知道我可以解决创建 Java 原始超类的方法。我想坚持只使用 Scala,也知道我是否缺少解决方案,以及我是否可以建议 SonarSource 人员进行改进(使用原始类型)。

我读过这有问题,以及某些情况下的一些解决方法,但似乎没有一个适用于这里(解决方法,显然是固定的票证,还有票证 2091......

经过一些试验和错误并查看错误消息,我想出了这个编译:

import org.sonar.api.batch._
import org.sonar.api.resources._ 
object D {
  type R = Resource[T] forSome {type T <: Resource[_ <: AnyRef]}
  type S[T] = Resource[T] forSome {type T <: Resource[_ <: AnyRef]}
}
class D extends Decorator {
  def decorate(r: D.R, context: DecoratorContext) {}
  //def decorate(r: D.S[_], context: DecoratorContext) {} // compiles too
  def shouldExecuteOnProject(project: Project) = true
}

我不确定它是否会让您实现所需的内容。我查看了资源,它可以表示File扩展Resource<Directory>,或者有时是擦除(原始Directory Resource

编辑:再考虑一下,可以消除forSome - 这也编译:

def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]],
             context: DecoratorContext) {
}
我不知道

答案,但如果我写

def decorate(r: Resource[Resource[_]])

我收到错误

type arguments [Resource[_]] do not conform to class Resource's type parameter bounds [PARENT <: Resource[_ <: AnyRef]]

这对我来说似乎是错误的,因为我认为实际的类型边界应该更像Resource[_ <: Resource[_ <: Resource[... ...]]AnyRef不适合作为上限)。

相关内容

最新更新