Uast: better reports for failed method conversions in KotlinUClass

This commit is contained in:
Nicolay Mitropolsky
2018-02-26 20:10:40 +03:00
parent 795516269d
commit 35ce30aedc
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript
import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.uast.* import org.jetbrains.uast.*
@@ -109,7 +110,7 @@ open class KotlinUClass private constructor(
else else
KotlinConstructorUMethod(ktClass, psiMethod, this) KotlinConstructorUMethod(ktClass, psiMethod, this)
} else { } else {
getLanguagePlugin().convert(psiMethod, this) getLanguagePlugin().convertOpt(psiMethod, this) ?: reportConvertFailure(psiMethod)
} }
} }
@@ -253,7 +254,7 @@ class KotlinScriptUClass(
KotlinScriptConstructorUMethod(psi.script, method as KtLightMethod, this) KotlinScriptConstructorUMethod(psi.script, method as KtLightMethod, this)
} }
else { else {
getLanguagePlugin().convert(method, this) getLanguagePlugin().convertOpt(method, this) ?: reportConvertFailure(method)
} }
} }
@@ -272,3 +273,20 @@ class KotlinScriptUClass(
override val sourcePsi = psi.kotlinOrigin override val sourcePsi = psi.kotlinOrigin
} }
} }
private fun reportConvertFailure(psiMethod: PsiMethod): Nothing {
val isValid = psiMethod.isValid
val report = KotlinExceptionWithAttachments(
"cant convert $psiMethod of ${psiMethod.javaClass} to UMethod"
+ if (!isValid) " (method is not valid)" else ""
)
if (isValid) {
report.withAttachment("method", psiMethod.text)
psiMethod.containingFile?.let {
report.withAttachment("file", it.text)
}
}
throw report
}