Java to Kotlin converter: used standard utility for main method detection
This commit is contained in:
@@ -24,44 +24,16 @@ import com.intellij.psi.PsiAnonymousClass
|
|||||||
import com.intellij.psi.PsiModifier
|
import com.intellij.psi.PsiModifier
|
||||||
import com.intellij.psi.PsiType
|
import com.intellij.psi.PsiType
|
||||||
import com.intellij.psi.PsiArrayType
|
import com.intellij.psi.PsiArrayType
|
||||||
|
import com.intellij.psi.util.PsiMethodUtil
|
||||||
|
|
||||||
fun createMainFunction(file: PsiJavaFile): String {
|
fun createMainFunction(file: PsiJavaFile): String {
|
||||||
val classNamesWithMains = ArrayList<Pair<String, PsiMethod>>()
|
for (`class` in file.getClasses()) {
|
||||||
for (c in file.getClasses()) {
|
val mainMethod = PsiMethodUtil.findMainMethod(`class`)
|
||||||
val main = findMainMethod(c)
|
if (mainMethod != null) {
|
||||||
val name = c.getName()
|
return "fun main(args: Array<String>) = ${`class`.getName()}.${mainMethod.getName()}(args)"
|
||||||
if (name != null && main != null) {
|
|
||||||
classNamesWithMains.add(name to main)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (classNamesWithMains.isNotEmpty()) {
|
|
||||||
var className = classNamesWithMains.first().first
|
|
||||||
return "fun main(args : Array<String>) = $className.main(args)"
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findMainMethod(aClass: PsiClass): PsiMethod?
|
fun PsiMethod.isMainMethod(): Boolean = PsiMethodUtil.isMainMethod(this)
|
||||||
= if (isMainClass(aClass)) aClass.findMethodsByName("main", false).firstOrNull { it.isMainMethod() } else null
|
|
||||||
|
|
||||||
private fun isMainClass(psiClass: PsiClass): Boolean
|
|
||||||
= psiClass !is PsiAnonymousClass &&
|
|
||||||
!psiClass.isInterface() &&
|
|
||||||
(psiClass.getContainingClass() == null || psiClass.hasModifierProperty(PsiModifier.STATIC))
|
|
||||||
|
|
||||||
fun PsiMethod.isMainMethod(): Boolean {
|
|
||||||
if (getReturnType() != PsiType.VOID) return false
|
|
||||||
if (!hasModifierProperty(PsiModifier.STATIC)) return false
|
|
||||||
if (!hasModifierProperty(PsiModifier.PUBLIC)) return false
|
|
||||||
|
|
||||||
val parameters = getParameterList().getParameters()
|
|
||||||
if (parameters.size != 1) return false
|
|
||||||
|
|
||||||
val `type` = parameters.single().getType()
|
|
||||||
if (`type` !is PsiArrayType) return false
|
|
||||||
|
|
||||||
val componentType = `type`.getComponentType()
|
|
||||||
return componentType.equalsToText("java.lang.String")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user