Java to Kotlin converter: base class parameters detection logic moved into ConstructorConverter too

This commit is contained in:
Valentin Kipyatkov
2014-06-26 23:11:21 +04:00
parent e997557ba1
commit 3c34197bc0
4 changed files with 18 additions and 54 deletions
@@ -74,6 +74,18 @@ class ConstructorConverter(private val psiClass: PsiClass, private val converter
}
}
public fun baseClassParams(): PsiExpressionList? {
if (primaryConstructor == null) return null
val statement = primaryConstructor.getBody()?.getStatements()?.firstOrNull()
val methodCall = (statement as? PsiExpressionStatement)?.getExpression() as? PsiMethodCallExpression
if (methodCall != null && methodCall.isSuperConstructorCall()) {
return methodCall.getArgumentList()
}
else {
return null
}
}
public fun convertConstructor(constructor: PsiMethod,
annotations: Annotations,
modifiers: Modifiers,
@@ -331,4 +343,9 @@ class ConstructorConverter(private val psiClass: PsiClass, private val converter
}
return statements
}
private fun PsiMethodCallExpression.isSuperConstructorCall(): Boolean {
val ref = getMethodExpression()
return ref.getCanonicalText() == "super" && ref.resolve()?.isConstructor() ?: false
}
}
+1 -12
View File
@@ -191,18 +191,6 @@ public class Converter private(val project: Project, val settings: ConverterSett
psiClass.isEnum() -> Enum(name, annotations, modifiers, typeParameters, listOf(), listOf(), implementsTypes, classBody)
else -> {
val baseClassParams: List<Expression> = run {
val superVisitor = SuperVisitor()
psiClass.accept(superVisitor)
val resolvedSuperCallParameters = superVisitor.resolvedSuperCallParameters
if (resolvedSuperCallParameters.size() == 1) {
convertExpressions(resolvedSuperCallParameters.single().getExpressions())
}
else {
listOf()
}
}
if (settings.openByDefault && !psiClass.hasModifierProperty(PsiModifier.FINAL)) {
modifiers = modifiers.with(Modifier.OPEN)
}
@@ -211,6 +199,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
modifiers = modifiers.with(Modifier.INNER)
}
val baseClassParams = convertExpressions(constructorConverter.baseClassParams()?.getExpressions() ?: array())
Class(name, annotations, modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, classBody)
}
}.assignPrototype(psiClass)
-8
View File
@@ -125,12 +125,4 @@ fun PsiElement.getContainingConstructor(): PsiMethod? {
return if (method?.isConstructor() == true) method else null
}
fun PsiMethodCallExpression.isSuperConstructorCall(): Boolean {
val ref = getMethodExpression()
if (ref.getCanonicalText() == "super") {
return ref.resolve()?.isConstructor() ?: false
}
return false
}
fun PsiElement.isConstructor(): Boolean = this is PsiMethod && this.isConstructor()
@@ -1,34 +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.visitors
import com.intellij.psi.*
import java.util.HashSet
import org.jetbrains.jet.j2k.isSuperConstructorCall
class SuperVisitor() : JavaRecursiveElementVisitor() {
private val _resolvedSuperCallParameters = HashSet<PsiExpressionList>()
public val resolvedSuperCallParameters: Set<PsiExpressionList>
get() = _resolvedSuperCallParameters
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
if (expression.isSuperConstructorCall()) {
_resolvedSuperCallParameters.add(expression.getArgumentList())
}
}
}