KT-65404 KAPT should print a warning if stub generation is triggered for an interface with method bodies but without -Xjvm-default=all or -Xjvm-default=all-compatibility
Merge-request: KT-MR-14139 Merged-by: Pavel Mikhailovskii <Pavel.Mikhailovskii@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
d75463a7d5
commit
e363a1228d
+5
-4
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.kapt.cli.test
|
||||
|
||||
import com.intellij.openapi.util.SystemInfo
|
||||
import com.intellij.openapi.util.text.StringUtil.convertLineSeparators
|
||||
import org.jetbrains.kotlin.cli.common.arguments.readArgumentsFromArgFile
|
||||
import org.jetbrains.kotlin.test.services.JUnit5Assertions
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil
|
||||
@@ -53,10 +54,10 @@ abstract class AbstractKaptToolIntegrationTest {
|
||||
"javac" -> runJavac(section.args)
|
||||
"java" -> runJava(section.args)
|
||||
"output" -> {
|
||||
val output = File(tmpdir, "processOutput.txt").readText()
|
||||
val expected = section.content.trim()
|
||||
JUnit5Assertions.assertTrue(output.contains(expected)) {
|
||||
"Output\"$output\" doesn't contain the expected string \"$expected\""
|
||||
val output = convertLineSeparators(File(tmpdir, "processOutput.txt").readText().trim())
|
||||
val expected = convertLineSeparators(section.content.trim())
|
||||
JUnit5Assertions.assertEquals(expected, output) {
|
||||
"Output\"$output\" is different from the expected string \"$expected\""
|
||||
}
|
||||
}
|
||||
"after" -> {}
|
||||
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
interface I1 {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
interface I2 {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class C: I1, I2
|
||||
plugins/kapt3/kapt3-cli/testData/integration-kapt4/defaultMethodsAllCompatibilityNoWarning/build.txt
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
# mkdir
|
||||
output/stubs
|
||||
|
||||
# kapt
|
||||
-Xjvm-default=all-compatibility
|
||||
-language-version 2.0
|
||||
-Xuse-kapt4
|
||||
-Kapt-mode=stubs
|
||||
-Kapt-stubs=output/stubs
|
||||
-Kapt-classes=output/classes
|
||||
-Kapt-sources=output/sources
|
||||
-Kapt-classpath=output/ap
|
||||
-d output/classes
|
||||
-cp %KOTLIN_STDLIB%
|
||||
Test.kt
|
||||
|
||||
# output
|
||||
warning: k2 kapt is an experimental feature. Use with caution.
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
interface I1 {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
interface I2 {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class C: I1, I2
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
# mkdir
|
||||
output/stubs
|
||||
|
||||
# kapt
|
||||
-Xjvm-default=all
|
||||
-language-version 2.0
|
||||
-Xuse-kapt4
|
||||
-Kapt-mode=stubs
|
||||
-Kapt-stubs=output/stubs
|
||||
-Kapt-classes=output/classes
|
||||
-Kapt-sources=output/sources
|
||||
-Kapt-classpath=output/ap
|
||||
-d output/classes
|
||||
-cp %KOTLIN_STDLIB%
|
||||
Test.kt
|
||||
|
||||
# output
|
||||
warning: k2 kapt is an experimental feature. Use with caution.
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
interface I1 {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
interface I2 {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class C: I1, I2
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
# mkdir
|
||||
output/stubs
|
||||
|
||||
# kapt
|
||||
-language-version 2.0
|
||||
-Xuse-kapt4
|
||||
-Kapt-mode=stubs
|
||||
-Kapt-stubs=output/stubs
|
||||
-Kapt-classes=output/classes
|
||||
-Kapt-sources=output/sources
|
||||
-Kapt-classpath=output/ap
|
||||
-d output/classes
|
||||
-cp %KOTLIN_STDLIB%
|
||||
Test.kt
|
||||
|
||||
# output
|
||||
warning: k2 kapt is an experimental feature. Use with caution.
|
||||
warning: [kapt] Support for interface methods with bodies in Kapt requires -Xjvm-default=all or -Xjvm-default=all-compatibility compiler option
|
||||
+18
@@ -42,6 +42,24 @@ public class Kapt4ToolIntegrationTestGenerated extends AbstractKapt4ToolIntegrat
|
||||
runTest("plugins/kapt3/kapt3-cli/testData/integration-kapt4/correctErrorTypesOn/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("defaultMethodsAllCompatibilityNoWarning")
|
||||
public void testDefaultMethodsAllCompatibilityNoWarning() throws Exception {
|
||||
runTest("plugins/kapt3/kapt3-cli/testData/integration-kapt4/defaultMethodsAllCompatibilityNoWarning/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("defaultMethodsAllNoWarning")
|
||||
public void testDefaultMethodsAllNoWarning() throws Exception {
|
||||
runTest("plugins/kapt3/kapt3-cli/testData/integration-kapt4/defaultMethodsAllNoWarning/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("defaultMethodsWarning")
|
||||
public void testDefaultMethodsWarning() throws Exception {
|
||||
runTest("plugins/kapt3/kapt3-cli/testData/integration-kapt4/defaultMethodsWarning/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("defaultPackage")
|
||||
public void testDefaultPackage() throws Exception {
|
||||
|
||||
@@ -10,6 +10,7 @@ package org.jetbrains.kotlin.kapt4
|
||||
import com.google.common.collect.HashMultimap
|
||||
import com.google.common.collect.Multimap
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.lang.jvm.JvmModifier
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.impl.source.tree.LeafPsiElement
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
@@ -25,6 +26,8 @@ import org.jetbrains.kotlin.asJava.elements.*
|
||||
import org.jetbrains.kotlin.asJava.findFacadeClass
|
||||
import org.jetbrains.kotlin.asJava.toLightClass
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.config.JvmAnalysisFlags
|
||||
import org.jetbrains.kotlin.config.JvmDefaultMode
|
||||
import org.jetbrains.kotlin.config.LanguageVersion
|
||||
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils.offsetToLineAndColumn
|
||||
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
||||
@@ -46,6 +49,7 @@ import org.jetbrains.kotlin.utils.toMetadataVersion
|
||||
import org.jetbrains.kotlin.kapt3.base.KaptOptions
|
||||
import org.jetbrains.kotlin.kapt3.base.util.KaptLogger
|
||||
import org.jetbrains.kotlin.kapt3.stubs.MembersPositionComparator
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.psi.psiUtil.children
|
||||
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
@@ -66,8 +70,10 @@ internal fun generateStubs(
|
||||
return emptyMap()
|
||||
}
|
||||
}
|
||||
val jvmDefaultMode = module.languageVersionSettings.getFlag(JvmAnalysisFlags.jvmDefaultMode)
|
||||
return analyze(module) {
|
||||
StubGenerator(files.filterIsInstance<KtFile>(), options, logger, metadataRenderer, overriddenMetadataVersion).generateStubs()
|
||||
StubGenerator(files.filterIsInstance<KtFile>(), options, logger, metadataRenderer, overriddenMetadataVersion, jvmDefaultMode)
|
||||
.generateStubs()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +91,7 @@ private class StubGenerator(
|
||||
private val logger: KaptLogger,
|
||||
private val metadataRenderer: (Printer.(Metadata) -> Unit)? = null,
|
||||
private val overriddenMetadataVersion: BinaryVersion? = null,
|
||||
private val jvmDefaultMode: JvmDefaultMode,
|
||||
) {
|
||||
private val strictMode = options[KaptFlag.STRICT]
|
||||
private val stripMetadata = options[KaptFlag.STRIP_METADATA]
|
||||
@@ -513,6 +520,11 @@ private class StubGenerator(
|
||||
if (!(modifierListOwner is PsiMethod && modifierListOwner.isConstructor && modifierListOwner.containingClass?.isEnum == true) && (modifierListOwner !is PsiEnumConstant)) {
|
||||
for (modifier in PsiModifier.MODIFIERS.filter(modifierListOwner::hasModifierProperty)) {
|
||||
if (modifier == PsiModifier.PRIVATE && (modifierListOwner as? PsiMember)?.containingClass?.isInterface == true) continue
|
||||
|
||||
if (!jvmDefaultMode.isEnabled && modifier == PsiModifier.DEFAULT) {
|
||||
onError("Support for interface methods with bodies in Kapt requires -Xjvm-default=all or -Xjvm-default=all-compatibility compiler option")
|
||||
}
|
||||
|
||||
if ((modifier != PsiModifier.FINAL && modifier != PsiModifier.ABSTRACT) || !(modifierListOwner is PsiClass && modifierListOwner.isEnum)) {
|
||||
printWithNoIndent(modifier, " ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user