本文介绍了使用Scala的Raw类型实现Java接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Scala为Sonar创建扩展。
我需要扩展以下Java接口:
public interface Decorator extends BatchExtension,CheckProject {
void装饰(资源资源,DecoratorContext上下文);
}
但是 Resource 类型实际上是这样定义的:
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< :资源[_ 类型S [T] =资源[T] forSome {类型T }
class D扩展装饰器{
def decorate(r:DR,context:DecoratorContext){}
// def decorate(r:DS [_],context:DecoratorContext){} //编译也是
def shouldExecuteOnProject(project:Project)= true
}
我不确定它是否允许您实施您所需的功能。我查看了,它可能代表文件
,它扩展了 Resource< Directory>
或者有时候会被删除(raw? c> Resource ,例如 Directory
。
编辑:再想一想, forSome
可以被消除 - 编译也是如此:
$ $ p $
def decorate(resource:Resource [_< ;: Resource [_< ;: AnyRef]],
context:DecoratorContext){
}
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);
}
but Resource type is actually defined like:
public abstract class Resource<PARENT extends Resource>
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).
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
}
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
.
Edit: thinking about it some more, the forSome
can be eliminated - this compiles too:
def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]],
context: DecoratorContext) {
}
这篇关于使用Scala的Raw类型实现Java接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!