Java to Kotlin converter: working on not loosing comments - refactored redundant .toKotlin() calls
This commit is contained in:
@@ -124,7 +124,7 @@ public class JavaToKotlinActionUtil {
|
|||||||
if (psiFile instanceof PsiJavaFile && virtualFile != null) {
|
if (psiFile instanceof PsiJavaFile && virtualFile != null) {
|
||||||
String result = "";
|
String result = "";
|
||||||
try {
|
try {
|
||||||
result = converter.convertFile((PsiJavaFile) psiFile).toKotlin();
|
result = converter.elementToKotlin(psiFile);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//noinspection CallToPrintStackTrace
|
//noinspection CallToPrintStackTrace
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
= Converter(project, settings, conversionScope, State(typeConverter, state.methodReturnType, state.expressionVisitorFactory, factory))
|
= Converter(project, settings, conversionScope, State(typeConverter, state.methodReturnType, state.expressionVisitorFactory, factory))
|
||||||
|
|
||||||
public fun elementToKotlin(element: PsiElement): String
|
public fun elementToKotlin(element: PsiElement): String
|
||||||
= convertTopElement(element)?.toKotlin() ?: ""
|
= convertTopElement(element)?.toKotlin(/*CommentConverter(element)*/) ?: ""
|
||||||
|
|
||||||
private fun convertTopElement(element: PsiElement?): Element? = when(element) {
|
private fun convertTopElement(element: PsiElement?): Element? = when(element) {
|
||||||
is PsiJavaFile -> convertFile(element)
|
is PsiJavaFile -> convertFile(element)
|
||||||
@@ -69,7 +69,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
is PsiField -> convertField(element)
|
is PsiField -> convertField(element)
|
||||||
is PsiStatement -> convertStatement(element)
|
is PsiStatement -> convertStatement(element)
|
||||||
is PsiExpression -> convertExpression(element)
|
is PsiExpression -> convertExpression(element)
|
||||||
is PsiComment -> Comment(element.getText()!!)
|
is PsiComment -> Comment(element.getText()!!/*, listOf(element)*/)
|
||||||
is PsiImportList -> convertImportList(element)
|
is PsiImportList -> convertImportList(element)
|
||||||
is PsiImportStatementBase -> convertImport(element, false)
|
is PsiImportStatementBase -> convertImport(element, false)
|
||||||
is PsiAnnotation -> convertAnnotation(element, false)
|
is PsiAnnotation -> convertAnnotation(element, false)
|
||||||
@@ -78,7 +78,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertFile(javaFile: PsiJavaFile): File {
|
fun convertFile(javaFile: PsiJavaFile): File {
|
||||||
var convertedChildren = javaFile.getChildren().map {
|
var convertedChildren = javaFile.getChildren().map {
|
||||||
if (it is PsiImportList) {
|
if (it is PsiImportList) {
|
||||||
val importList = convertImportList(it)
|
val importList = convertImportList(it)
|
||||||
@@ -100,7 +100,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return File(FileMemberList(convertedChildren), createMainFunction(javaFile))
|
return File(FileMemberList(convertedChildren), createMainFunction(javaFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
fun convertAnonymousClassBody(anonymousClass: PsiAnonymousClass): AnonymousClassBody {
|
||||||
return AnonymousClassBody(convertClassBody(anonymousClass), anonymousClass.getBaseClassType().resolve()?.isInterface() ?: false)
|
return AnonymousClassBody(convertClassBody(anonymousClass), anonymousClass.getBaseClassType().resolve()?.isInterface() ?: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,13 +248,13 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
private fun generateArtificialPrimaryConstructor(className: Identifier, classBody: ClassBody): ClassBody {
|
private fun generateArtificialPrimaryConstructor(className: Identifier, classBody: ClassBody): ClassBody {
|
||||||
assert(classBody.primaryConstructor == null)
|
assert(classBody.primaryConstructor == null)
|
||||||
|
|
||||||
val finalOrWithEmptyInitializerFields = classBody.normalMembers.members.filterIsInstance(javaClass<Field>()).filter { it.isVal || it.initializer.toKotlin().isEmpty() }
|
val finalOrWithEmptyInitializerFields = classBody.normalMembers.members.filterIsInstance(javaClass<Field>()).filter { it.isVal || it.initializer.isEmpty }
|
||||||
val initializers = HashMap<String, String>()
|
val initializers = HashMap<Field, Expression>()
|
||||||
for (constructor in classBody.secondaryConstructors.members) {
|
for (constructor in classBody.secondaryConstructors.members) {
|
||||||
constructor as SecondaryConstructor
|
constructor as SecondaryConstructor
|
||||||
|
|
||||||
for (field in finalOrWithEmptyInitializerFields) {
|
for (field in finalOrWithEmptyInitializerFields) {
|
||||||
initializers.put(field.identifier.toKotlin(), getDefaultInitializer(field))
|
initializers.put(field, getDefaultInitializer(field))
|
||||||
}
|
}
|
||||||
|
|
||||||
val newStatements = ArrayList<Statement>()
|
val newStatements = ArrayList<Statement>()
|
||||||
@@ -262,11 +262,11 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
var keepStatement = true
|
var keepStatement = true
|
||||||
if (statement is AssignmentExpression) {
|
if (statement is AssignmentExpression) {
|
||||||
val assignee = statement.left
|
val assignee = statement.left
|
||||||
if (assignee is QualifiedExpression) {
|
if (assignee is QualifiedExpression && (assignee.qualifier as? Identifier)?.name == SecondaryConstructor.tempValIdentifier.name) {
|
||||||
|
val name = (assignee.identifier as Identifier).name
|
||||||
for (field in finalOrWithEmptyInitializerFields) {
|
for (field in finalOrWithEmptyInitializerFields) {
|
||||||
val id = field.identifier.toKotlin()
|
if (name == field.identifier.name) {
|
||||||
if (assignee.identifier.toKotlin().endsWith("." + id)) {
|
initializers.put(field, statement.right)
|
||||||
initializers.put(id, statement.right.toKotlin())
|
|
||||||
keepStatement = false
|
keepStatement = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +280,16 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
newStatements.add(0, DummyStringExpression("val __ = " + createPrimaryConstructorInvocation(className.toKotlin(), finalOrWithEmptyInitializerFields, initializers)))
|
|
||||||
|
val initializer = MethodCallExpression.buildNotNull(null, className.name, finalOrWithEmptyInitializerFields.map { initializers[it]!! })
|
||||||
|
val localVar = LocalVariable(SecondaryConstructor.tempValIdentifier,
|
||||||
|
Annotations.Empty,
|
||||||
|
setOf(),
|
||||||
|
{ ClassType(className, listOf(), Nullability.NotNull, settings) },
|
||||||
|
initializer,
|
||||||
|
true,
|
||||||
|
settings)
|
||||||
|
newStatements.add(0, DeclarationStatement(listOf(localVar)))
|
||||||
constructor.block = Block(newStatements)
|
constructor.block = Block(newStatements)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,7 +515,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertBlock(block: PsiCodeBlock?, notEmpty: Boolean = true, statementFilter: (PsiStatement) -> Boolean = {true}): Block {
|
fun convertBlock(block: PsiCodeBlock?, notEmpty: Boolean = true, statementFilter: (PsiStatement) -> Boolean = {true}): Block {
|
||||||
if (block == null) return Block.Empty
|
if (block == null) return Block.Empty
|
||||||
|
|
||||||
val filteredChildren = block.getChildren().filter { it !is PsiStatement || statementFilter(it) }
|
val filteredChildren = block.getChildren().filter { it !is PsiStatement || statementFilter(it) }
|
||||||
@@ -514,7 +523,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return Block(statementList, notEmpty)
|
return Block(statementList, notEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertStatement(statement: PsiStatement?): Statement {
|
fun convertStatement(statement: PsiStatement?): Statement {
|
||||||
if (statement == null) return Statement.Empty
|
if (statement == null) return Statement.Empty
|
||||||
|
|
||||||
statementVisitor.reset()
|
statementVisitor.reset()
|
||||||
@@ -522,10 +531,10 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return statementVisitor.result
|
return statementVisitor.result
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertExpressions(expressions: Array<PsiExpression>): List<Expression>
|
fun convertExpressions(expressions: Array<PsiExpression>): List<Expression>
|
||||||
= expressions.map { convertExpression(it) }
|
= expressions.map { convertExpression(it) }
|
||||||
|
|
||||||
public fun convertExpression(expression: PsiExpression?): Expression {
|
fun convertExpression(expression: PsiExpression?): Expression {
|
||||||
if (expression == null) return Expression.Empty
|
if (expression == null) return Expression.Empty
|
||||||
|
|
||||||
expressionVisitor.reset()
|
expressionVisitor.reset()
|
||||||
@@ -533,7 +542,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return expressionVisitor.result
|
return expressionVisitor.result
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertElement(element: PsiElement?): Element {
|
fun convertElement(element: PsiElement?): Element {
|
||||||
if (element == null) return Element.Empty
|
if (element == null) return Element.Empty
|
||||||
|
|
||||||
val elementVisitor = ElementVisitor(this)
|
val elementVisitor = ElementVisitor(this)
|
||||||
@@ -541,16 +550,16 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return elementVisitor.result
|
return elementVisitor.result
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertTypeElement(element: PsiTypeElement?): TypeElement
|
fun convertTypeElement(element: PsiTypeElement?): TypeElement
|
||||||
= TypeElement(if (element == null) Type.Empty else typeConverter.convertType(element.getType()))
|
= TypeElement(if (element == null) Type.Empty else typeConverter.convertType(element.getType()))
|
||||||
|
|
||||||
private fun convertToNotNullableTypes(types: Array<out PsiType?>): List<Type>
|
private fun convertToNotNullableTypes(types: Array<out PsiType?>): List<Type>
|
||||||
= types.map { typeConverter.convertType(it, Nullability.NotNull) }
|
= types.map { typeConverter.convertType(it, Nullability.NotNull) }
|
||||||
|
|
||||||
public fun convertParameterList(parameterList: PsiParameterList): ParameterList
|
fun convertParameterList(parameterList: PsiParameterList): ParameterList
|
||||||
= ParameterList(parameterList.getParameters().map { convertParameter(it) })
|
= ParameterList(parameterList.getParameters().map { convertParameter(it) })
|
||||||
|
|
||||||
public fun convertParameter(parameter: PsiParameter,
|
fun convertParameter(parameter: PsiParameter,
|
||||||
nullability: Nullability = Nullability.Default,
|
nullability: Nullability = Nullability.Default,
|
||||||
varValModifier: Parameter.VarValModifier = Parameter.VarValModifier.None,
|
varValModifier: Parameter.VarValModifier = Parameter.VarValModifier.None,
|
||||||
modifiers: Collection<Modifier> = listOf()): Parameter {
|
modifiers: Collection<Modifier> = listOf()): Parameter {
|
||||||
@@ -562,7 +571,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return Parameter(Identifier(parameter.getName()!!), `type`, varValModifier, convertAnnotations(parameter), modifiers)
|
return Parameter(Identifier(parameter.getName()!!), `type`, varValModifier, convertAnnotations(parameter), modifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertExpression(argument: PsiExpression?, expectedType: PsiType?): Expression {
|
fun convertExpression(argument: PsiExpression?, expectedType: PsiType?): Expression {
|
||||||
if (argument == null) return Identifier.Empty
|
if (argument == null) return Identifier.Empty
|
||||||
|
|
||||||
var expression = convertExpression(argument)
|
var expression = convertExpression(argument)
|
||||||
@@ -590,17 +599,13 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return expression
|
return expression
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createPrimaryConstructorInvocation(s: String, fields: List<Field>, initializers: Map<String, String>): String {
|
fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
||||||
return s + "(" + fields.map { initializers[it.identifier.toKotlin()] }.makeString(", ") + ")"
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun convertIdentifier(identifier: PsiIdentifier?): Identifier {
|
|
||||||
if (identifier == null) return Identifier.Empty
|
if (identifier == null) return Identifier.Empty
|
||||||
|
|
||||||
return Identifier(identifier.getText()!!)
|
return Identifier(identifier.getText()!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertModifiers(owner: PsiModifierListOwner): MutableSet<Modifier>
|
fun convertModifiers(owner: PsiModifierListOwner): MutableSet<Modifier>
|
||||||
= HashSet(MODIFIERS_MAP.filter { owner.hasModifierProperty(it.first) }.map { it.second })
|
= HashSet(MODIFIERS_MAP.filter { owner.hasModifierProperty(it.first) }.map { it.second })
|
||||||
|
|
||||||
private val MODIFIERS_MAP = listOf(
|
private val MODIFIERS_MAP = listOf(
|
||||||
@@ -610,7 +615,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
PsiModifier.PRIVATE to Modifier.PRIVATE
|
PsiModifier.PRIVATE to Modifier.PRIVATE
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun convertAnnotations(owner: PsiModifierListOwner): Annotations {
|
fun convertAnnotations(owner: PsiModifierListOwner): Annotations {
|
||||||
val modifierList = owner.getModifierList()
|
val modifierList = owner.getModifierList()
|
||||||
val annotations = modifierList?.getAnnotations()?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
|
val annotations = modifierList?.getAnnotations()?.filter { it.getQualifiedName() !in ANNOTATIONS_TO_REMOVE }
|
||||||
if (annotations == null || annotations.isEmpty()) return Annotations.Empty
|
if (annotations == null || annotations.isEmpty()) return Annotations.Empty
|
||||||
@@ -633,7 +638,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
return Annotations(list, newLines)
|
return Annotations(list, newLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun convertAnnotation(annotation: PsiAnnotation, brackets: Boolean): Annotation? {
|
private fun convertAnnotation(annotation: PsiAnnotation, brackets: Boolean): Annotation? {
|
||||||
val qualifiedName = annotation.getQualifiedName()
|
val qualifiedName = annotation.getQualifiedName()
|
||||||
if (qualifiedName == CommonClassNames.JAVA_LANG_DEPRECATED && annotation.getParameterList().getAttributes().isEmpty()) {
|
if (qualifiedName == CommonClassNames.JAVA_LANG_DEPRECATED && annotation.getParameterList().getAttributes().isEmpty()) {
|
||||||
return Annotation(Identifier("deprecated"), listOf(null to LiteralExpression("\"\"")), brackets) //TODO: insert comment
|
return Annotation(Identifier("deprecated"), listOf(null to LiteralExpression("\"\"")), brackets) //TODO: insert comment
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ public object JavaToKotlinTranslator {
|
|||||||
val file = createFile(javaCode)
|
val file = createFile(javaCode)
|
||||||
if (file is PsiJavaFile) {
|
if (file is PsiJavaFile) {
|
||||||
val converter = Converter.create(file.getProject(), ConverterSettings.defaultSettings, FilesConversionScope(listOf(file)))
|
val converter = Converter.create(file.getProject(), ConverterSettings.defaultSettings, FilesConversionScope(listOf(file)))
|
||||||
return prettify(converter.convertFile(file).toKotlin())
|
return prettify(converter.elementToKotlin(file))
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,12 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k
|
package org.jetbrains.jet.j2k
|
||||||
|
|
||||||
import org.jetbrains.jet.j2k.ast.Identifier
|
|
||||||
import org.jetbrains.jet.j2k.ast.Field
|
|
||||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||||
import org.jetbrains.jet.j2k.ast.Nullability
|
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.util.PsiUtil
|
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.*
|
||||||
|
|
||||||
fun quoteKeywords(packageName: String): String = packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
fun quoteKeywords(packageName: String): String = packageName.split("\\.").map { Identifier(it).toKotlin() }.makeString(".")
|
||||||
|
|
||||||
@@ -59,19 +57,22 @@ fun PsiModifierListOwner.nullabilityFromAnnotations(): Nullability {
|
|||||||
Nullability.Default
|
Nullability.Default
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getDefaultInitializer(field: Field): String {
|
fun getDefaultInitializer(field: Field): Expression {
|
||||||
if (field.`type`.isNullable) {
|
val t = field.`type`
|
||||||
return "null"
|
if (t.isNullable) {
|
||||||
|
return LiteralExpression("null")
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return when(field.`type`.toKotlin()) {
|
if (t is PrimitiveType) {
|
||||||
"Boolean" -> "false"
|
when(t.`type`.name) {
|
||||||
"Char" -> "' '"
|
"Boolean" -> return LiteralExpression("false")
|
||||||
"Double" -> "0." + OperatorConventions.DOUBLE + "()"
|
"Char" -> return LiteralExpression("' '")
|
||||||
"Float" -> "0." + OperatorConventions.FLOAT + "()"
|
"Double" -> return MethodCallExpression.buildNotNull(LiteralExpression("0"), OperatorConventions.DOUBLE.toString())
|
||||||
else -> "0"
|
"Float" -> return MethodCallExpression.buildNotNull(LiteralExpression("0"), OperatorConventions.FLOAT.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return LiteralExpression("0")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isQualifierEmptyOrThis(ref: PsiReferenceExpression): Boolean {
|
fun isQualifierEmptyOrThis(ref: PsiReferenceExpression): Boolean {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class SecondaryConstructor(converter: Converter,
|
|||||||
public fun toInitFunction(containingClass: Class): Function {
|
public fun toInitFunction(containingClass: Class): Function {
|
||||||
val modifiers = HashSet(modifiers)
|
val modifiers = HashSet(modifiers)
|
||||||
val statements = ArrayList(block?.statements ?: listOf())
|
val statements = ArrayList(block?.statements ?: listOf())
|
||||||
statements.add(ReturnStatement(Identifier("__")))
|
statements.add(ReturnStatement(tempValIdentifier))
|
||||||
val block = Block(statements)
|
val block = Block(statements)
|
||||||
val typeParameters = ArrayList<TypeParameter>()
|
val typeParameters = ArrayList<TypeParameter>()
|
||||||
typeParameters.addAll(containingClass.typeParameterList.parameters)
|
typeParameters.addAll(containingClass.typeParameterList.parameters)
|
||||||
@@ -65,4 +65,8 @@ class SecondaryConstructor(converter: Converter,
|
|||||||
ClassType(containingClass.name, typeParameters, Nullability.NotNull, converter.settings),
|
ClassType(containingClass.name, typeParameters, Nullability.NotNull, converter.settings),
|
||||||
TypeParameterList(typeParameters), parameterList, block, false)
|
TypeParameterList(typeParameters), parameterList, block, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class object {
|
||||||
|
public val tempValIdentifier: Identifier = Identifier("__", false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,13 +71,13 @@ class SuperExpression(val identifier: Identifier) : Expression() {
|
|||||||
override fun toKotlin() = "super" + identifier.withPrefix("@")
|
override fun toKotlin() = "super" + identifier.withPrefix("@")
|
||||||
}
|
}
|
||||||
|
|
||||||
class QualifiedExpression(val expression: Expression, val identifier: Expression) : Expression() {
|
class QualifiedExpression(val qualifier: Expression, val identifier: Expression) : Expression() {
|
||||||
override val isNullable: Boolean
|
override val isNullable: Boolean
|
||||||
get() = identifier.isNullable
|
get() = identifier.isNullable
|
||||||
|
|
||||||
override fun toKotlin(): String {
|
override fun toKotlin(): String {
|
||||||
if (!expression.isEmpty) {
|
if (!qualifier.isEmpty) {
|
||||||
return operandToKotlin(expression) + (if (expression.isNullable) "!!." else ".") + identifier.toKotlin()
|
return operandToKotlin(qualifier) + (if (qualifier.isNullable) "!!." else ".") + identifier.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
return identifier.toKotlin()
|
return identifier.toKotlin()
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ open class Field(
|
|||||||
override fun toKotlin(): String {
|
override fun toKotlin(): String {
|
||||||
val declaration = commentsToKotlin() + annotations.toKotlin() + modifiersToKotlin() + (if (isVal) "val " else "var ") + identifier.toKotlin() + " : " + `type`.toKotlin()
|
val declaration = commentsToKotlin() + annotations.toKotlin() + modifiersToKotlin() + (if (isVal) "val " else "var ") + identifier.toKotlin() + " : " + `type`.toKotlin()
|
||||||
return if (initializer.isEmpty)
|
return if (initializer.isEmpty)
|
||||||
declaration + (if (isVal && hasWriteAccesses) "" else " = " + getDefaultInitializer(this))
|
declaration + (if (isVal && hasWriteAccesses) "" else " = " + getDefaultInitializer(this).toKotlin())
|
||||||
else
|
else
|
||||||
declaration + " = " + initializer.toKotlin()
|
declaration + " = " + initializer.toKotlin()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class Identifier(
|
|||||||
override val isEmpty: Boolean
|
override val isEmpty: Boolean
|
||||||
get() = name.isEmpty()
|
get() = name.isEmpty()
|
||||||
|
|
||||||
override fun toKotlin(): String {
|
private fun identifierToKotlin(): String {
|
||||||
if (quotingNeeded && ONLY_KOTLIN_KEYWORDS.contains(name) || name.contains("$")) {
|
if (quotingNeeded && ONLY_KOTLIN_KEYWORDS.contains(name) || name.contains("$")) {
|
||||||
return quote(name)
|
return quote(name)
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,8 @@ class Identifier(
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun toKotlin(): String = identifierToKotlin()
|
||||||
|
|
||||||
private fun quote(str: String): String = "`" + str + "`"
|
private fun quote(str: String): String = "`" + str + "`"
|
||||||
|
|
||||||
override fun toString() = if (isNullable) "$name?" else name
|
override fun toString() = if (isNullable) "$name?" else name
|
||||||
@@ -43,6 +45,8 @@ class Identifier(
|
|||||||
|
|
||||||
val ONLY_KOTLIN_KEYWORDS: Set<String> = setOf(
|
val ONLY_KOTLIN_KEYWORDS: Set<String> = setOf(
|
||||||
"package", "as", "type", "val", "var", "fun", "is", "in", "object", "when", "trait", "This"
|
"package", "as", "type", "val", "var", "fun", "is", "in", "object", "when", "trait", "This"
|
||||||
);
|
)
|
||||||
|
|
||||||
|
fun toKotlin(name: String): String = Identifier(name).identifierToKotlin()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,15 +79,7 @@ class ClassType(val `type`: Identifier, val typeArgs: List<Element>, nullability
|
|||||||
: MayBeNullableType(nullability, settings) {
|
: MayBeNullableType(nullability, settings) {
|
||||||
|
|
||||||
override fun toKotlin(): String {
|
override fun toKotlin(): String {
|
||||||
// TODO change to map() when KT-2051 is fixed
|
var params = if (typeArgs.isEmpty()) "" else typeArgs.map { it.toKotlin() }.makeString(", ", "<", ">")
|
||||||
val parametersToKotlin = ArrayList<String>()
|
|
||||||
for (param in typeArgs) {
|
|
||||||
parametersToKotlin.add(param.toKotlin())
|
|
||||||
}
|
|
||||||
var params: String = if (parametersToKotlin.size() == 0)
|
|
||||||
""
|
|
||||||
else
|
|
||||||
"<" + parametersToKotlin.makeString(", ") + ">"
|
|
||||||
return `type`.toKotlin() + params + isNullableStr()
|
return `type`.toKotlin() + params + isNullableStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,11 +46,11 @@ class ElementVisitor(private val converter: Converter) : JavaElementVisitor() {
|
|||||||
result = ReferenceElement(Identifier(reference.getReferenceName()!!), types)
|
result = ReferenceElement(Identifier(reference.getReferenceName()!!), types)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var code = Identifier(reference.getReferenceName()!!).toKotlin()
|
var code = Identifier.toKotlin(reference.getReferenceName()!!)
|
||||||
var qualifier = reference.getQualifier()
|
var qualifier = reference.getQualifier()
|
||||||
while (qualifier != null) {
|
while (qualifier != null) {
|
||||||
val p = qualifier as PsiJavaCodeReferenceElement
|
val p = qualifier as PsiJavaCodeReferenceElement
|
||||||
code = Identifier(p.getReferenceName()!!).toKotlin() + "." + code
|
code = Identifier.toKotlin(p.getReferenceName()!!) + "." + code
|
||||||
qualifier = p.getQualifier()
|
qualifier = p.getQualifier()
|
||||||
}
|
}
|
||||||
result = ReferenceElement(Identifier(code), types)
|
result = ReferenceElement(Identifier(code), types)
|
||||||
|
|||||||
@@ -306,7 +306,7 @@ class ExpressionVisitor(private val converter: Converter,
|
|||||||
val insideSecondaryConstructor = containingConstructor != null && !containingConstructor.isPrimaryConstructor()
|
val insideSecondaryConstructor = containingConstructor != null && !containingConstructor.isPrimaryConstructor()
|
||||||
|
|
||||||
if (insideSecondaryConstructor && (expression.getReference()?.resolve() as? PsiField)?.getContainingClass() == containingConstructor!!.getContainingClass()) {
|
if (insideSecondaryConstructor && (expression.getReference()?.resolve() as? PsiField)?.getContainingClass() == containingConstructor!!.getContainingClass()) {
|
||||||
identifier = QualifiedExpression(Identifier("__", false), Identifier(referencedName, isNullable))
|
identifier = QualifiedExpression(SecondaryConstructor.tempValIdentifier, Identifier(referencedName, isNullable))
|
||||||
}
|
}
|
||||||
else if (insideSecondaryConstructor && expression.isThisConstructorCall()) {
|
else if (insideSecondaryConstructor && expression.isThisConstructorCall()) {
|
||||||
identifier = Identifier("val __ = " + (containingConstructor?.getContainingClass()?.getNameIdentifier()?.getText() ?: ""))
|
identifier = Identifier("val __ = " + (containingConstructor?.getContainingClass()?.getNameIdentifier()?.getText() ?: ""))
|
||||||
@@ -336,9 +336,9 @@ class ExpressionVisitor(private val converter: Converter,
|
|||||||
&& !PsiTreeUtil.isAncestor(target.getContainingClass(), expression, true)
|
&& !PsiTreeUtil.isAncestor(target.getContainingClass(), expression, true)
|
||||||
&& !isStaticallyImported(target, expression)) {
|
&& !isStaticallyImported(target, expression)) {
|
||||||
var member: PsiMember = target
|
var member: PsiMember = target
|
||||||
var code = Identifier(referencedName).toKotlin()
|
var code = Identifier.toKotlin(referencedName)
|
||||||
while (member.getContainingClass() != null) {
|
while (member.getContainingClass() != null) {
|
||||||
code = Identifier(member.getContainingClass()!!.getName()!!).toKotlin() + "." + code
|
code = Identifier.toKotlin(member.getContainingClass()!!.getName()!!) + "." + code
|
||||||
member = member.getContainingClass()!!
|
member = member.getContainingClass()!!
|
||||||
}
|
}
|
||||||
result = Identifier(code, false, false)
|
result = Identifier(code, false, false)
|
||||||
@@ -354,7 +354,7 @@ class ExpressionVisitor(private val converter: Converter,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result = QualifiedExpression(converter.convertExpression(qualifier), identifier)
|
result = if (qualifier != null) QualifiedExpression(converter.convertExpression(qualifier), identifier) else identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitSuperExpression(expression: PsiSuperExpression) {
|
override fun visitSuperExpression(expression: PsiSuperExpression) {
|
||||||
|
|||||||
@@ -273,7 +273,7 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
|||||||
var statementList = bodyConverted.statementList
|
var statementList = bodyConverted.statementList
|
||||||
var expression: Expression = Expression.Empty
|
var expression: Expression = Expression.Empty
|
||||||
for (variable in variables.reverse()) {
|
for (variable in variables.reverse()) {
|
||||||
val lambda = LambdaExpression(Identifier(variable.getName()!!).toKotlin(), statementList)
|
val lambda = LambdaExpression(Identifier.toKotlin(variable.getName()!!), statementList)
|
||||||
expression = MethodCallExpression.build(converter.convertExpression(variable.getInitializer()), "use", listOf(), listOf(), false, lambda)
|
expression = MethodCallExpression.build(converter.convertExpression(variable.getInitializer()), "use", listOf(), listOf(), false, lambda)
|
||||||
statementList = StatementList(listOf(expression))
|
statementList = StatementList(listOf(expression))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,11 +85,11 @@ class TypeVisitor(private val converter: TypeConverter, private val classesToImp
|
|||||||
if (classType is PsiClassReferenceType) {
|
if (classType is PsiClassReferenceType) {
|
||||||
val reference = classType.getReference()
|
val reference = classType.getReference()
|
||||||
if (reference.isQualified()) {
|
if (reference.isQualified()) {
|
||||||
var result = Identifier(reference.getReferenceName()!!).toKotlin()
|
var result = Identifier.toKotlin(reference.getReferenceName()!!)
|
||||||
var qualifier = reference.getQualifier()
|
var qualifier = reference.getQualifier()
|
||||||
while (qualifier != null) {
|
while (qualifier != null) {
|
||||||
val codeRefElement = qualifier as PsiJavaCodeReferenceElement
|
val codeRefElement = qualifier as PsiJavaCodeReferenceElement
|
||||||
result = Identifier(codeRefElement.getReferenceName()!!).toKotlin() + "." + result
|
result = Identifier.toKotlin(codeRefElement.getReferenceName()!!) + "." + result
|
||||||
qualifier = codeRefElement.getQualifier()
|
qualifier = codeRefElement.getQualifier()
|
||||||
}
|
}
|
||||||
return Identifier(result)
|
return Identifier(result)
|
||||||
|
|||||||
Reference in New Issue
Block a user