Implement Java Interface with Raw type from Scala

2019-03-29 04:40发布

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...)

2条回答
2楼-- · 2019-03-29 05:17

I have no idea about an answer, but if I write

def decorate(r: Resource[Resource[_]])

I get an error

type arguments [Resource[_]] do not conform to class Resource's type parameter bounds [PARENT <: Resource[_ <: AnyRef]]

Which seems wrong to me, because I would think the actual type bounds should be more like Resource[_ <: Resource[_ <: Resource[... ...]] (AnyRef wouldn't be appropriate as an upper bound).

查看更多
疯言疯语
3楼-- · 2019-03-29 05:35

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) {
}
查看更多
登录 后发表回答