Retrieving Java method signature from Kotlin PSI

Answered

I am currently writing a Detekt rule that reports undocumented throws in Kotlin code. Detekt uses the Kotlin's embeddable compiler (but also uses IntelliJ's Open API, that's why I'm posting there).

What I am doing is inspecting the method calls using "getResolvedCall()":

private fun enumerateCalledFunctions(function: KtNamedFunction) =
function.collectDescendantsOfType<KtCallExpression> { it.containingDeclarationForPseudocode == function }
.mapNotNull { it.getResolvedCall(this.bindingContext) }

However, when I encounter an external Java method (such as "java.io.file.Files.delete()"), I'm unable to retrieve the method's "throws".

 

Is there a way to do this?

0
3 comments

With JavaMethodDescriptior.getMethod(), you can access the PsiMethod which can be used to access such throws list with getThrowsList():

someCalledFunction.getMethod().getThrowsList()
0

I met the same problem as well.

I'm trying to convert both Java(PsiMethod) and Kotlin(KtFunction) Psi method to JVM signature:

e.g. 

(Ljava/lang/String;)V

I found the Java impl in https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009724660-How-to-retrieve-the-signature-of-a-PsiMethod-in-the-original-JVM-format-

But no Kotlin impl for this.

Is there any solution for the Psi conversion of JVM signature for Kotlin?

0

You may be able to retrieve a computed Java method signature, but it won't contain the throws.  

The “getThrowsList()” is only used for code gen of @Throws.  Output not input.   The throws list from Java methods are never loaded.  See org.jetbrains.kotlin.load.java.structure.JavaMethod

0

Please sign in to leave a comment.