More cleanup: lift return / assignment out
This commit is contained in:
@@ -101,11 +101,11 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
}
|
||||
|
||||
val operator = Operator(tokenType).assignPrototype(expression.operationSign)
|
||||
if (secondOp) {
|
||||
result = AssignmentExpression(lhs, BinaryExpression(lhs, rhs, operator).assignNoPrototype(), Operator.EQ)
|
||||
result = if (secondOp) {
|
||||
AssignmentExpression(lhs, BinaryExpression(lhs, rhs, operator).assignNoPrototype(), Operator.EQ)
|
||||
}
|
||||
else {
|
||||
result = AssignmentExpression(lhs, rhs, operator)
|
||||
AssignmentExpression(lhs, rhs, operator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,14 +532,14 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
override fun visitPrefixExpression(expression: PsiPrefixExpression) {
|
||||
val operand = codeConverter.convertExpression(expression.operand, expression.operand!!.type)
|
||||
val token = expression.operationTokenType
|
||||
if (token == JavaTokenType.TILDE) {
|
||||
result = MethodCallExpression.buildNonNull(operand, "inv")
|
||||
result = if (token == JavaTokenType.TILDE) {
|
||||
MethodCallExpression.buildNonNull(operand, "inv")
|
||||
}
|
||||
else if (token == JavaTokenType.EXCL && operand is BinaryExpression && operand.op.asString() == "==") { // happens when equals is converted to ==
|
||||
result = BinaryExpression(operand.left, operand.right, Operator(JavaTokenType.NE).assignPrototype(expression.operand))
|
||||
BinaryExpression(operand.left, operand.right, Operator(JavaTokenType.NE).assignPrototype(expression.operand))
|
||||
}
|
||||
else {
|
||||
result = PrefixExpression(Operator(token).assignPrototype(expression.operand), operand)
|
||||
PrefixExpression(Operator(token).assignPrototype(expression.operand), operand)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,16 +612,15 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
|
||||
if (!converter.inConversionScope(target)) return false
|
||||
|
||||
val canChangeType: Boolean
|
||||
when (target) {
|
||||
val canChangeType: Boolean = when (target) {
|
||||
is PsiLocalVariable -> {
|
||||
if (converter.settings.specifyLocalVariableTypeByDefault) return false
|
||||
canChangeType = codeConverter.canChangeType(target)
|
||||
codeConverter.canChangeType(target)
|
||||
}
|
||||
|
||||
is PsiField -> {
|
||||
if (converter.settings.specifyFieldTypeByDefault) return false
|
||||
canChangeType = target.hasModifierProperty(PsiModifier.PRIVATE)
|
||||
target.hasModifierProperty(PsiModifier.PRIVATE)
|
||||
}
|
||||
|
||||
else -> return false
|
||||
@@ -652,8 +651,8 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
val typeText = castType.type.canonicalText
|
||||
val typeConversion = PRIMITIVE_TYPE_CONVERSIONS[typeText]
|
||||
val operandConverted = codeConverter.convertExpression(operand)
|
||||
if (operandType is PsiPrimitiveType && typeConversion != null) {
|
||||
result = MethodCallExpression.buildNonNull(operandConverted, typeConversion)
|
||||
result = if (operandType is PsiPrimitiveType && typeConversion != null) {
|
||||
MethodCallExpression.buildNonNull(operandConverted, typeConversion)
|
||||
}
|
||||
else {
|
||||
val nullability = if (operandConverted.isNullable && !expression.isQualifier())
|
||||
@@ -661,16 +660,16 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
else
|
||||
Nullability.NotNull
|
||||
val typeConverted = typeConverter.convertType(castType.type, nullability)
|
||||
result = TypeCastExpression(typeConverted, operandConverted)
|
||||
TypeCastExpression(typeConverted, operandConverted)
|
||||
}
|
||||
}
|
||||
|
||||
private fun PsiExpression.isQualifier(): Boolean {
|
||||
val parent = parent
|
||||
when (parent) {
|
||||
is PsiParenthesizedExpression -> return parent.isQualifier()
|
||||
is PsiReferenceExpression -> return this == parent.qualifierExpression
|
||||
else -> return false
|
||||
return when (parent) {
|
||||
is PsiParenthesizedExpression -> parent.isQualifier()
|
||||
is PsiReferenceExpression -> this == parent.qualifierExpression
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -876,12 +875,12 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
|
||||
val lambdaExpression = LambdaExpression(lambdaParameterList, Block.of(statement).assignNoPrototype()).assignNoPrototype()
|
||||
|
||||
if (isKotlinFunctionType) {
|
||||
result = lambdaExpression
|
||||
result = if (isKotlinFunctionType) {
|
||||
lambdaExpression
|
||||
}
|
||||
else {
|
||||
val convertedFunctionalType = converter.typeConverter.convertType(functionalType)
|
||||
result = MethodCallExpression.buildNonNull(
|
||||
MethodCallExpression.buildNonNull(
|
||||
null,
|
||||
convertedFunctionalType.canonicalCode(),
|
||||
ArgumentList.withNoPrototype(lambdaExpression)
|
||||
|
||||
@@ -60,16 +60,16 @@ class ForConverter(
|
||||
|
||||
val continueConverted = this@ForConverter.codeConverter.convertStatement(statement)
|
||||
val statements = listOf(updateConverted, continueConverted)
|
||||
if (statement.parent is PsiCodeBlock) {
|
||||
return if (statement.parent is PsiCodeBlock) {
|
||||
// generate fictive statement which will generate multiple statements
|
||||
return object : Statement() {
|
||||
object : Statement() {
|
||||
override fun generateCode(builder: CodeBuilder) {
|
||||
builder.append(statements, "\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Block.of(statements)
|
||||
Block.of(statements)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -307,11 +307,11 @@ enum class SpecialMethod(val qualifiedClassName: String?, val methodName: String
|
||||
= super.matches(method, superMethodsSearcher) && method.parameterList.let { it.parametersCount == 2 && it.parameters.last().isVarArgs }
|
||||
|
||||
override fun ConvertCallData.convertCall(): Expression? {
|
||||
if (arguments.size == 2 && arguments.last().isAssignableToCharSequenceArray()) {
|
||||
return STRING_JOIN.convertCall(this)
|
||||
return if (arguments.size == 2 && arguments.last().isAssignableToCharSequenceArray()) {
|
||||
STRING_JOIN.convertCall(this)
|
||||
}
|
||||
else {
|
||||
return MethodCallExpression.buildNonNull(
|
||||
MethodCallExpression.buildNonNull(
|
||||
MethodCallExpression.buildNonNull(null, "arrayOf", ArgumentList.withNoPrototype(codeConverter.convertExpressionsInList(arguments.drop(1)))).assignNoPrototype(),
|
||||
"joinToString",
|
||||
ArgumentList.withNoPrototype(codeConverter.convertExpressionsInList(arguments.take(1)))
|
||||
|
||||
@@ -53,13 +53,13 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
||||
override fun visitAssertStatement(statement: PsiAssertStatement) {
|
||||
val descriptionExpr = statement.assertDescription
|
||||
val condition = codeConverter.convertExpression(statement.assertCondition)
|
||||
if (descriptionExpr == null) {
|
||||
result = MethodCallExpression.buildNonNull(null, "assert", ArgumentList.withNoPrototype(condition))
|
||||
result = if (descriptionExpr == null) {
|
||||
MethodCallExpression.buildNonNull(null, "assert", ArgumentList.withNoPrototype(condition))
|
||||
}
|
||||
else {
|
||||
val description = codeConverter.convertExpression(descriptionExpr)
|
||||
val lambda = LambdaExpression(null, Block.of(description).assignNoPrototype())
|
||||
result = MethodCallExpression.buildNonNull(null, "assert", ArgumentList.withNoPrototype(condition, lambda))
|
||||
MethodCallExpression.buildNonNull(null, "assert", ArgumentList.withNoPrototype(condition, lambda))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,20 +69,20 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
||||
}
|
||||
|
||||
override fun visitBreakStatement(statement: PsiBreakStatement) {
|
||||
if (statement.labelIdentifier == null) {
|
||||
result = BreakStatement(Identifier.Empty)
|
||||
result = if (statement.labelIdentifier == null) {
|
||||
BreakStatement(Identifier.Empty)
|
||||
}
|
||||
else {
|
||||
result = BreakStatement(converter.convertIdentifier(statement.labelIdentifier))
|
||||
BreakStatement(converter.convertIdentifier(statement.labelIdentifier))
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitContinueStatement(statement: PsiContinueStatement) {
|
||||
if (statement.labelIdentifier == null) {
|
||||
result = ContinueStatement(Identifier.Empty)
|
||||
result = if (statement.labelIdentifier == null) {
|
||||
ContinueStatement(Identifier.Empty)
|
||||
}
|
||||
else {
|
||||
result = ContinueStatement(converter.convertIdentifier(statement.labelIdentifier))
|
||||
ContinueStatement(converter.convertIdentifier(statement.labelIdentifier))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,12 +139,13 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
||||
override fun visitLabeledStatement(statement: PsiLabeledStatement) {
|
||||
val statementConverted = codeConverter.convertStatement(statement.statement)
|
||||
val identifier = converter.convertIdentifier(statement.labelIdentifier)
|
||||
if (statementConverted is ForConverter.WhileWithInitializationPseudoStatement) { // special case - if our loop gets converted to while with initialization we should move the label to the loop
|
||||
result = if (statementConverted is ForConverter.WhileWithInitializationPseudoStatement) {
|
||||
// special case - if our loop gets converted to while with initialization we should move the label to the loop
|
||||
val labeledLoop = LabeledStatement(identifier, statementConverted.loop).assignPrototype(statement)
|
||||
result = ForConverter.WhileWithInitializationPseudoStatement(statementConverted.initialization, labeledLoop, statementConverted.kind)
|
||||
ForConverter.WhileWithInitializationPseudoStatement(statementConverted.initialization, labeledLoop, statementConverted.kind)
|
||||
}
|
||||
else {
|
||||
result = LabeledStatement(identifier, statementConverted)
|
||||
LabeledStatement(identifier, statementConverted)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,11 +83,11 @@ class TypeVisitor(
|
||||
}
|
||||
|
||||
private fun convertTypeArgs(classType: PsiClassType): List<Type> {
|
||||
if (classType.parameterCount == 0) {
|
||||
return createTypeArgsForRawTypeUsage(classType, Mutability.Default)
|
||||
return if (classType.parameterCount == 0) {
|
||||
createTypeArgsForRawTypeUsage(classType, Mutability.Default)
|
||||
}
|
||||
else {
|
||||
return typeConverter.convertTypes(classType.parameters)
|
||||
typeConverter.convertTypes(classType.parameters)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,16 +111,16 @@ fun PsiExpressionList.lPar(): PsiElement? = node.findChildByType(JavaTokenType.L
|
||||
fun PsiExpressionList.rPar(): PsiElement? = node.findChildByType(JavaTokenType.RPARENTH)?.psi
|
||||
|
||||
fun PsiMember.isImported(file: PsiJavaFile): Boolean {
|
||||
if (this is PsiClass) {
|
||||
return if (this is PsiClass) {
|
||||
val fqName = qualifiedName
|
||||
val index = fqName?.lastIndexOf('.') ?: -1
|
||||
val parentName = if (index >= 0) fqName!!.substring(0, index) else null
|
||||
return file.importList?.allImportStatements?.any {
|
||||
file.importList?.allImportStatements?.any {
|
||||
it.importReference?.qualifiedName == (if (it.isOnDemand) parentName else fqName)
|
||||
} ?: false
|
||||
}
|
||||
else {
|
||||
return containingClass != null && file.importList?.importStaticStatements?.any {
|
||||
containingClass != null && file.importList?.importStaticStatements?.any {
|
||||
it.resolveTargetClass() == containingClass && (it.isOnDemand || it.referenceName == name)
|
||||
} ?: false
|
||||
}
|
||||
|
||||
@@ -70,16 +70,16 @@ private fun Converter.convertImport(fqName: FqName, ref: PsiJavaCodeReferenceEle
|
||||
|
||||
//TODO: how to detect compiled Kotlin here?
|
||||
val target = ref.resolve()
|
||||
if (isImportStatic) {
|
||||
return if (isImportStatic) {
|
||||
if (isOnDemand) {
|
||||
return convertStaticImportOnDemand(fqName, target)
|
||||
convertStaticImportOnDemand(fqName, target)
|
||||
}
|
||||
else {
|
||||
return convertStaticExplicitImport(fqName, target)
|
||||
convertStaticExplicitImport(fqName, target)
|
||||
}
|
||||
}
|
||||
else {
|
||||
return convertNonStaticImport(fqName, isOnDemand, target)
|
||||
convertNonStaticImport(fqName, isOnDemand, target)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,15 +32,15 @@ class MemberIntoObjectProcessing(private val member: PsiMember, private val obje
|
||||
val refExpr = reference.element as? PsiReferenceExpression ?: return null
|
||||
val qualifier = refExpr.qualifierExpression
|
||||
val factory = PsiElementFactory.SERVICE.getInstance(member.project)
|
||||
if (qualifier != null) {
|
||||
return if (qualifier != null) {
|
||||
val newQualifier = factory.createExpressionFromText(qualifier.text + "." + objectName, null)
|
||||
qualifier.replace(newQualifier)
|
||||
return arrayOf(reference)
|
||||
arrayOf(reference)
|
||||
}
|
||||
else {
|
||||
var qualifiedExpr = factory.createExpressionFromText(objectName + "." + refExpr.text, null) as PsiReferenceExpression
|
||||
qualifiedExpr = refExpr.replace(qualifiedExpr) as PsiReferenceExpression
|
||||
return arrayOf(qualifiedExpr)
|
||||
arrayOf(qualifiedExpr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user