Refactor converter:
Refactor handling of type parameter list
This commit is contained in:
committed by
Pavel V. Talanov
parent
9ed6fe8888
commit
ffbbbc8529
@@ -112,15 +112,15 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
private fun classToClass(psiClass: PsiClass): Class {
|
||||
val modifiers: Set<Modifier> = modifiersListToModifiersSet(psiClass.getModifierList())
|
||||
val fields: List<Field> = fieldsToFieldList(psiClass.getFields(), psiClass)
|
||||
val typeParameters: List<Element> = elementsToElementList(psiClass.getTypeParameters())
|
||||
val implementsTypes: List<Type> = typesToNotNullableTypeList(psiClass.getImplementsListTypes())
|
||||
val extendsTypes: List<Type> = typesToNotNullableTypeList(psiClass.getExtendsListTypes())
|
||||
val modifiers = modifiersListToModifiersSet(psiClass.getModifierList())
|
||||
val fields = fieldsToFieldList(psiClass.getFields(), psiClass)
|
||||
val typeParameters = typeParameterListToTypeParameterList(psiClass.getTypeParameterList())
|
||||
val implementsTypes = typesToNotNullableTypeList(psiClass.getImplementsListTypes())
|
||||
val extendsTypes = typesToNotNullableTypeList(psiClass.getExtendsListTypes())
|
||||
val name: Identifier = Identifier(psiClass.getName()!!)
|
||||
val baseClassParams = ArrayList<Expression>()
|
||||
val members = ArrayList(getMembers(psiClass))
|
||||
val visitor: SuperVisitor = SuperVisitor()
|
||||
val visitor = SuperVisitor()
|
||||
psiClass.accept(visitor)
|
||||
val resolvedSuperCallParameters = visitor.resolvedSuperCallParameters
|
||||
if (resolvedSuperCallParameters.size() == 1) {
|
||||
@@ -169,7 +169,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
members.add(Constructor(this, Identifier.EMPTY_IDENTIFIER, null, Collections.emptySet<Modifier>(),
|
||||
ClassType(name, Collections.emptyList<Element>(), false, this),
|
||||
Collections.emptyList<Element>(),
|
||||
TypeParameterList.Empty,
|
||||
ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
|
||||
Block(createInitStatementsFromFields(finalOrWithEmptyInitializer)),
|
||||
true))
|
||||
@@ -234,7 +234,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
val body = blockToBlock(method.getBody(), notEmpty)
|
||||
|
||||
val params: Element = createFunctionParameters(method)
|
||||
val typeParameters = elementsToElementList(method.getTypeParameters())
|
||||
val typeParameterList = typeParameterListToTypeParameterList(method.getTypeParameterList())
|
||||
val modifiers = modifiersListToModifiersSet(method.getModifierList())
|
||||
if (isOverride(method)) {
|
||||
modifiers.add(Modifier.OVERRIDE)
|
||||
@@ -250,11 +250,11 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
if (method.isConstructor()) {
|
||||
return Constructor(this, identifier, getDocComment(method), modifiers, returnType, typeParameters, params,
|
||||
return Constructor(this, identifier, getDocComment(method), modifiers, returnType, typeParameterList, params,
|
||||
Block(body.statements), isConstructorPrimary(method))
|
||||
}
|
||||
|
||||
return Function(this, identifier, getDocComment(method), modifiers, returnType, typeParameters, params, body)
|
||||
return Function(this, identifier, getDocComment(method), modifiers, returnType, typeParameterList, params, body)
|
||||
}
|
||||
|
||||
private fun createFunctionParameters(method: PsiMethod): ParameterList {
|
||||
|
||||
@@ -25,7 +25,7 @@ public class AnonymousClass(converter: Converter, members: List<Node>)
|
||||
Identifier("anonClass"),
|
||||
null,
|
||||
Collections.emptySet<Modifier>(),
|
||||
Collections.emptyList<Element>(),
|
||||
TypeParameterList.Empty,
|
||||
Collections.emptyList<Type>(),
|
||||
Collections.emptyList<Expression>(),
|
||||
Collections.emptyList<Type>(), members) {
|
||||
|
||||
@@ -26,7 +26,7 @@ public open class Class(val converter: Converter,
|
||||
val name: Identifier,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
val typeParameters: List<Element>,
|
||||
val typeParameterList: TypeParameterList,
|
||||
val extendsTypes: List<Type>,
|
||||
val baseClassParams: List<Expression>,
|
||||
val implementsTypes: List<Type>,
|
||||
@@ -43,7 +43,7 @@ public open class Class(val converter: Converter,
|
||||
return if (maybeConstructor != null) maybeConstructor.primarySignatureToKotlin() else "()"
|
||||
}
|
||||
|
||||
open fun primaryConstructorBodyToKotlin(): String? {
|
||||
fun primaryConstructorBodyToKotlin(): String? {
|
||||
val maybeConstructor = getPrimaryConstructor()
|
||||
if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty() ?: true)) {
|
||||
return maybeConstructor.primaryBodyToKotlin() + "\n"
|
||||
@@ -52,19 +52,9 @@ public open class Class(val converter: Converter,
|
||||
return ""
|
||||
}
|
||||
|
||||
private fun hasWhere(): Boolean = typeParameters.any { it is TypeParameter && it.hasWhere() }
|
||||
fun membersExceptConstructors(): List<Node> = members.filterNot { it is Constructor }
|
||||
|
||||
open fun typeParameterWhereToKotlin(): String {
|
||||
if (hasWhere()) {
|
||||
val wheres = typeParameters.filter { it is TypeParameter }.map { (it as TypeParameter).getWhereToKotlin() }
|
||||
return " where " + wheres.makeString(", ")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
open fun membersExceptConstructors(): List<Node> = members.filterNot { it is Constructor }
|
||||
|
||||
open fun secondaryConstructorsAsStaticInitFunction(): List<Function> {
|
||||
fun secondaryConstructorsAsStaticInitFunction(): List<Function> {
|
||||
return members.filter { it is Constructor && !it.isPrimary }.map { constructorToInit(it as Function) }
|
||||
}
|
||||
|
||||
@@ -74,25 +64,23 @@ public open class Class(val converter: Converter,
|
||||
val statements = ArrayList<Element>(f.block?.statements ?: listOf())
|
||||
statements.add(ReturnStatement(Identifier("__")))
|
||||
val block = Block(statements)
|
||||
val constructorTypeParameters = ArrayList<Element>()
|
||||
constructorTypeParameters.addAll(typeParameters)
|
||||
constructorTypeParameters.addAll(f.typeParameters)
|
||||
val constructorTypeParameters = ArrayList<TypeParameter>()
|
||||
constructorTypeParameters.addAll(typeParameterList.parameters)
|
||||
constructorTypeParameters.addAll(f.typeParameterList.parameters)
|
||||
return Function(converter, Identifier("init"), null, modifiers,
|
||||
ClassType(name, constructorTypeParameters, false, converter),
|
||||
constructorTypeParameters, f.params, block)
|
||||
TypeParameterList(constructorTypeParameters), f.params, block)
|
||||
}
|
||||
|
||||
open fun typeParametersToKotlin(): String = typeParameters.toKotlin(", ", "<", ">")
|
||||
|
||||
open fun baseClassSignatureWithParams(): List<String> {
|
||||
fun baseClassSignatureWithParams(): List<String> {
|
||||
if (TYPE.equals("class") && extendsTypes.size() == 1) {
|
||||
val baseParams = baseClassParams.toKotlin(", ")
|
||||
return arrayList(extendsTypes[0].toKotlin() + "(" + baseParams + ")")
|
||||
return arrayListOf(extendsTypes[0].toKotlin() + "(" + baseParams + ")")
|
||||
}
|
||||
return extendsTypes.map { it.toKotlin() }
|
||||
}
|
||||
|
||||
open fun implementTypesToKotlin(): String {
|
||||
fun implementTypesToKotlin(): String {
|
||||
val allTypes = ArrayList<String>()
|
||||
allTypes.addAll(baseClassSignatureWithParams())
|
||||
allTypes.addAll(implementsTypes.map { it.toKotlin() })
|
||||
@@ -102,7 +90,7 @@ public open class Class(val converter: Converter,
|
||||
" : " + allTypes.makeString(", ")
|
||||
}
|
||||
|
||||
open fun modifiersToKotlin(): String {
|
||||
fun modifiersToKotlin(): String {
|
||||
val modifierList = ArrayList<Modifier>()
|
||||
val modifier = accessModifier()
|
||||
if (modifier != null) {
|
||||
@@ -125,21 +113,21 @@ public open class Class(val converter: Converter,
|
||||
return " {\n" + getNonStatic(membersExceptConstructors()).toKotlin("\n", "", "\n") + primaryConstructorBodyToKotlin() + classObjectToKotlin() + "}"
|
||||
}
|
||||
|
||||
private fun classObjectToKotlin(): String {
|
||||
fun classObjectToKotlin(): String {
|
||||
val staticMembers = ArrayList<Node>()
|
||||
staticMembers.addAll(secondaryConstructorsAsStaticInitFunction())
|
||||
staticMembers.addAll(getStatic(membersExceptConstructors()))
|
||||
return staticMembers.toKotlin("\n", "class object {\n", "\n}")
|
||||
}
|
||||
|
||||
public override fun toKotlin(): String =
|
||||
override fun toKotlin(): String =
|
||||
docCommentToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
TYPE + " " + name.toKotlin() +
|
||||
typeParametersToKotlin() +
|
||||
typeParameterList.toKotlin() +
|
||||
primaryConstructorSignatureToKotlin() +
|
||||
implementTypesToKotlin() +
|
||||
typeParameterWhereToKotlin() +
|
||||
typeParameterList.whereToKotlin().withPrefix(" ") +
|
||||
bodyToKotlin()
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ public class Constructor(converter: Converter,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
typeParameters: List<Element>,
|
||||
typeParameters: TypeParameterList,
|
||||
params: Element,
|
||||
block: Block,
|
||||
val isPrimary: Boolean) : Function(converter, identifier, docComment, modifiers,
|
||||
|
||||
@@ -20,15 +20,15 @@ import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public class Enum(converter: Converter,
|
||||
name: Identifier,
|
||||
docComments: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameters: List<Element>,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Node>) : Class(converter, name, docComments, modifiers, typeParameters,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
name: Identifier,
|
||||
docComments: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameterList: TypeParameterList,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Node>) : Class(converter, name, docComments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
|
||||
override fun primaryConstructorSignatureToKotlin(): String {
|
||||
val s: String = super.primaryConstructorSignatureToKotlin()
|
||||
@@ -42,7 +42,7 @@ public class Enum(converter: Converter,
|
||||
return modifiersToKotlin() +
|
||||
"enum class " + name.toKotlin() +
|
||||
primaryConstructorSignatureToKotlin() +
|
||||
typeParametersToKotlin() +
|
||||
typeParameterList.toKotlin() +
|
||||
implementTypesToKotlin() +
|
||||
" {\n" + membersExceptConstructors().toKotlin("\n", "", "\n") +
|
||||
(if (primaryConstructorBody.isEmpty()) "" else primaryConstructorBody) +
|
||||
|
||||
@@ -26,28 +26,10 @@ public open class Function(val converter: Converter,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
val `type`: Type,
|
||||
val typeParameters: List<Element>,
|
||||
val typeParameterList: TypeParameterList,
|
||||
val params: Element,
|
||||
var block: Block?) : Member(docComment, modifiers) {
|
||||
|
||||
private fun typeParametersToKotlin() = when {
|
||||
!typeParameters.isEmpty() -> typeParameters.map { it.toKotlin() }.makeString(", ", "<", "> ")
|
||||
else -> ""
|
||||
}
|
||||
|
||||
|
||||
private fun hasWhere(): Boolean = typeParameters.any { it is TypeParameter && it.hasWhere() }
|
||||
|
||||
private fun typeParameterWhereToKotlin(): String {
|
||||
if (hasWhere())
|
||||
{
|
||||
val wheres = typeParameters.filter { it is TypeParameter }.map { ((it as TypeParameter).getWhereToKotlin() ) }
|
||||
return "where " + wheres.makeString(", ") + " "
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
private fun modifiersToKotlin(): String {
|
||||
val resultingModifiers = ArrayList<Modifier>()
|
||||
val isOverride = modifiers.contains(Modifier.OVERRIDE)
|
||||
@@ -84,10 +66,10 @@ public open class Function(val converter: Converter,
|
||||
public override fun toKotlin(): String {
|
||||
return docCommentToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
"fun ${typeParametersToKotlin()}${name.toKotlin()}" +
|
||||
"fun ${typeParameterList.toKotlin().withSuffix(" ")}${name.toKotlin()}" +
|
||||
"(${params.toKotlin()})" +
|
||||
returnTypeToKotlin() +
|
||||
typeParameterWhereToKotlin() +
|
||||
typeParameterList.whereToKotlin() +
|
||||
block?.toKotlin()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ import org.jetbrains.jet.j2k.Converter
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public class Trait(converter: Converter,
|
||||
name: Identifier,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameters: List<Element>,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Node>) : Class(converter, name, docComment, modifiers, typeParameters,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
name: Identifier,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameterList: TypeParameterList,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Node>) : Class(converter, name, docComment, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
|
||||
override val TYPE: String
|
||||
get() = "trait"
|
||||
|
||||
@@ -1,38 +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.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public class TypeParameter(val name: Identifier, val extendsTypes: List<Type>) : Element {
|
||||
public fun hasWhere(): Boolean = extendsTypes.size() > 1
|
||||
public fun getWhereToKotlin(): String {
|
||||
if (hasWhere()) {
|
||||
return name.toKotlin() + " : " + extendsTypes.get(1).toKotlin()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
if (extendsTypes.size() > 0) {
|
||||
return name.toKotlin() + " : " + extendsTypes [0].toKotlin()
|
||||
}
|
||||
|
||||
return name.toKotlin()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.ast
|
||||
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import com.intellij.psi.PsiTypeParameter
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
import com.intellij.psi.PsiTypeParameterList
|
||||
import java.util.ArrayList
|
||||
|
||||
public class TypeParameter(val name: Identifier, val extendsTypes: List<Type>) : Element {
|
||||
public fun hasWhere(): Boolean = extendsTypes.size() > 1
|
||||
public fun getWhereToKotlin(): String {
|
||||
if (hasWhere()) {
|
||||
return name.toKotlin() + " : " + extendsTypes.get(1).toKotlin()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
if (extendsTypes.size() > 0) {
|
||||
return name.toKotlin() + " : " + extendsTypes [0].toKotlin()
|
||||
}
|
||||
|
||||
return name.toKotlin()
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeParameterList(val parameters: List<TypeParameter>) : Element {
|
||||
override fun toKotlin(): String = if (!parameters.isEmpty())
|
||||
parameters.map {
|
||||
it.toKotlin()
|
||||
}.makeString(", ", "<", ">")
|
||||
else ""
|
||||
|
||||
public fun whereToKotlin(): String {
|
||||
if (hasWhere()) {
|
||||
val wheres = parameters.map { it.getWhereToKotlin() }
|
||||
return "where " + wheres.makeString(", ")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean = parameters.isEmpty()
|
||||
|
||||
private fun hasWhere(): Boolean = parameters.any { it.hasWhere() }
|
||||
|
||||
class object {
|
||||
public val Empty: TypeParameterList = TypeParameterList(ArrayList())
|
||||
}
|
||||
}
|
||||
|
||||
public fun Converter.typeParameterToTypeParameter(psiTypeParameter: PsiTypeParameter): TypeParameter {
|
||||
return elementToElement(psiTypeParameter) as TypeParameter
|
||||
}
|
||||
|
||||
public fun Converter.typeParameterListToTypeParameterList(psiTypeParameterlist: PsiTypeParameterList?): TypeParameterList {
|
||||
return if (psiTypeParameterlist == null) TypeParameterList.Empty
|
||||
else TypeParameterList(psiTypeParameterlist.getTypeParameters()!!.toList().map { typeParameterToTypeParameter(it) })
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
fun List<Node>.toKotlin(separator: String, prefix: String = "", suffix: String = ""): String {
|
||||
public fun List<Node>.toKotlin(separator: String, prefix: String = "", suffix: String = ""): String {
|
||||
val result = StringBuilder()
|
||||
if (size() > 0) {
|
||||
result.append(prefix)
|
||||
@@ -31,7 +31,7 @@ fun List<Node>.toKotlin(separator: String, prefix: String = "", suffix: String =
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
fun Collection<Modifier>.toKotlin(separator: String = " "): String {
|
||||
public fun Collection<Modifier>.toKotlin(separator: String = " "): String {
|
||||
val result = StringBuilder()
|
||||
for (x in this) {
|
||||
result.append(x.name)
|
||||
@@ -40,5 +40,6 @@ fun Collection<Modifier>.toKotlin(separator: String = " "): String {
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
fun String.withPrefix(prefix: String) = if (isEmpty()) "" else prefix + this
|
||||
fun Expression.withPrefix(prefix: String) = if (isEmpty()) "" else prefix + toKotlin()
|
||||
public fun String.withSuffix(suffix: String): String = if (isEmpty()) "" else this + suffix
|
||||
public fun String.withPrefix(prefix: String): String = if (isEmpty()) "" else prefix + this
|
||||
public fun Expression.withPrefix(prefix: String): String = if (isEmpty()) "" else prefix + toKotlin()
|
||||
|
||||
Reference in New Issue
Block a user