Refactoring: Extract wrapping 'try' expressions

This commit is contained in:
Yan Zhulanow
2018-12-14 19:25:53 +09:00
parent f5a3a46e76
commit 757fc421f7
@@ -12,14 +12,7 @@ import com.intellij.debugger.jdi.StackFrameProxyImpl
import com.sun.jdi.* import com.sun.jdi.*
fun StackFrameProxyImpl.safeVisibleVariables(): List<LocalVariableProxyImpl> { fun StackFrameProxyImpl.safeVisibleVariables(): List<LocalVariableProxyImpl> {
return try { return wrapAbsentInformationException { visibleVariables() } ?: emptyList()
visibleVariables()
} catch (e: AbsentInformationEvaluateException) {
// Current implementation of visibleVariables() wraps an AbsentInformationException into EvaluateException
emptyList()
} catch (e: AbsentInformationException) {
emptyList()
}
} }
fun Method.safeAllLineLocations(): List<Location> { fun Method.safeAllLineLocations(): List<Location> {
@@ -31,35 +24,19 @@ fun ReferenceType.safeAllLineLocations(): List<Location> {
} }
fun ReferenceType.safeSourceName(): String? { fun ReferenceType.safeSourceName(): String? {
return try { return wrapAbsentInformationException { sourceName() }
sourceName()
} catch (e: AbsentInformationException) {
null
}
} }
fun Method.safeLocationsOfLine(line: Int): List<Location> { fun Method.safeLocationsOfLine(line: Int): List<Location> {
return try { return wrapAbsentInformationException { locationsOfLine(line) } ?: emptyList()
locationsOfLine(line)
} catch (e: AbsentInformationException) {
emptyList()
}
} }
fun Method.safeVariables(): List<LocalVariable>? { fun Method.safeVariables(): List<LocalVariable>? {
return try { return wrapAbsentInformationException { variables() }
variables()
} catch (e: AbsentInformationException) {
null
}
} }
fun Method.safeArguments(): List<LocalVariable>? { fun Method.safeArguments(): List<LocalVariable>? {
return try { return wrapAbsentInformationException { arguments() }
arguments()
} catch (e: AbsentInformationException) {
null
}
} }
fun Location.safeSourceName(): String? { fun Location.safeSourceName(): String? {
@@ -85,16 +62,26 @@ fun Location.safeMethod(): Method? {
} }
fun LocalVariableProxyImpl.safeType(): Type? { fun LocalVariableProxyImpl.safeType(): Type? {
return wrapClassNotLoadedException { type }
}
fun Field.safeType(): Type? {
return wrapClassNotLoadedException { type() }
}
private inline fun <T> wrapAbsentInformationException(block: () -> T): T? {
return try { return try {
type block()
} catch (e: ClassNotLoadedException) { } catch (e: AbsentInformationException) {
null
} catch (e: AbsentInformationEvaluateException) {
null null
} }
} }
fun Field.safeType(): Type? { private inline fun <T> wrapClassNotLoadedException(block: () -> T): T? {
return try { return try {
type() block()
} catch (e: ClassNotLoadedException) { } catch (e: ClassNotLoadedException) {
null null
} }