Cleanup: apply "cascade if..." inspection (+ some others)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
9c06739594
commit
1d2017b0fc
@@ -345,44 +345,44 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
if (target is KtLightMethod) {
|
||||
val origin = target.kotlinOrigin
|
||||
val isTopLevel = origin?.getStrictParentOfType<KtClassOrObject>() == null
|
||||
if (origin is KtProperty || origin is KtPropertyAccessor || origin is KtParameter) {
|
||||
val property = if (origin is KtPropertyAccessor)
|
||||
origin.parent as KtProperty
|
||||
else
|
||||
origin as KtNamedDeclaration
|
||||
val parameterCount = target.parameterList.parameters.size
|
||||
if (parameterCount == arguments.size) {
|
||||
val propertyName = Identifier.withNoPrototype(property.name!!, isNullable)
|
||||
val isExtension = property.isExtensionDeclaration()
|
||||
val propertyAccess = if (isTopLevel) {
|
||||
if (isExtension)
|
||||
QualifiedExpression(codeConverter.convertExpression(arguments.firstOrNull(), shouldParenthesize = true), propertyName, null).assignNoPrototype()
|
||||
else
|
||||
when (origin) {
|
||||
is KtProperty, is KtPropertyAccessor, is KtParameter -> {
|
||||
val property = if (origin is KtPropertyAccessor)
|
||||
origin.parent as KtProperty
|
||||
else
|
||||
origin as KtNamedDeclaration
|
||||
val parameterCount = target.parameterList.parameters.size
|
||||
if (parameterCount == arguments.size) {
|
||||
val propertyName = Identifier.withNoPrototype(property.name!!, isNullable)
|
||||
val isExtension = property.isExtensionDeclaration()
|
||||
val propertyAccess = if (isTopLevel) {
|
||||
if (isExtension)
|
||||
QualifiedExpression(codeConverter.convertExpression(arguments.firstOrNull(), shouldParenthesize = true), propertyName, null).assignNoPrototype()
|
||||
else
|
||||
propertyName
|
||||
}
|
||||
else if (qualifier != null) {
|
||||
QualifiedExpression(codeConverter.convertExpression(qualifier), propertyName, dot).assignNoPrototype()
|
||||
}
|
||||
else {
|
||||
propertyName
|
||||
}
|
||||
else if (qualifier != null) {
|
||||
QualifiedExpression(codeConverter.convertExpression(qualifier), propertyName, dot).assignNoPrototype()
|
||||
}
|
||||
else {
|
||||
propertyName
|
||||
}
|
||||
|
||||
when(if (isExtension) parameterCount - 1 else parameterCount) {
|
||||
0 /* getter */ -> {
|
||||
result = propertyAccess
|
||||
return
|
||||
}
|
||||
|
||||
1 /* setter */ -> {
|
||||
val argument = codeConverter.convertExpression(arguments[if (isExtension) 1 else 0])
|
||||
result = AssignmentExpression(propertyAccess, argument, Operator.EQ)
|
||||
return
|
||||
when(if (isExtension) parameterCount - 1 else parameterCount) {
|
||||
0 /* getter */ -> {
|
||||
result = propertyAccess
|
||||
return
|
||||
}
|
||||
|
||||
1 /* setter */ -> {
|
||||
val argument = codeConverter.convertExpression(arguments[if (isExtension) 1 else 0])
|
||||
result = AssignmentExpression(propertyAccess, argument, Operator.EQ)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (origin is KtFunction) {
|
||||
if (isTopLevel) {
|
||||
is KtFunction -> if (isTopLevel) {
|
||||
result = if (origin.isExtensionDeclaration()) {
|
||||
val qualifier = codeConverter.convertExpression(arguments.firstOrNull(), shouldParenthesize = true)
|
||||
MethodCallExpression.build(qualifier,
|
||||
@@ -401,27 +401,27 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
else if (origin == null){
|
||||
val resolvedQualifier = (methodExpr.qualifier as? PsiReferenceExpression)?.resolve()
|
||||
if (isFacadeClassFromLibrary(resolvedQualifier)) {
|
||||
result = if (target.isKotlinExtensionFunction()) {
|
||||
val qualifier = codeConverter.convertExpression(arguments.firstOrNull(), shouldParenthesize = true)
|
||||
MethodCallExpression.build(qualifier,
|
||||
methodExpr.referenceName!!,
|
||||
convertArguments(expression, isExtension = true),
|
||||
typeArguments,
|
||||
isNullable,
|
||||
dot)
|
||||
null -> {
|
||||
val resolvedQualifier = (methodExpr.qualifier as? PsiReferenceExpression)?.resolve()
|
||||
if (isFacadeClassFromLibrary(resolvedQualifier)) {
|
||||
result = if (target.isKotlinExtensionFunction()) {
|
||||
val qualifier = codeConverter.convertExpression(arguments.firstOrNull(), shouldParenthesize = true)
|
||||
MethodCallExpression.build(qualifier,
|
||||
methodExpr.referenceName!!,
|
||||
convertArguments(expression, isExtension = true),
|
||||
typeArguments,
|
||||
isNullable,
|
||||
dot)
|
||||
}
|
||||
else {
|
||||
MethodCallExpression.build(null,
|
||||
methodExpr.referenceName!!,
|
||||
convertArguments(expression),
|
||||
typeArguments,
|
||||
isNullable)
|
||||
}
|
||||
return
|
||||
}
|
||||
else {
|
||||
MethodCallExpression.build(null,
|
||||
methodExpr.referenceName!!,
|
||||
convertArguments(expression),
|
||||
typeArguments,
|
||||
isNullable)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -826,39 +826,40 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
|
||||
|
||||
val specialMethod = method?.let { SpecialMethod.match(it, callParams.size, converter.services) }
|
||||
val statement: Statement
|
||||
if (expression.isConstructor) {
|
||||
val argumentList = ArgumentList.withNoPrototype(callParams.map { it.first })
|
||||
statement = MethodCallExpression.buildNonNull(null, convertMethodReferenceQualifier(qualifier), argumentList)
|
||||
}
|
||||
else if (specialMethod != null) {
|
||||
val factory = PsiElementFactory.SERVICE.getInstance(converter.project)
|
||||
val fakeReceiver = receiver?.let {
|
||||
val psiExpression = qualifier as? PsiExpression ?: factory.createExpressionFromText("fakeReceiver", null)
|
||||
psiExpression.convertedExpression = it.first
|
||||
psiExpression
|
||||
when {
|
||||
expression.isConstructor -> {
|
||||
val argumentList = ArgumentList.withNoPrototype(callParams.map { it.first })
|
||||
statement = MethodCallExpression.buildNonNull(null, convertMethodReferenceQualifier(qualifier), argumentList)
|
||||
}
|
||||
val fakeParams = callParams.mapIndexed {
|
||||
i, param ->
|
||||
with(factory.createExpressionFromText("fake$i", null)) {
|
||||
this.convertedExpression = param.first
|
||||
this
|
||||
specialMethod != null -> {
|
||||
val factory = PsiElementFactory.SERVICE.getInstance(converter.project)
|
||||
val fakeReceiver = receiver?.let {
|
||||
val psiExpression = qualifier as? PsiExpression ?: factory.createExpressionFromText("fakeReceiver", null)
|
||||
psiExpression.convertedExpression = it.first
|
||||
psiExpression
|
||||
}
|
||||
}
|
||||
val patchedConverter = codeConverter.withSpecialExpressionConverter(object : SpecialExpressionConverter {
|
||||
override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? {
|
||||
val convertedExpression = expression.convertedExpression
|
||||
expression.convertedExpression = null
|
||||
return convertedExpression
|
||||
val fakeParams = callParams.mapIndexed { i, param ->
|
||||
with(factory.createExpressionFromText("fake$i", null)) {
|
||||
this.convertedExpression = param.first
|
||||
this
|
||||
}
|
||||
}
|
||||
})
|
||||
val patchedConverter = codeConverter.withSpecialExpressionConverter(object : SpecialExpressionConverter {
|
||||
override fun convertExpression(expression: PsiExpression, codeConverter: CodeConverter): Expression? {
|
||||
val convertedExpression = expression.convertedExpression
|
||||
expression.convertedExpression = null
|
||||
return convertedExpression
|
||||
}
|
||||
})
|
||||
|
||||
val callData = SpecialMethod.ConvertCallData(fakeReceiver, fakeParams, emptyList(), null, null, null, patchedConverter)
|
||||
statement = specialMethod.convertCall(callData)!!
|
||||
}
|
||||
else {
|
||||
val referenceName = expression.referenceName!!
|
||||
val argumentList = ArgumentList.withNoPrototype(callParams.map { it.first })
|
||||
statement = MethodCallExpression.buildNonNull(receiver?.first, referenceName, argumentList)
|
||||
val callData = SpecialMethod.ConvertCallData(fakeReceiver, fakeParams, emptyList(), null, null, null, patchedConverter)
|
||||
statement = specialMethod.convertCall(callData)!!
|
||||
}
|
||||
else -> {
|
||||
val referenceName = expression.referenceName!!
|
||||
val argumentList = ArgumentList.withNoPrototype(callParams.map { it.first })
|
||||
statement = MethodCallExpression.buildNonNull(receiver?.first, referenceName, argumentList)
|
||||
}
|
||||
}
|
||||
|
||||
statement.assignNoPrototype()
|
||||
|
||||
@@ -104,13 +104,14 @@ class ForConverter(
|
||||
statement.isInSingleLine()).assignNoPrototype()
|
||||
if (initializationConverted.isEmpty) return whileStatement
|
||||
|
||||
val kind = if (statement.parents.filter { it !is PsiLabeledStatement }.first() !is PsiCodeBlock) {
|
||||
WhileWithInitializationPseudoStatement.Kind.WITH_BLOCK
|
||||
val kind = when {
|
||||
statement.parents.filter { it !is PsiLabeledStatement }.first() !is PsiCodeBlock ->
|
||||
WhileWithInitializationPseudoStatement.Kind.WITH_BLOCK
|
||||
hasNameConflict() ->
|
||||
WhileWithInitializationPseudoStatement.Kind.WITH_RUN_BLOCK
|
||||
else ->
|
||||
WhileWithInitializationPseudoStatement.Kind.SIMPLE
|
||||
}
|
||||
else if (hasNameConflict())
|
||||
WhileWithInitializationPseudoStatement.Kind.WITH_RUN_BLOCK
|
||||
else
|
||||
WhileWithInitializationPseudoStatement.Kind.SIMPLE
|
||||
return WhileWithInitializationPseudoStatement(initializationConverted, whileStatement, kind)
|
||||
}
|
||||
|
||||
|
||||
@@ -266,12 +266,14 @@ class TypeConverter(val converter: Converter) {
|
||||
|
||||
override fun fromAnnotations(owner: PsiModifierListOwner): Nullability {
|
||||
val manager = NullableNotNullManager.getInstance(owner.project)
|
||||
return if (manager.isNotNull(owner, false/* we do not check bases because they are checked by callers of this method*/))
|
||||
Nullability.NotNull
|
||||
else if (manager.isNullable(owner, false))
|
||||
Nullability.Nullable
|
||||
else
|
||||
Nullability.Default
|
||||
return when {
|
||||
manager.isNotNull(owner, false/* we do not check bases because they are checked by callers of this method*/) ->
|
||||
Nullability.NotNull
|
||||
manager.isNullable(owner, false) ->
|
||||
Nullability.Nullable
|
||||
else ->
|
||||
Nullability.Default
|
||||
}
|
||||
}
|
||||
|
||||
override fun forVariableTypeBeforeUsageSearch(variable: PsiVariable): Nullability {
|
||||
|
||||
@@ -40,17 +40,11 @@ class TypeVisitor(
|
||||
|
||||
override fun visitPrimitiveType(primitiveType: PsiPrimitiveType): Type {
|
||||
val name = primitiveType.canonicalText
|
||||
return if (name == "void") {
|
||||
UnitType()
|
||||
}
|
||||
else if (PRIMITIVE_TYPES_NAMES.contains(name)) {
|
||||
PrimitiveType(Identifier.withNoPrototype(StringUtil.capitalize(name)))
|
||||
}
|
||||
else if (name == "null") {
|
||||
NullType()
|
||||
}
|
||||
else {
|
||||
PrimitiveType(Identifier.withNoPrototype(name))
|
||||
return when {
|
||||
name == "void" -> UnitType()
|
||||
PRIMITIVE_TYPES_NAMES.contains(name) -> PrimitiveType(Identifier.withNoPrototype(StringUtil.capitalize(name)))
|
||||
name == "null" -> NullType()
|
||||
else -> PrimitiveType(Identifier.withNoPrototype(name))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,20 +27,16 @@ fun quoteKeywords(packageName: String): String = packageName.split('.').joinToSt
|
||||
|
||||
fun getDefaultInitializer(property: Property): Expression? {
|
||||
val t = property.type
|
||||
val result = if (t.isNullable) {
|
||||
LiteralExpression("null")
|
||||
}
|
||||
else if (t is PrimitiveType) {
|
||||
when (t.name.name) {
|
||||
val result = when {
|
||||
t.isNullable -> LiteralExpression("null")
|
||||
t is PrimitiveType -> when (t.name.name) {
|
||||
"Boolean" -> LiteralExpression("false")
|
||||
"Char" -> LiteralExpression("' '")
|
||||
"Double" -> MethodCallExpression.buildNonNull(LiteralExpression("0").assignNoPrototype(), OperatorConventions.DOUBLE.toString())
|
||||
"Float" -> MethodCallExpression.buildNonNull(LiteralExpression("0").assignNoPrototype(), OperatorConventions.FLOAT.toString())
|
||||
else -> LiteralExpression("0")
|
||||
}
|
||||
}
|
||||
else {
|
||||
null
|
||||
else -> null
|
||||
}
|
||||
return result?.assignNoPrototype()
|
||||
}
|
||||
|
||||
@@ -279,11 +279,10 @@ class ClassLiteralExpression(val type: Type): Expression() {
|
||||
|
||||
fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Expression>, needExplicitType: Boolean = true) : MethodCallExpression {
|
||||
val elementType = arrayType.elementType
|
||||
val createArrayFunction = if (elementType is PrimitiveType)
|
||||
(elementType.toNotNullType().canonicalCode() + "ArrayOf").decapitalize()
|
||||
else if (needExplicitType)
|
||||
"arrayOf<" + arrayType.elementType.canonicalCode() + ">"
|
||||
else
|
||||
"arrayOf"
|
||||
val createArrayFunction = when {
|
||||
elementType is PrimitiveType -> (elementType.toNotNullType().canonicalCode() + "ArrayOf").decapitalize()
|
||||
needExplicitType -> "arrayOf<" + arrayType.elementType.canonicalCode() + ">"
|
||||
else -> "arrayOf"
|
||||
}
|
||||
return MethodCallExpression.buildNonNull(null, createArrayFunction, ArgumentList.withNoPrototype(initializers))
|
||||
}
|
||||
|
||||
@@ -387,14 +387,10 @@ private class PropertyDetector(
|
||||
modifiers.add(Modifier.OVERRIDE)
|
||||
}
|
||||
|
||||
if (getMethod != null) {
|
||||
modifiers.addIfNotNull(getterModifiers.accessModifier())
|
||||
}
|
||||
else if (setMethod != null) {
|
||||
modifiers.addIfNotNull(getterModifiers.accessModifier())
|
||||
}
|
||||
else {
|
||||
modifiers.addIfNotNull(fieldModifiers.accessModifier())
|
||||
when {
|
||||
getMethod != null -> modifiers.addIfNotNull(getterModifiers.accessModifier())
|
||||
setMethod != null -> modifiers.addIfNotNull(getterModifiers.accessModifier())
|
||||
else -> modifiers.addIfNotNull(fieldModifiers.accessModifier())
|
||||
}
|
||||
|
||||
val prototypes = listOfNotNull<PsiElement>(field, getMethod, setMethod)
|
||||
@@ -432,16 +428,20 @@ private class PropertyDetector(
|
||||
val superMethod = converter.services.superMethodsSearcher.findDeepestSuperMethods(getOrSetMethod).firstOrNull() ?: return null
|
||||
|
||||
val containingClass = superMethod.containingClass!!
|
||||
if (converter.inConversionScope(containingClass)) {
|
||||
val propertyInfo = converter.propertyDetectionCache[containingClass][superMethod]
|
||||
return if (propertyInfo != null) SuperInfo.Property(propertyInfo.isVar, propertyInfo.name, propertyInfo.modifiers.contains(Modifier.ABSTRACT)) else SuperInfo.Function
|
||||
}
|
||||
else if (superMethod is KtLightMethod) {
|
||||
val origin = superMethod.kotlinOrigin
|
||||
return if (origin is KtProperty) SuperInfo.Property(origin.isVar, origin.name ?: "", origin.hasModifier(KtTokens.ABSTRACT_KEYWORD)) else SuperInfo.Function
|
||||
}
|
||||
else {
|
||||
return SuperInfo.Function
|
||||
return when {
|
||||
converter.inConversionScope(containingClass) -> {
|
||||
val propertyInfo = converter.propertyDetectionCache[containingClass][superMethod]
|
||||
if (propertyInfo != null) SuperInfo.Property(propertyInfo.isVar, propertyInfo.name, propertyInfo.modifiers.contains(Modifier.ABSTRACT))
|
||||
else SuperInfo.Function
|
||||
}
|
||||
superMethod is KtLightMethod -> {
|
||||
val origin = superMethod.kotlinOrigin
|
||||
if (origin is KtProperty) SuperInfo.Property(origin.isVar, origin.name ?: "", origin.hasModifier(KtTokens.ABSTRACT_KEYWORD))
|
||||
else SuperInfo.Function
|
||||
}
|
||||
else -> {
|
||||
SuperInfo.Function
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +37,11 @@ class FieldToPropertyProcessing(
|
||||
if (field.name != propertyName || replaceReadWithFieldReference || replaceWriteWithFieldReference) MyConvertedCodeProcessor() else null
|
||||
|
||||
override var javaCodeProcessors =
|
||||
if (field.hasModifierProperty(PsiModifier.PRIVATE))
|
||||
emptyList()
|
||||
else if (field.name != propertyName)
|
||||
listOf(ElementRenamedCodeProcessor(propertyName), UseAccessorsJavaCodeProcessor())
|
||||
else
|
||||
listOf(UseAccessorsJavaCodeProcessor())
|
||||
when {
|
||||
field.hasModifierProperty(PsiModifier.PRIVATE) -> emptyList()
|
||||
field.name != propertyName -> listOf(ElementRenamedCodeProcessor(propertyName), UseAccessorsJavaCodeProcessor())
|
||||
else -> listOf(UseAccessorsJavaCodeProcessor())
|
||||
}
|
||||
|
||||
override val kotlinCodeProcessors =
|
||||
if (field.name != propertyName)
|
||||
|
||||
Reference in New Issue
Block a user