说你给定的一个实例List[Class[_ <: Base]]
,并说许多类扩展Base
:
class A extends Base
class B extends Base
class C extends A
等等'...
现在,给定的列表可能只包含一些类。例如:val classes = classOf[A] :: Nil
好吧,当我实例化val
它的类是否属于列表中的类型,或者它是否是列表中某个类的子类时,我该如何测试?即您将如何实施:
def testClass(class : List[Class[_ <: Base]], instance : Base) : Boolean
何时:
val classes = classOf[A] :: Nil
testClass(classes, new A) // should return true
testClass(classes, new B) // should return false
testClass(classes, new C) // should return true
用例:
我正在尝试编写一个通用的重试模式,即获取一些非常容易出错的代码,并且我想重试执行它的最大尝试次数,当每次失败时,它应该执行一些“等待“ 方法。
例如
retryRequest({
//Some code that throws exceptions
}, classOf[SomeException] :: classOf[SomeOtherException] :: Nil,
100, {Thread.sleep(5000)})
好吧,这可以正常工作,但它不会测试给定异常的子类:
def retryRequest(req : => Unit, validExceptions : List[Class[_ <: java.lang.Throwable]], tries : Int, waitMethod : => Unit) {
var keepTrying = false
var tryCount = 0
do{
try{
logger.debug("retryRequest, try #" + tryCount)
keepTrying = false
req
}catch{
case ex if(tryCount >= tries && validExceptions.contains(ex.getClass)) => {
throw new MaxTriesReachedException("tried for " + tryCount + "times, but no luck. " +
"you may want to try ommitting generic exceptions types from the given list.")
}
case ex if (validExceptions.contains(ex.getClass)) => {
logger.debug("intercepted " + ex.toString)
tryCount += 1
keepTrying = true
waitMethod
}
}
}while(keepTrying)
}
我真的想替换:
validExceptions.contains(ex.getClass)
有类似的东西:
validExceptions.exists(exClass => ex.isInstanceOf[exClass]) //won't compile
是否可以?如何?