From 46685549497ed40c8d30b45f288b8ce5fc152680 Mon Sep 17 00:00:00 2001 From: Bart van Helvert Date: Tue, 10 Feb 2026 22:28:06 +0100 Subject: [PATCH] [jvm] Don't warn about overrides in future Java versions #IDEA-385156 Fixed GitOrigin-RevId: 598ef1b8b7b1c9a8fe58849792674e0ecc9315d5 --- .../codeInspection/JavaApiUsageInspection.kt | 2 +- .../java/JavaJavaApiUsageInspectionTest.kt | 19 +++++++++++++++ .../kotlin/K1JavaApiUsageInspectionTest.kt | 23 +++++++++++++++++++ .../kotlin/K2JavaApiUsageInspectionTest.kt | 23 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/JavaApiUsageInspection.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/JavaApiUsageInspection.kt index e5d9be4352f8..3e277eb29c8c 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/JavaApiUsageInspection.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/JavaApiUsageInspection.kt @@ -148,7 +148,7 @@ class JavaApiUsageInspection : AbstractBaseUastLocalInspectionTool() { private fun processMethodOverriding(method: UMethod, overriddenMethods: Array) { val overrideAnnotation = method.findAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE) - val hasOverrideModifier = overrideModifierLanguages.any { method.sourcePsi?.language != Language.findLanguageByID(it) } + val hasOverrideModifier = overrideModifierLanguages.contains(method.sourcePsi?.language?.id) if (overrideAnnotation == null && !hasOverrideModifier) return val sourcePsi = method.sourcePsi ?: return val module = ModuleUtilCore.findModuleForPsiElement(sourcePsi) ?: return diff --git a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/JavaJavaApiUsageInspectionTest.kt b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/JavaJavaApiUsageInspectionTest.kt index 4fb877af8e04..31e3b4b25724 100644 --- a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/JavaJavaApiUsageInspectionTest.kt +++ b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/JavaJavaApiUsageInspectionTest.kt @@ -386,4 +386,23 @@ class JavaJavaApiUsageInspectionTest : JavaApiUsageInspectionTestBase() { } """.trimIndent()) } + + fun `test method that will be overridden in a future Java version`() { + myFixture.setLanguageLevel(LanguageLevel.JDK_1_7) + myFixture.testHighlighting(JvmLanguage.JAVA, """ + import java.util.Comparator; + + class MyComparator implements Comparator { + @Override + public int compare(String o1, String o2) { + return 0; + } + + // this method is introduced in Java 8 + public Comparator reversed() { + return this; + } + } + """) + } } \ No newline at end of file diff --git a/jvm/jvm-analysis-kotlin-tests-k1/testSrc/com/intellij/codeInspection/tests/kotlin/K1JavaApiUsageInspectionTest.kt b/jvm/jvm-analysis-kotlin-tests-k1/testSrc/com/intellij/codeInspection/tests/kotlin/K1JavaApiUsageInspectionTest.kt index 23e4ea855059..cd88db5bc400 100644 --- a/jvm/jvm-analysis-kotlin-tests-k1/testSrc/com/intellij/codeInspection/tests/kotlin/K1JavaApiUsageInspectionTest.kt +++ b/jvm/jvm-analysis-kotlin-tests-k1/testSrc/com/intellij/codeInspection/tests/kotlin/K1JavaApiUsageInspectionTest.kt @@ -1,7 +1,30 @@ package com.intellij.codeInspection.tests.kotlin +import com.intellij.jvm.analysis.testFramework.JvmLanguage +import com.intellij.pom.java.LanguageLevel import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginMode class K1JavaApiUsageInspectionTest : KotlinJavaApiUsageInspectionTest() { override val pluginMode: KotlinPluginMode get() = KotlinPluginMode.K1 + + fun `test method that will be overridden in a future Java version`() { + myFixture.setLanguageLevel(LanguageLevel.JDK_1_7) + + // The reversed method declaration is erroneous because there is a missing `override` + // In Java, such code would be fine because overrides are implicit + myFixture.testHighlighting(JvmLanguage.KOTLIN, """ + import java.util.Comparator + + + class MyComparator : Comparator { + override fun compare(p0: String, p1: String): Int { + return 0 + } + + fun reversed(): Comparator { + return this + } + } + """) + } } \ No newline at end of file diff --git a/jvm/jvm-analysis-kotlin-tests-k2/testSrc/com/intellij/codeInspection/tests/kotlin/K2JavaApiUsageInspectionTest.kt b/jvm/jvm-analysis-kotlin-tests-k2/testSrc/com/intellij/codeInspection/tests/kotlin/K2JavaApiUsageInspectionTest.kt index 90884fc84604..d24183bbb57c 100644 --- a/jvm/jvm-analysis-kotlin-tests-k2/testSrc/com/intellij/codeInspection/tests/kotlin/K2JavaApiUsageInspectionTest.kt +++ b/jvm/jvm-analysis-kotlin-tests-k2/testSrc/com/intellij/codeInspection/tests/kotlin/K2JavaApiUsageInspectionTest.kt @@ -1,7 +1,30 @@ package com.intellij.codeInspection.tests.kotlin +import com.intellij.jvm.analysis.testFramework.JvmLanguage +import com.intellij.pom.java.LanguageLevel import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginMode class K2JavaApiUsageInspectionTest : KotlinJavaApiUsageInspectionTest() { override val pluginMode: KotlinPluginMode get() = KotlinPluginMode.K2 + + fun `test method that will be overridden in a future Java version`() { + myFixture.setLanguageLevel(LanguageLevel.JDK_1_7) + + // The reversed method declaration is erroneous because there is a missing `override` + // In Java, such code would be fine because overrides are implicit + myFixture.testHighlighting(JvmLanguage.KOTLIN, """ + import java.util.Comparator + + + class MyComparator : Comparator { + override fun compare(p0: String, p1: String): Int { + return 0 + } + + fun reversed(): Comparator { + return this + } + } + """) + } } \ No newline at end of file