6

我正在尝试使用 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 ......)

4

2 回答 2

3

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

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
}

我不确定它是否能让你实现你所需要的。我查看了Resource,它可以表示File哪个扩展Resource<Directory>,或者有时是一个擦除(原始?)类型,Resource就像 for一样扩展Directory

编辑:再想一想,forSome可以消除-这也可以编译:

def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]],
             context: DecoratorContext) {
}
于 2012-06-04T14:51:14.320 回答
0

我不知道答案,但如果我写

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不适合作为上限)。

于 2012-06-04T03:13:28.313 回答