New J2K: don't allow open modifier on top-level/private declarations

This commit is contained in:
Ilya Kirillov
2020-03-27 16:34:01 +03:00
parent 4d4aa88534
commit b3e728f4fb
10 changed files with 37 additions and 1 deletions
@@ -177,6 +177,7 @@ private val removeRedundantElementsProcessingGroup =
RemoveExplicitTypeArgumentsProcessing(), RemoveExplicitTypeArgumentsProcessing(),
RemoveJavaStreamsCollectCallTypeArgumentsProcessing(), RemoveJavaStreamsCollectCallTypeArgumentsProcessing(),
ExplicitThisInspectionBasedProcessing(), ExplicitThisInspectionBasedProcessing(),
RemoveOpenModifierOnTopLevelDeclarationsProcessing(),
intentionBasedProcessing(RemoveEmptyClassBodyIntention()) intentionBasedProcessing(RemoveEmptyClassBodyIntention())
) )
) )
@@ -503,4 +503,16 @@ class MoveLambdaOutsideParenthesesProcessing :
override fun apply(element: KtCallExpression) { override fun apply(element: KtCallExpression) {
element.moveFunctionLiteralOutsideParentheses() element.moveFunctionLiteralOutsideParentheses()
} }
} }
class RemoveOpenModifierOnTopLevelDeclarationsProcessing :
InspectionLikeProcessingForElement<KtDeclaration>(KtDeclaration::class.java) {
override fun isApplicableTo(element: KtDeclaration, settings: ConverterSettings?): Boolean =
element.hasModifier(KtTokens.OPEN_KEYWORD)
&& (element is KtFunction || element is KtProperty)
&& element.parent is KtFile
override fun apply(element: KtDeclaration) {
element.removeModifier(KtTokens.OPEN_KEYWORD)
}
}
@@ -41,6 +41,9 @@ class ModalityConversion(context: NewJ2kConverterContext) : RecursiveApplicableC
val psi = method.psi<PsiMethod>() ?: return val psi = method.psi<PsiMethod>() ?: return
val containingClass = method.parentOfType<JKClass>() ?: return val containingClass = method.parentOfType<JKClass>() ?: return
when { when {
method.visibility == Visibility.PRIVATE -> {
method.modality = Modality.FINAL
}
method.modality != Modality.ABSTRACT method.modality != Modality.ABSTRACT
&& psi.findSuperMethods().isNotEmpty() -> { && psi.findSuperMethods().isNotEmpty() -> {
method.modality = Modality.FINAL method.modality = Modality.FINAL
@@ -0,0 +1 @@
fun x() {}
@@ -0,0 +1,3 @@
class A {
<selection>void x() {}</selection>
}
@@ -0,0 +1 @@
<caret>
@@ -0,0 +1 @@
private fun x() {}
@@ -0,0 +1,3 @@
class A {
<selection>private void x() {}</selection>
}
@@ -0,0 +1 @@
<caret>
@@ -183,6 +183,16 @@ public class NewJavaToKotlinCopyPasteConversionTestGenerated extends AbstractNew
runTest("nj2k/testData/copyPaste/OnlyQualifier.java"); runTest("nj2k/testData/copyPaste/OnlyQualifier.java");
} }
@TestMetadata("OpenPublicFunctionToTopLevel.java")
public void testOpenPublicFunctionToTopLevel() throws Exception {
runTest("nj2k/testData/copyPaste/OpenPublicFunctionToTopLevel.java");
}
@TestMetadata("PrivateFunctionToTopLevel.java")
public void testPrivateFunctionToTopLevel() throws Exception {
runTest("nj2k/testData/copyPaste/PrivateFunctionToTopLevel.java");
}
@TestMetadata("RawTypeRef.java") @TestMetadata("RawTypeRef.java")
public void testRawTypeRef() throws Exception { public void testRawTypeRef() throws Exception {
runTest("nj2k/testData/copyPaste/RawTypeRef.java"); runTest("nj2k/testData/copyPaste/RawTypeRef.java");