Uast: findUSwitchClauseBody dont throw exception if there is no clause

(EA-114369)
This commit is contained in:
Nicolay Mitropolsky
2018-02-05 18:09:25 +03:00
parent db33a7203a
commit 6321cec6ab
4 changed files with 18 additions and 3 deletions
@@ -76,7 +76,7 @@ private fun JavaAbstractUElement.unwrapSwitch(uParent: UElement): UElement {
}
val uSwitchExpression = codeBlockParent.uastParent as? JavaUSwitchExpression ?: return uParent
val psiElement = psi ?: return uParent
return findUSwitchClauseBody(uSwitchExpression, psiElement)
return findUSwitchClauseBody(uSwitchExpression, psiElement) ?: return codeBlockParent
}
if (codeBlockParent is JavaUSwitchExpression) {
return unwrapSwitch(codeBlockParent)
@@ -71,11 +71,11 @@ internal fun findUSwitchEntry(body: UExpressionList, el: PsiSwitchLabelStatement
body.also { require(it.kind == JavaSpecialExpressionKinds.SWITCH) }
.expressions.find { (it as? JavaUSwitchEntry)?.labels?.contains(el) ?: false } as? JavaUSwitchEntry
internal fun findUSwitchClauseBody(switch: JavaUSwitchExpression, psi: PsiElement): UExpressionList {
internal fun findUSwitchClauseBody(switch: JavaUSwitchExpression, psi: PsiElement): UExpressionList? {
val bodyExpressions = switch.body.expressions
val uExpression = bodyExpressions.find {
(it as JavaUSwitchEntry).body.expressions.any { it.psi == psi }
} ?: throw IllegalStateException("${psi.javaClass} not found in ${bodyExpressions.map { it.asLogString() }}")
} ?: return null
return (uExpression as JavaUSwitchEntry).body
}
@@ -0,0 +1,9 @@
class A {
void foo(int a){
switch (a){
return;
}
}
}
@@ -165,4 +165,10 @@ class JavaUastApiTest : AbstractJavaUastTest() {
}
}
@Test
fun testCanFindAWayFromBrokenSwitch() = doTest("BrokenCode/Switch.java") { name, file ->
val testClass = file.findElementByTextFromPsi<UElement>("""return;""")
TestCase.assertEquals(7, testClass.withContainingElements.count())
}
}