NewJavaToKotlinConverter.filesToKotlin giving "java.lang.IllegalStateException: Calling invokeAndWait from read-action leads to possible deadlock."

Answered

I am trying to run IntelliJ J2K from command line. Since there is no way of running this tool from command line, I am using the following hack.
 
1. Since code inspections can be run from command line, I am trying to wrap J2K conversion logic inside code inspection. (Something similar to this. But this inspection tool is not useful since it only works until first phase conversion).
2. I found out the conversion is a two step process, a. First phase Java to Kotlin Conversion b. Post processing the converted Kotlin. 
a. If I use elementsToKotlin method, it works fine for first the first step. But the converted Kotlin is still not clean.

For example, following JAVA code gets converted to,

package demo;

public class Demo {
private String name="kiran";
}

this,

package demo class Demo constructor (){ private var name:/*@@adhquo@@*/kotlin.String? = "kiran" } 

Here, as you can see, the converted code is verbose with unnecessary `constructor()` etc. The code is also not formatted. 

b. There is another method filesToKotlin that does first phase conversion and post processing as well. However, I found that it is doing the first phase well, but when going through second phase(post processing), it is failing with below error. 

`"java.lang.IllegalStateException: Calling invokeAndWait from read-action leads to possible deadlock."`


Here, is the code snippet. 

val extension = NewJ2kConverterExtension()
val newJavaToKotlinConverter = extension.createJavaToKotlinConverter(project, module, defaultSettings, oldServices)
try {
val filesResult = newJavaToKotlinConverter.filesToKotlin(javaFileList,extension.createPostProcessor(true))
} catch (e:Exception) {
e.printStackTrace()
}


The reason for this is, as part of post processing, and to rename the `.java` file to `.kt` file, the `NewJavaToKotlinConverter` class is using this method to dispatch a task to the event thread of IDE. The invokeAndWait method is used to execute a task on the IntelliJ IDEA event dispatch thread and it is throwing this error maybe because it is a blocking operation. There is another method called invokeLater for Application class but none of the `NewJavaToKotlinConverter` methods are using it. 

Can you please tell me how to fix this error?



0
2 comments

Disclaimer: J2K is not supposed to be used outside of Kotlin IntelliJ plugin and it does not have any public API, so everything in the API and implementation might be changed.

By the exception you have, it looks like you are running the `newJavaToKotlinConverter.filesToKotlin` from the read action (https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html#read-write-lock). IntelliJ platform extension you are using might run your code from the read action. J2K handles all the readAction/writeAction stuff itself. To solve the problem, you might need to execute `newJavaToKotlinConverter.filesToKotlin`  outside of the read action.

0

To solve the problem, you might need to execute `newJavaToKotlinConverter.filesToKotlin`  outside of the read action.
Would you please tell me which extension is best suited for that purpose? Is it appStarter?

0

Please sign in to leave a comment.