From 37ee81dfa10a689ac2849afa557467d62cd9bbf4 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 25 Sep 2019 18:33:47 +0300 Subject: [PATCH] New J2K: use Java classes for instance checking instead of Kotlin ones --- .../inspectionLikePostProcessings.kt | 10 +-- .../processings/inspeсtionLikeProcessings.kt | 61 +++++++++---------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/inspectionLikePostProcessings.kt b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/inspectionLikePostProcessings.kt index 97ec72ace90..bbeded84136 100644 --- a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/inspectionLikePostProcessings.kt +++ b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/inspectionLikePostProcessings.kt @@ -124,20 +124,20 @@ abstract class InspectionLikeProcessing { open val writeActionNeeded = true } -abstract class InspectionLikeProcessingForElement(private val classTag: KClass) : InspectionLikeProcessing() { +abstract class InspectionLikeProcessingForElement(private val classTag: Class) : InspectionLikeProcessing() { protected abstract fun isApplicableTo(element: E, settings: ConverterSettings?): Boolean protected abstract fun apply(element: E) @Suppress("UNCHECKED_CAST") final override fun isApplicableToElement(element: PsiElement, settings: ConverterSettings?): Boolean { - if (!element::class.isSubclassOf(classTag)) return false + if (!classTag.isInstance(element)) return false if (!element.isValid) return false @Suppress("UNCHECKED_CAST") return isApplicableTo(element as E, settings) } final override fun applyToElement(element: PsiElement) { - if (!element::class.isSubclassOf(classTag)) return + if (!classTag.isInstance(element)) return if (!element.isValid) return @Suppress("UNCHECKED_CAST") apply(element as E) } @@ -148,7 +148,7 @@ inline fun > intentio intention: I, writeActionNeeded: Boolean = true, noinline additionalChecker: (E) -> Boolean = { true } -) = object : InspectionLikeProcessingForElement(E::class) { +) = object : InspectionLikeProcessingForElement(E::class.java) { override fun isApplicableTo(element: E, settings: ConverterSettings?): Boolean = intention.applicabilityRange(element) != null && additionalChecker(element) @@ -164,7 +164,7 @@ inline fun > intentio inline fun > inspectionBasedProcessing( inspection: I, writeActionNeeded: Boolean = true -) = object : InspectionLikeProcessingForElement(E::class) { +) = object : InspectionLikeProcessingForElement(E::class.java) { override fun isApplicableTo(element: E, settings: ConverterSettings?): Boolean = inspection.isApplicable(element) diff --git a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/processings/inspeсtionLikeProcessings.kt b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/processings/inspeсtionLikeProcessings.kt index 4fbc93a3b15..20e5d92248f 100644 --- a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/processings/inspeсtionLikeProcessings.kt +++ b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/postProcessing/processings/inspeсtionLikeProcessings.kt @@ -37,11 +37,10 @@ import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.isNullable -import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs -class RemoveExplicitPropertyTypeProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class RemoveExplicitPropertyTypeProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean { val needFieldTypes = settings?.specifyFieldTypeByDefault == true val needLocalVariablesTypes = settings?.specifyLocalVariableTypeByDefault == true @@ -60,7 +59,7 @@ class RemoveExplicitPropertyTypeProcessing : InspectionLikeProcessingForElement< } } -class RemoveRedundantNullabilityProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class RemoveRedundantNullabilityProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean { if (!element.isLocal) return false val typeReference = element.typeReference @@ -86,7 +85,7 @@ class RemoveRedundantNullabilityProcessing : InspectionLikeProcessingForElement< } } -class RemoveExplicitTypeArgumentsProcessing : InspectionLikeProcessingForElement(KtTypeArgumentList::class) { +class RemoveExplicitTypeArgumentsProcessing : InspectionLikeProcessingForElement(KtTypeArgumentList::class.java) { override fun isApplicableTo(element: KtTypeArgumentList, settings: ConverterSettings?): Boolean = RemoveExplicitTypeArgumentsIntention.isApplicableTo(element, approximateFlexible = true) @@ -98,7 +97,7 @@ class RemoveExplicitTypeArgumentsProcessing : InspectionLikeProcessingForElement // the types arguments for Stream.collect calls cannot be explicitly specified in Kotlin // but we need them in nullability inference, so we remove it here class RemoveJavaStreamsCollectCallTypeArgumentsProcessing : - InspectionLikeProcessingForElement(KtCallExpression::class) { + InspectionLikeProcessingForElement(KtCallExpression::class.java) { override fun isApplicableTo(element: KtCallExpression, settings: ConverterSettings?): Boolean { if (element.typeArgumentList == null) return false if (element.callName() != COLLECT_FQ_NAME.shortName().identifier) return false @@ -116,7 +115,7 @@ class RemoveJavaStreamsCollectCallTypeArgumentsProcessing : class RemoveRedundantOverrideVisibilityProcessing : - InspectionLikeProcessingForElement(KtCallableDeclaration::class) { + InspectionLikeProcessingForElement(KtCallableDeclaration::class.java) { override fun isApplicableTo(element: KtCallableDeclaration, settings: ConverterSettings?): Boolean { if (!element.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return false @@ -130,7 +129,7 @@ class RemoveRedundantOverrideVisibilityProcessing : } class ReplaceGetterBodyWithSingleReturnStatementWithExpressionBody : - InspectionLikeProcessingForElement(KtPropertyAccessor::class) { + InspectionLikeProcessingForElement(KtPropertyAccessor::class.java) { private fun KtPropertyAccessor.singleBodyStatementExpression() = bodyBlockExpression?.statements @@ -156,7 +155,7 @@ class ReplaceGetterBodyWithSingleReturnStatementWithExpressionBody : } class RemoveRedundantCastToNullableProcessing : - InspectionLikeProcessingForElement(KtBinaryExpressionWithTypeRHS::class) { + InspectionLikeProcessingForElement(KtBinaryExpressionWithTypeRHS::class.java) { override fun isApplicableTo(element: KtBinaryExpressionWithTypeRHS, settings: ConverterSettings?): Boolean { if (element.right?.typeElement !is KtNullableType) return false @@ -173,7 +172,7 @@ class RemoveRedundantCastToNullableProcessing : } class RemoveRedundantSamAdaptersProcessing : - InspectionLikeProcessingForElement(KtCallExpression::class) { + InspectionLikeProcessingForElement(KtCallExpression::class.java) { override val writeActionNeeded = false override fun isApplicableTo(element: KtCallExpression, settings: ConverterSettings?): Boolean = @@ -190,7 +189,7 @@ class RemoveRedundantSamAdaptersProcessing : } class UninitializedVariableReferenceFromInitializerToThisReferenceProcessing : - InspectionLikeProcessingForElement(KtSimpleNameExpression::class) { + InspectionLikeProcessingForElement(KtSimpleNameExpression::class.java) { override fun isApplicableTo(element: KtSimpleNameExpression, settings: ConverterSettings?): Boolean { val anonymousObject = element.getStrictParentOfType()?.takeIf { it.name == null } ?: return false @@ -211,7 +210,7 @@ class UninitializedVariableReferenceFromInitializerToThisReferenceProcessing : } class UnresolvedVariableReferenceFromInitializerToThisReferenceProcessing : - InspectionLikeProcessingForElement(KtSimpleNameExpression::class) { + InspectionLikeProcessingForElement(KtSimpleNameExpression::class.java) { override fun isApplicableTo(element: KtSimpleNameExpression, settings: ConverterSettings?): Boolean { val anonymousObject = element.getStrictParentOfType() ?: return false @@ -226,7 +225,7 @@ class UnresolvedVariableReferenceFromInitializerToThisReferenceProcessing : } } -class VarToValProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class VarToValProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { private fun KtProperty.hasWriteUsages(): Boolean = ReferencesSearch.search(this, useScope).any { usage -> (usage as? KtSimpleNameReference)?.element?.let { nameReference -> @@ -252,7 +251,7 @@ class VarToValProcessing : InspectionLikeProcessingForElement(KtProp } } -class JavaObjectEqualsToEqOperatorProcessing : InspectionLikeProcessingForElement(KtCallExpression::class) { +class JavaObjectEqualsToEqOperatorProcessing : InspectionLikeProcessingForElement(KtCallExpression::class.java) { companion object { val CALL_FQ_NAME = FqName("java.util.Objects.equals") } @@ -278,7 +277,7 @@ class JavaObjectEqualsToEqOperatorProcessing : InspectionLikeProcessingForElemen class RemoveForExpressionLoopParameterTypeProcessing : - InspectionLikeProcessingForElement(KtForExpression::class) { + InspectionLikeProcessingForElement(KtForExpression::class.java) { override fun isApplicableTo(element: KtForExpression, settings: ConverterSettings?): Boolean = element.loopParameter?.typeReference?.typeElement != null && settings?.specifyLocalVariableTypeByDefault != true @@ -289,7 +288,7 @@ class RemoveForExpressionLoopParameterTypeProcessing : } class RemoveRedundantConstructorKeywordProcessing : - InspectionLikeProcessingForElement(KtPrimaryConstructor::class) { + InspectionLikeProcessingForElement(KtPrimaryConstructor::class.java) { override fun isApplicableTo(element: KtPrimaryConstructor, settings: ConverterSettings?): Boolean = element.containingClassOrObject is KtClass && element.getConstructorKeyword() != null @@ -302,7 +301,7 @@ class RemoveRedundantConstructorKeywordProcessing : } } -class RemoveRedundantModalityModifierProcessing : InspectionLikeProcessingForElement(KtDeclaration::class) { +class RemoveRedundantModalityModifierProcessing : InspectionLikeProcessingForElement(KtDeclaration::class.java) { override fun isApplicableTo(element: KtDeclaration, settings: ConverterSettings?): Boolean { if (element.hasModifier(KtTokens.FINAL_KEYWORD)) { return !element.hasModifier(KtTokens.OVERRIDE_KEYWORD) @@ -317,7 +316,7 @@ class RemoveRedundantModalityModifierProcessing : InspectionLikeProcessingForEle } -class RemoveRedundantVisibilityModifierProcessing : InspectionLikeProcessingForElement(KtDeclaration::class) { +class RemoveRedundantVisibilityModifierProcessing : InspectionLikeProcessingForElement(KtDeclaration::class.java) { override fun isApplicableTo(element: KtDeclaration, settings: ConverterSettings?) = when { element.hasModifier(KtTokens.PUBLIC_KEYWORD) && element.hasModifier(KtTokens.OVERRIDE_KEYWORD) -> false @@ -333,7 +332,7 @@ class RemoveRedundantVisibilityModifierProcessing : InspectionLikeProcessingForE } } -class RemoveExplicitOpenInInterfaceProcessing : InspectionLikeProcessingForElement(KtClass::class) { +class RemoveExplicitOpenInInterfaceProcessing : InspectionLikeProcessingForElement(KtClass::class.java) { override fun isApplicableTo(element: KtClass, settings: ConverterSettings?): Boolean = element.isValid && element.isInterface() @@ -344,7 +343,7 @@ class RemoveExplicitOpenInInterfaceProcessing : InspectionLikeProcessingForEleme } } -class MoveGetterAndSetterAnnotationsToPropertyProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class MoveGetterAndSetterAnnotationsToPropertyProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean = element.accessors.isNotEmpty() @@ -364,7 +363,7 @@ class MoveGetterAndSetterAnnotationsToPropertyProcessing : InspectionLikeProcess } } -class RedundantExplicitTypeInspectionBasedProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class RedundantExplicitTypeInspectionBasedProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean = RedundantExplicitTypeInspection.hasRedundantType(element) @@ -374,7 +373,7 @@ class RedundantExplicitTypeInspectionBasedProcessing : InspectionLikeProcessingF } } -class CanBeValInspectionBasedProcessing : InspectionLikeProcessingForElement(KtDeclaration::class) { +class CanBeValInspectionBasedProcessing : InspectionLikeProcessingForElement(KtDeclaration::class.java) { override fun isApplicableTo(element: KtDeclaration, settings: ConverterSettings?): Boolean = CanBeValInspection.canBeVal(element, ignoreNotUsedVals = false) @@ -385,7 +384,7 @@ class CanBeValInspectionBasedProcessing : InspectionLikeProcessingForElement(KtProperty::class) { +class MayBeConstantInspectionBasedProcessing : InspectionLikeProcessingForElement(KtProperty::class.java) { override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean = with(MayBeConstantInspection) { val status = element.getStatus() @@ -398,7 +397,7 @@ class MayBeConstantInspectionBasedProcessing : InspectionLikeProcessingForElemen } } -class RemoveExplicitUnitTypeProcessing : InspectionLikeProcessingForElement(KtNamedFunction::class) { +class RemoveExplicitUnitTypeProcessing : InspectionLikeProcessingForElement(KtNamedFunction::class.java) { override fun isApplicableTo(element: KtNamedFunction, settings: ConverterSettings?): Boolean { val typeReference = element.typeReference?.typeElement ?: return false if (!typeReference.textMatches("Unit")) return false @@ -412,7 +411,7 @@ class RemoveExplicitUnitTypeProcessing : InspectionLikeProcessingForElement(KtPropertyAccessor::class) { + InspectionLikeProcessingForElement(KtPropertyAccessor::class.java) { override fun isApplicableTo(element: KtPropertyAccessor, settings: ConverterSettings?): Boolean = element.isRedundantGetter() @@ -422,7 +421,7 @@ class RemoveExplicitGetterInspectionBasedProcessing : } class RemoveExplicitSetterInspectionBasedProcessing : - InspectionLikeProcessingForElement(KtPropertyAccessor::class) { + InspectionLikeProcessingForElement(KtPropertyAccessor::class.java) { override fun isApplicableTo(element: KtPropertyAccessor, settings: ConverterSettings?): Boolean = element.isRedundantSetter() @@ -433,7 +432,7 @@ class RemoveExplicitSetterInspectionBasedProcessing : class RedundantSemicolonInspectionBasedProcessing : - InspectionLikeProcessingForElement(PsiElement::class) { + InspectionLikeProcessingForElement(PsiElement::class.java) { override fun isApplicableTo(element: PsiElement, settings: ConverterSettings?): Boolean = element.node.elementType == KtTokens.SEMICOLON && RedundantSemicolonInspection.isRedundantSemicolon(element) @@ -445,7 +444,7 @@ class RedundantSemicolonInspectionBasedProcessing : class RedundantCompanionReferenceInspectionBasedProcessing : - InspectionLikeProcessingForElement(KtReferenceExpression::class) { + InspectionLikeProcessingForElement(KtReferenceExpression::class.java) { override fun isApplicableTo(element: KtReferenceExpression, settings: ConverterSettings?): Boolean = RedundantCompanionReferenceInspection.isRedundantCompanionReference(element) @@ -455,7 +454,7 @@ class RedundantCompanionReferenceInspectionBasedProcessing : } class ExplicitThisInspectionBasedProcessing : - InspectionLikeProcessingForElement(KtExpression::class) { + InspectionLikeProcessingForElement(KtExpression::class.java) { override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean = ExplicitThisInspection.hasExplicitThis(element) @@ -469,7 +468,7 @@ class ExplicitThisInspectionBasedProcessing : } class LiftReturnInspectionBasedProcessing : - InspectionLikeProcessingForElement(KtExpression::class) { + InspectionLikeProcessingForElement(KtExpression::class.java) { override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean = LiftReturnOrAssignmentInspection.getState(element, false)?.any { it.liftType == LiftReturnOrAssignmentInspection.Companion.LiftType.LIFT_RETURN_OUT @@ -481,7 +480,7 @@ class LiftReturnInspectionBasedProcessing : } class LiftAssignmentInspectionBasedProcessing : - InspectionLikeProcessingForElement(KtExpression::class) { + InspectionLikeProcessingForElement(KtExpression::class.java) { override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean = LiftReturnOrAssignmentInspection.getState(element, false)?.any { it.liftType == LiftReturnOrAssignmentInspection.Companion.LiftType.LIFT_ASSIGNMENT_OUT @@ -493,7 +492,7 @@ class LiftAssignmentInspectionBasedProcessing : } class MoveLambdaOutsideParenthesesProcessing : - InspectionLikeProcessingForElement(KtCallExpression::class) { + InspectionLikeProcessingForElement(KtCallExpression::class.java) { override fun isApplicableTo(element: KtCallExpression, settings: ConverterSettings?): Boolean = element.canMoveLambdaOutsideParentheses()