Refactor converter:

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