从Scala实现具有Raw类型的Java接口
我正在尝试使用Scala为Sonar构建扩展。 我需要扩展以下Java接口:
public interface Decorator extends BatchExtension, CheckProject { void decorate(Resource resource, DecoratorContext context); }
但资源类型实际上定义如下:
public abstract class Resource
我知道我可以解决创建Java原始超类的问题。 我想坚持Scala-only,也知道我是否缺少一个解决方案,以及是否有一个改进我可以向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: DR, context: DecoratorContext) {} //def decorate(r: DS[_], context: DecoratorContext) {} // compiles too def shouldExecuteOnProject(project: Project) = true }
我不确定它是否会允许你实现你需要的东西。 我查看了Resource ,它可以代表扩展Resource
File
,或者有时候是一个擦除(raw?)类型,它只是为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
不适合作为上限) 。