Implement Java Interface with Raw type from Scala(使用 Scala 的 Raw 类型实现 Java 接口)
问题描述
我正在尝试使用 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 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Scala 的 Raw 类型实现 Java 接口
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
