说出你给定的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
用例:
我正在尝试编写一个通用的重试模式,即获取一些非常容易出错的代码,并且我想重试执行一些最大次数的尝试,每当它失败时,它应该执行一些“等待”方法
e.g。
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
有可能吗?如何?
答案 0 :(得分:9)
更简单的方法是使用util.control.Exception
:
def retryRequest(req: => Unit,
validExceptions: List[Class[_ <: Throwable]],
tries: Int,
waitMethod: => Unit): Unit =
(Exception.catching(validExceptions:_*) withApply { e =>
waitMethod
if (tries > 1) retryRequest(req, validExceptions, tries - 1, waitMethod)
}) { req }
retryRequest( { println("a"); throw new Exception },
List(classOf[Exception]),
3,
Thread.sleep(100))
简而言之:withApply
采用一个闭包来处理传递给catching
的异常之一的情况。在我们的例子中,我们只是递归地称呼自己(假设重试次数很少,我认为这不是问题)。
答案 1 :(得分:6)
我认为这应该有效:
validExceptions.exists(exClass => exClass.isAssignableFrom(ex.getClass))
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom%28java.lang.Class%29