Java to Kotlin converter: refactoring
This commit is contained in:
@@ -23,6 +23,7 @@ import java.util.*
|
|||||||
import com.intellij.psi.CommonClassNames.*
|
import com.intellij.psi.CommonClassNames.*
|
||||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.util.PsiMethodUtil
|
||||||
|
|
||||||
public trait ConversionScope {
|
public trait ConversionScope {
|
||||||
public fun contains(element: PsiElement): Boolean
|
public fun contains(element: PsiElement): Boolean
|
||||||
@@ -100,6 +101,16 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return File(convertedChildren, createMainFunction(javaFile)).assignPrototype(javaFile)
|
return File(convertedChildren, createMainFunction(javaFile)).assignPrototype(javaFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createMainFunction(file: PsiJavaFile): String? {
|
||||||
|
for (`class` in file.getClasses()) {
|
||||||
|
val mainMethod = PsiMethodUtil.findMainMethod(`class`)
|
||||||
|
if (mainMethod != null) {
|
||||||
|
return "fun main(args: Array<String>) = ${`class`.getName()}.${mainMethod.getName()}(args)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
||||||
return AnonymousClassBody(convertBody(anonymousClass, null), anonymousClass.getBaseClassType().resolve()?.isInterface() ?: false).assignPrototype(anonymousClass)
|
return AnonymousClassBody(convertBody(anonymousClass, null), anonymousClass.getBaseClassType().resolve()?.isInterface() ?: false).assignPrototype(anonymousClass)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.jet.j2k
|
|
||||||
|
|
||||||
import com.intellij.psi.PsiMethod
|
|
||||||
import com.intellij.psi.PsiClass
|
|
||||||
import com.intellij.psi.PsiJavaFile
|
|
||||||
import java.util.ArrayList
|
|
||||||
import com.intellij.psi.PsiAnonymousClass
|
|
||||||
import com.intellij.psi.PsiModifier
|
|
||||||
import com.intellij.psi.PsiType
|
|
||||||
import com.intellij.psi.PsiArrayType
|
|
||||||
import com.intellij.psi.util.PsiMethodUtil
|
|
||||||
|
|
||||||
fun createMainFunction(file: PsiJavaFile): String {
|
|
||||||
for (`class` in file.getClasses()) {
|
|
||||||
val mainMethod = PsiMethodUtil.findMainMethod(`class`)
|
|
||||||
if (mainMethod != null) {
|
|
||||||
return "fun main(args: Array<String>) = ${`class`.getName()}.${mainMethod.getName()}(args)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
fun PsiMethod.isMainMethod(): Boolean = PsiMethodUtil.isMainMethod(this)
|
|
||||||
@@ -22,6 +22,7 @@ import com.intellij.psi.util.PsiUtil
|
|||||||
import com.intellij.psi.search.LocalSearchScope
|
import com.intellij.psi.search.LocalSearchScope
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import org.jetbrains.jet.j2k.ast.*
|
import org.jetbrains.jet.j2k.ast.*
|
||||||
|
import com.intellij.psi.util.PsiMethodUtil
|
||||||
|
|
||||||
fun quoteKeywords(packageName: String): String = packageName.split("\\.").map { Identifier.toKotlin(it) }.joinToString(".")
|
fun quoteKeywords(packageName: String): String = packageName.split("\\.").map { Identifier.toKotlin(it) }.joinToString(".")
|
||||||
|
|
||||||
@@ -133,3 +134,5 @@ fun PsiModifierListOwner.accessModifier(): String = when {
|
|||||||
hasModifierProperty(PsiModifier.PROTECTED) -> PsiModifier.PROTECTED
|
hasModifierProperty(PsiModifier.PROTECTED) -> PsiModifier.PROTECTED
|
||||||
else -> PsiModifier.PACKAGE_LOCAL
|
else -> PsiModifier.PACKAGE_LOCAL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun PsiMethod.isMainMethod(): Boolean = PsiMethodUtil.isMainMethod(this)
|
||||||
|
|||||||
@@ -25,12 +25,11 @@ class PackageStatement(val packageName: String) : Element() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class File(
|
class File(val elements: List<Element>, val mainFunction: String?) : Element() {
|
||||||
val elements: List<Element>,
|
|
||||||
val mainFunction: String
|
|
||||||
) : Element() {
|
|
||||||
|
|
||||||
override fun generateCode(builder: CodeBuilder) {
|
override fun generateCode(builder: CodeBuilder) {
|
||||||
builder.append(elements, "\n").append(mainFunction)
|
builder.append(elements, "\n")
|
||||||
|
if (mainFunction != null) {
|
||||||
|
builder.append(mainFunction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user