Inheritance 实现不安全的Java接口
我最近在使用SpringSecurity进行开发时遇到了一个问题。它有一个带有以下签名的接口Inheritance 实现不安全的Java接口,inheritance,scala,language-interoperability,Inheritance,Scala,Language Interoperability,我最近在使用SpringSecurity进行开发时遇到了一个问题。它有一个带有以下签名的接口GrantedAuthority: public interface GrantedAuthority extends Serializable, Comparable 对于Java1.5和更高版本,接口Comparable采用类型参数T,这在Spring安全库中被省略(显然是为了JVM1.4的兼容性) 因此,我试图在Scala中实现GrantedAuthority class Role extends
GrantedAuthority
:
public interface GrantedAuthority extends Serializable, Comparable
对于Java1.5和更高版本,接口Comparable
采用类型参数T
,这在Spring安全库中被省略(显然是为了JVM1.4的兼容性)
因此,我试图在Scala中实现GrantedAuthority
class Role extends GrantedAuthority {
. . .
def compareTo(obj: Any): Int = obj match {
case (r: Role) => r.toString.compareTo(this.toString)
case _ => -1
}
}
它不编译:
error: class Role needs to be abstract, since method compareTo in trait Comparable of type (T)Int is not defined
如何在Scala中实现这样的接口?Java泛型的互操作问题(至少)有两种形式:
Comparable
被视为存在类型Comparable[\u]
。有时你可以摆脱这个问题。但是,在这种情况下,我看不到实现def compareTo(other:)=…
的方法排序[-T]
扩展可比[T],除非使用@uncheckedVariance
注释,否则将发生错误。()我建议您尝试升级到Spring3.x,它是根据Java1.5编译的。如果不可能,请用Java编写一个基类
BaseGrantedAuthority
,实现compareTo
,并委托给可以在Scala中实现的模板方法 与原始类型相关的声音如下:谢谢,追溯。最后的答案是,实际上,“wontfix”?我对这个互操作问题感兴趣,而不是解决我的特殊情况——你完美地向我展示了这一点。谢谢
class Role extends GrantedAuthority {
. . .
def compareTo(obj: Any): Int = obj match {
case (r: Role) => r.toString.compareTo(this.toString)
case _ => -1
}
}