问题描述
我正在尝试使用 Scala 为 Sonar 构建扩展.我需要扩展以下Java接口:
I'm trying to build an extension for Sonar, using Scala. I need to extend the following Java interface:
public interface Decorator extends BatchExtension, CheckProject {
void decorate(Resource resource, DecoratorContext context);
}
但 Resource 类型实际上是这样定义的:
but Resource type is actually defined like:
public abstract class Resource<PARENT extends Resource>
我知道我可以解决创建 Java 原始超类的问题.我想坚持仅使用 Scala,也知道是否有我遗漏的解决方案,以及我是否可以建议 SonarSource 人员在他们这边做出改进(使用原始类型).
I know I can workaround creating a Java raw super-class. I'd like to stick to Scala-only, also know if there's a solution I'm missing, and whether there's an improvement I could suggest to SonarSource people to make on their side (on using raw types).
我已经阅读了这方面的问题,以及某些情况下的一些解决方法,但似乎没有一个适用于这里(一种解决方法,一张明显固定的票,还有一张 2091 的票……)
I've read there were issues with this, and some workarounds for some cases, but none seemed to apply here (a workaround, an apparently fixed ticket, also there's ticket 2091...)
推荐答案
经过反复试验并查看错误消息,我想出了这个编译:
After some trial and error and looking at the error messages, I came up with this which compiles:
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
就像 Directory代码>.
I'm not sure whether it will allow you to implement what you need. I looked at Resource, and it could represent File
which extends Resource<Directory>
or sometimes be an erased (raw?) type that just extends Resource
like for Directory
.
再想一想,forSome
可以消除 - 这也可以编译:
thinking about it some more, the forSome
can be eliminated - this compiles too:
def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]],
context: DecoratorContext) {
}
这篇关于使用 Scala 的 Raw 类型实现 Java 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!