Embed a Java interface with Raw-type from Scala

I am 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 the type of resource is defined as:

 public abstract class Resource<PARENT extends Resource> 

I know I can get around creating a Java super class. I would like to stick with Scala - I just know if there is a solution that I am missing, and if there is an improvement that I could suggest that SonarSource people can do it on their side (using raw types).

I read that there were problems with this, and some workarounds for some cases, but no one applied here ( workaround , obviously fixed ticket , also have ticket 2091 ...)

+6
source share
2 answers

After some trial and error and looking at the error messages, I came up with the following:

 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: DR, context: DecoratorContext) {} //def decorate(r: DS[_], context: DecoratorContext) {} // compiles too def shouldExecuteOnProject(project: Project) = true } 

I am not sure if this will allow you to realize what you need. I looked at Resource , and it can represent a File that extends Resource<Directory> or is sometimes a raw? Type that just extends Resource like for Directory .

Edit: after thinking about it, you can remove forSome - this also compiles:

 def decorate(resource: Resource[_ <: Resource[_ <: AnyRef]], context: DecoratorContext) { } 
+3
source

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

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

I get an error

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

Which seems wrong to me because I think that the actual type constraints should be more like Resource[_ <: Resource[_ <: Resource[... ...]] ( AnyRef not suitable for the upper bound).

0
source

Source: https://habr.com/ru/post/917326/


All Articles