Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Java Interface with Raw type from Scala

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

like image 467
Roman G. Avatar asked Jun 04 '12 00:06

Roman G.


1 Answers

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) {
}
like image 124
huynhjl Avatar answered Oct 17 '22 23:10

huynhjl