forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Support: Inspection for final Spring-annotated classes/functions
#KT-11098 Fixed
- Loading branch information
Showing
30 changed files
with
582 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
idea/idea-ultimate/resources/inspectionDescriptions/KotlinFinalClassOrFunSpring.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
This inspection reports final Kotlin classes and functions annotated with Spring @Component, @Configuration or @Bean annotations | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
<idea-plugin> | ||
<extensions defaultExtensionNs="com.intellij"> | ||
|
||
<localInspection implementationClass="org.jetbrains.kotlin.idea.spring.inspections.KotlinFinalClassOrFunSpringInspection" | ||
displayName="Final Kotlin class or function with Spring annotation" | ||
groupBundle="resources.messages.SpringBundle" | ||
groupKey="inspection.group.code" | ||
groupPath="Spring,Spring Core" | ||
enabledByDefault="true" | ||
level="WARNING"/> | ||
</extensions> | ||
</idea-plugin> |
92 changes: 92 additions & 0 deletions
92
...src/org/jetbrains/kotlin/idea/spring/inspections/KotlinFinalClassOrFunSpringInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2010-2016 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.kotlin.idea.spring.inspections | ||
|
||
import com.intellij.codeInsight.highlighting.HighlightUsagesDescriptionLocation | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.ElementDescriptionUtil | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.spring.constants.SpringAnnotationsConstants | ||
import com.intellij.spring.model.jam.stereotype.SpringComponent | ||
import com.intellij.spring.model.jam.stereotype.SpringConfiguration | ||
import org.jetbrains.kotlin.asJava.toLightClass | ||
import org.jetbrains.kotlin.asJava.toLightMethods | ||
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection | ||
import org.jetbrains.kotlin.idea.spring.isAnnotatedWith | ||
import org.jetbrains.kotlin.lexer.KtTokens | ||
import org.jetbrains.kotlin.psi.* | ||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject | ||
import org.jetbrains.kotlin.psi.psiUtil.isInheritable | ||
import org.jetbrains.kotlin.psi.psiUtil.isOverridable | ||
|
||
class KotlinFinalClassOrFunSpringInspection : AbstractKotlinInspection() { | ||
class QuickFix<T: KtModifierListOwner>(private val element: T) : LocalQuickFix { | ||
override fun getName(): String { | ||
return "Make ${ElementDescriptionUtil.getElementDescription(element, HighlightUsagesDescriptionLocation.INSTANCE)} open" | ||
} | ||
|
||
override fun getFamilyName() = "Make declaration open" | ||
|
||
override fun applyFix(project: Project, problemDescriptor: ProblemDescriptor) { | ||
(element as? KtNamedDeclaration)?.containingClassOrObject?.addModifier(KtTokens.OPEN_KEYWORD) | ||
element.addModifier(KtTokens.OPEN_KEYWORD) | ||
} | ||
} | ||
|
||
private fun getMessage(declaration: KtNamedDeclaration): String? { | ||
when (declaration) { | ||
is KtClass -> { | ||
val lightClass = declaration.toLightClass() ?: return null | ||
when { | ||
SpringConfiguration.META.getJamElement(lightClass) != null -> return "@Configuration class should be declared open" | ||
SpringComponent.META.getJamElement(lightClass) != null -> return "@Component class should be declared open" | ||
} | ||
} | ||
|
||
is KtNamedFunction -> { | ||
val lightMethod = declaration.toLightMethods().firstOrNull() ?: return null | ||
if (lightMethod.isAnnotatedWith(SpringAnnotationsConstants.JAVA_SPRING_BEAN)) return "@Bean function should be declared open" | ||
} | ||
} | ||
return null | ||
} | ||
|
||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object: KtVisitorVoid() { | ||
override fun visitNamedDeclaration(declaration: KtNamedDeclaration) { | ||
when (declaration) { | ||
is KtClass -> if (declaration.isInheritable()) return | ||
is KtNamedFunction -> if (declaration.isOverridable()) return | ||
else -> return | ||
} | ||
|
||
val message = getMessage(declaration) ?: return | ||
|
||
holder.registerProblem( | ||
declaration.nameIdentifier ?: declaration, | ||
message, | ||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, | ||
QuickFix(declaration) | ||
) | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
idea/idea-ultimate/src/org/jetbrains/kotlin/idea/spring/springUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2010-2016 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.kotlin.idea.spring | ||
|
||
import com.intellij.codeInsight.AnnotationUtil | ||
import com.intellij.openapi.module.ModuleUtilCore | ||
import com.intellij.psi.PsiModifierListOwner | ||
import com.intellij.spring.model.jam.utils.JamAnnotationTypeUtil | ||
|
||
internal fun PsiModifierListOwner.isAnnotatedWith(annotationFqName: String): Boolean { | ||
val module = ModuleUtilCore.findModuleForPsiElement(this) ?: return false | ||
return JamAnnotationTypeUtil.getInstance(module) | ||
.getAnnotationTypesWithChildren(annotationFqName) | ||
.mapNotNull { it.qualifiedName } | ||
.any { AnnotationUtil.isAnnotated(this, it, true) } | ||
} |
43 changes: 43 additions & 0 deletions
43
...ultimate/tests/org/jetbrains/kotlin/idea/codeInsight/UltimateInspectionTestGenerated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2010-2016 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.kotlin.idea.codeInsight; | ||
|
||
import com.intellij.testFramework.TestDataPath; | ||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; | ||
import org.jetbrains.kotlin.test.KotlinTestUtils; | ||
import org.jetbrains.kotlin.test.TestMetadata; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
|
||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ | ||
@SuppressWarnings("all") | ||
@TestMetadata("idea/testData/ultimateInspections") | ||
@TestDataPath("$PROJECT_ROOT") | ||
@RunWith(JUnit3RunnerWithInners.class) | ||
public class UltimateInspectionTestGenerated extends AbstractInspectionTest { | ||
public void testAllFilesPresentInUltimateInspections() throws Exception { | ||
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/ultimateInspections"), Pattern.compile("^(inspections\\.test)$")); | ||
} | ||
|
||
@TestMetadata("spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test") | ||
public void testSpring_finalSpringAnnotatedDeclaration_inspectionData_Inspections_test() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateInspections/spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test"); | ||
doTest(fileName); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...idea-ultimate/tests/org/jetbrains/kotlin/idea/quickfix/UltimateQuickFixTestGenerated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2010-2016 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.kotlin.idea.quickfix; | ||
|
||
import com.intellij.testFramework.TestDataPath; | ||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; | ||
import org.jetbrains.kotlin.test.KotlinTestUtils; | ||
import org.jetbrains.kotlin.test.TestMetadata; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
|
||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ | ||
@SuppressWarnings("all") | ||
@TestMetadata("idea/testData/ultimateQuickFixes") | ||
@TestDataPath("$PROJECT_ROOT") | ||
@RunWith(JUnit3RunnerWithInners.class) | ||
public class UltimateQuickFixTestGenerated extends AbstractQuickFixTest { | ||
public void testAllFilesPresentInUltimateQuickFixes() throws Exception { | ||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/ultimateQuickFixes"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); | ||
} | ||
|
||
@TestMetadata("idea/testData/ultimateQuickFixes/spring") | ||
@TestDataPath("$PROJECT_ROOT") | ||
@RunWith(JUnit3RunnerWithInners.class) | ||
public static class Spring extends AbstractQuickFixTest { | ||
public void testAllFilesPresentInSpring() throws Exception { | ||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/ultimateQuickFixes/spring"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); | ||
} | ||
|
||
@TestMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration") | ||
@TestDataPath("$PROJECT_ROOT") | ||
@RunWith(JUnit3RunnerWithInners.class) | ||
public static class FinalSpringAnnotatedDeclaration extends AbstractQuickFixTest { | ||
public void testAllFilesPresentInFinalSpringAnnotatedDeclaration() throws Exception { | ||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); | ||
} | ||
|
||
@TestMetadata("classWithComponentRuntime.kt") | ||
public void testClassWithComponentRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/classWithComponentRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("classWithConfigurationRuntime.kt") | ||
public void testClassWithConfigurationRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/classWithConfigurationRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("classWithCustomConfigurationRuntime.kt") | ||
public void testClassWithCustomConfigurationRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/classWithCustomConfigurationRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("funWithBeanFinalClassRuntime.kt") | ||
public void testFunWithBeanFinalClassRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/funWithBeanFinalClassRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("funWithBeanOpenClassRuntime.kt") | ||
public void testFunWithBeanOpenClassRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/funWithBeanOpenClassRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("funWithCustomBeanFinalClassRuntime.kt") | ||
public void testFunWithCustomBeanFinalClassRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/funWithCustomBeanFinalClassRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
|
||
@TestMetadata("funWithCustomBeanOpenClassRuntime.kt") | ||
public void testFunWithCustomBeanOpenClassRuntime() throws Exception { | ||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/ultimateQuickFixes/spring/finalSpringAnnotatedDeclaration/funWithCustomBeanOpenClassRuntime.kt"); | ||
doTest(fileName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.