Skip to content

Commit 2553513

Browse files
committed
Kapt3: Allow users to disable error type correction (and now it's disabled by default)
1 parent 297f8d4 commit 2553513

File tree

12 files changed

+118
-11
lines changed

12 files changed

+118
-11
lines changed

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/Kapt3KotlinGradleSubplugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
186186
}
187187

188188
pluginOptions += SubpluginOption("useLightAnalysis", "${kaptExtension.useLightAnalysis}")
189+
pluginOptions += SubpluginOption("correctErrorTypes", "${kaptExtension.correctErrorTypes}")
189190
pluginOptions += SubpluginOption("stubs", getKaptStubsDir(project, sourceSetName).canonicalPath)
190191

191192
if (project.hasProperty(VERBOSE_OPTION_NAME) && project.property(VERBOSE_OPTION_NAME) == "true") {

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KaptExtension.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ open class KaptExtension {
2828

2929
open var useLightAnalysis: Boolean = true
3030

31+
open var correctErrorTypes: Boolean = false
32+
3133
private var closure: Closure<*>? = null
3234

3335
open fun arguments(closure: Closure<*>) {

plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Extension.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ class ClasspathBasedKapt3Extension(
5353
options: Map<String, String>,
5454
aptOnly: Boolean,
5555
val useLightAnalysis: Boolean,
56+
correctErrorTypes: Boolean,
5657
pluginInitializedTime: Long,
5758
logger: KaptLogger
5859
) : AbstractKapt3Extension(compileClasspath, annotationProcessingClasspath, javaSourceRoots, sourcesOutputDir,
59-
classFilesOutputDir, stubsOutputDir, incrementalDataOutputDir, options, aptOnly, pluginInitializedTime, logger) {
60+
classFilesOutputDir, stubsOutputDir, incrementalDataOutputDir, options,
61+
aptOnly, pluginInitializedTime, logger, correctErrorTypes) {
6062
override val analyzePartially: Boolean
6163
get() = useLightAnalysis
6264

@@ -102,7 +104,8 @@ abstract class AbstractKapt3Extension(
102104
val options: Map<String, String>,
103105
val aptOnly: Boolean,
104106
val pluginInitializedTime: Long,
105-
val logger: KaptLogger
107+
val logger: KaptLogger,
108+
val correctErrorTypes: Boolean
106109
) : PartialAnalysisHandlerExtension() {
107110
val compileClasspath = compileClasspath.distinct()
108111
val annotationProcessingClasspath = annotationProcessingClasspath.distinct()
@@ -203,7 +206,8 @@ abstract class AbstractKapt3Extension(
203206
}
204207

205208
private fun generateKotlinSourceStubs(kaptContext: KaptContext, generationState: GenerationState) {
206-
val converter = ClassFileToSourceStubConverter(kaptContext, generationState.typeMapper, generateNonExistentClass = true)
209+
val converter = ClassFileToSourceStubConverter(kaptContext, generationState.typeMapper,
210+
generateNonExistentClass = true, correctErrorTypes = correctErrorTypes)
207211

208212
val (stubGenerationTime, kotlinSourceStubs) = measureTimeMillis {
209213
converter.convert()

plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ object Kapt3ConfigurationKeys {
7474

7575
val USE_LIGHT_ANALYSIS: CompilerConfigurationKey<String> =
7676
CompilerConfigurationKey.create<String>("do not analyze declaration bodies if can")
77+
78+
val CORRECT_ERROR_TYPES: CompilerConfigurationKey<String> =
79+
CompilerConfigurationKey.create<String>("replace error types with ones from the declaration sources")
7780
}
7881

7982
class Kapt3CommandLineProcessor : CommandLineProcessor {
@@ -108,13 +111,17 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
108111

109112
val USE_LIGHT_ANALYSIS_OPTION: CliOption =
110113
CliOption("useLightAnalysis", "true | false", "Do not analyze declaration bodies if can", required = false)
114+
115+
val CORRECT_ERROR_TYPES_OPTION: CliOption =
116+
CliOption("correctErrorTypes", "true | false", "Replace generated or error types with ones from the generated sources", required = false)
111117
}
112118

113119
override val pluginId: String = ANNOTATION_PROCESSING_COMPILER_PLUGIN_ID
114120

115121
override val pluginOptions: Collection<CliOption> =
116122
listOf(SOURCE_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION,
117-
CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION, USE_LIGHT_ANALYSIS_OPTION)
123+
CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION,
124+
USE_LIGHT_ANALYSIS_OPTION, CORRECT_ERROR_TYPES_OPTION)
118125

119126
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
120127
when (option) {
@@ -127,6 +134,7 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
127134
VERBOSE_MODE_OPTION -> configuration.put(Kapt3ConfigurationKeys.VERBOSE_MODE, value)
128135
APT_ONLY_OPTION -> configuration.put(Kapt3ConfigurationKeys.APT_ONLY, value)
129136
USE_LIGHT_ANALYSIS_OPTION -> configuration.put(Kapt3ConfigurationKeys.USE_LIGHT_ANALYSIS, value)
137+
CORRECT_ERROR_TYPES_OPTION -> configuration.put(Kapt3ConfigurationKeys.CORRECT_ERROR_TYPES, value)
130138
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
131139
}
132140
}
@@ -177,12 +185,15 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
177185
val javaSourceRoots = contentRoots.filterIsInstance<JavaSourceRoot>().map { it.file }
178186

179187
val useLightAnalysis = configuration.get(Kapt3ConfigurationKeys.USE_LIGHT_ANALYSIS) == "true"
188+
val correctErrorTypes = configuration.get(Kapt3ConfigurationKeys.CORRECT_ERROR_TYPES) == "true"
180189

181190
Extensions.getRootArea().getExtensionPoint(DefaultErrorMessages.Extension.EP_NAME).registerExtension(DefaultErrorMessagesKapt3())
182191

183192
if (isVerbose) {
184193
logger.info("Kapt3 is enabled.")
185194
logger.info("Do annotation processing only: $isAptOnly")
195+
logger.info("Use light analysis: $useLightAnalysis")
196+
logger.info("Correct error types: $correctErrorTypes")
186197
logger.info("Source output directory: $sourcesOutputDir")
187198
logger.info("Classes output directory: $classFilesOutputDir")
188199
logger.info("Stubs output directory: $stubsOutputDir")
@@ -196,7 +207,7 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
196207
val kapt3AnalysisCompletedHandlerExtension = ClasspathBasedKapt3Extension(
197208
compileClasspath, apClasspath, javaSourceRoots, sourcesOutputDir, classFilesOutputDir,
198209
stubsOutputDir, incrementalDataOutputDir, apOptions,
199-
isAptOnly, useLightAnalysis, System.currentTimeMillis(), logger)
210+
isAptOnly, useLightAnalysis, correctErrorTypes, System.currentTimeMillis(), logger)
200211
AnalysisHandlerExtension.registerExtension(project, kapt3AnalysisCompletedHandlerExtension)
201212
}
202213

plugins/kapt3/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import com.sun.tools.javac.util.List as JavacList
4545
class ClassFileToSourceStubConverter(
4646
val kaptContext: KaptContext,
4747
val typeMapper: KotlinTypeMapper,
48-
val generateNonExistentClass: Boolean
48+
val generateNonExistentClass: Boolean,
49+
val correctErrorTypes: Boolean
4950
) {
5051
private companion object {
5152
private val VISIBILITY_MODIFIERS = (Opcodes.ACC_PUBLIC or Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED).toLong()
@@ -441,6 +442,10 @@ class ClassFileToSourceStubConverter(
441442
ktTypeProvider: () -> KtTypeReference?,
442443
ifNonError: () -> T
443444
): T {
445+
if (!correctErrorTypes) {
446+
return ifNonError()
447+
}
448+
444449
if (type?.containsErrorTypes() ?: false) {
445450
val ktType = ktTypeProvider()
446451
if (ktType != null) {

plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/AbstractKotlinKapt3IntegrationTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ abstract class AbstractKotlinKapt3IntegrationTest : CodegenTestCase() {
161161
stubsOutputDir: File,
162162
incrementalDataOutputDir: File
163163
) : AbstractKapt3Extension(PathUtil.getJdkClassesRoots(), emptyList(), javaSourceRoots, outputDir, outputDir,
164-
stubsOutputDir, incrementalDataOutputDir, options, true, System.currentTimeMillis(), KaptLogger(true)
164+
stubsOutputDir, incrementalDataOutputDir, options, true, System.currentTimeMillis(),
165+
KaptLogger(true), correctErrorTypes = true
165166
) {
166167
internal var savedStubs: String? = null
167168
internal var savedBindings: Map<String, KaptJavaFileObject>? = null

plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/AbstractKotlinKapt3Test.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ abstract class AbstractKotlinKapt3Test : CodegenTestCase() {
6868
}
6969
}
7070

71-
protected fun convert(kaptRunner: KaptContext, typeMapper: KotlinTypeMapper, generateNonExistentClass: Boolean): JavacList<JCCompilationUnit> {
72-
val converter = ClassFileToSourceStubConverter(kaptRunner, typeMapper, generateNonExistentClass)
71+
protected fun convert(
72+
kaptRunner: KaptContext,
73+
typeMapper: KotlinTypeMapper,
74+
generateNonExistentClass: Boolean,
75+
correctErrorTypes: Boolean
76+
): JavacList<JCCompilationUnit> {
77+
val converter = ClassFileToSourceStubConverter(kaptRunner, typeMapper, generateNonExistentClass, correctErrorTypes)
7378
return converter.convert()
7479
}
7580

@@ -85,9 +90,10 @@ abstract class AbstractClassFileToSourceStubConverterTest : AbstractKotlinKapt3T
8590
fun isOptionSet(name: String) = wholeFile.useLines { lines -> lines.any { it.trim() == "// $name" } }
8691

8792
val generateNonExistentClass = isOptionSet("NON_EXISTENT_CLASS")
93+
val correctErrorTypes = isOptionSet("CORRECT_ERROR_TYPES")
8894
val validate = !isOptionSet("NO_VALIDATION")
8995

90-
val javaFiles = convert(kaptRunner, typeMapper, generateNonExistentClass)
96+
val javaFiles = convert(kaptRunner, typeMapper, generateNonExistentClass, correctErrorTypes)
9197

9298
if (validate) kaptRunner.compiler.enterTrees(javaFiles)
9399

@@ -104,7 +110,7 @@ abstract class AbstractClassFileToSourceStubConverterTest : AbstractKotlinKapt3T
104110

105111
abstract class AbstractKotlinKaptContextTest : AbstractKotlinKapt3Test() {
106112
override fun check(kaptRunner: KaptContext, typeMapper: KotlinTypeMapper, txtFile: File, wholeFile: File) {
107-
val compilationUnits = convert(kaptRunner, typeMapper, generateNonExistentClass = false)
113+
val compilationUnits = convert(kaptRunner, typeMapper, generateNonExistentClass = false, correctErrorTypes = true)
108114
val sourceOutputDir = Files.createTempDirectory("kaptRunner").toFile()
109115
try {
110116
kaptRunner.doAnnotationProcessing(emptyList(), listOf(JavaKaptContextTest.simpleProcessor()),

plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public void testNonExistentClassTypesConversion() throws Exception {
204204
doTest(fileName);
205205
}
206206

207+
@TestMetadata("nonExistentClassWIthoutCorrection.kt")
208+
public void testNonExistentClassWIthoutCorrection() throws Exception {
209+
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/testData/converter/nonExistentClassWIthoutCorrection.kt");
210+
doTest(fileName);
211+
}
212+
207213
@TestMetadata("primitiveTypes.kt")
208214
public void testPrimitiveTypes() throws Exception {
209215
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/testData/converter/primitiveTypes.kt");

plugins/kapt3/testData/converter/nonExistentClass.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// CORRECT_ERROR_TYPES
12
// NON_EXISTENT_CLASS
23
// NO_VALIDATION
34

plugins/kapt3/testData/converter/nonExistentClassTypesConversion.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// CORRECT_ERROR_TYPES
12
// NON_EXISTENT_CLASS
23
// NO_VALIDATION
34

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// NON_EXISTENT_CLASS
2+
// NO_VALIDATION
3+
4+
@Suppress("UNRESOLVED_REFERENCE")
5+
object NonExistentType {
6+
val a: ABCDEF? = null
7+
val b: List<ABCDEF>? = null
8+
val c: (ABCDEF) -> Unit = { f -> }
9+
val d: ABCDEF<String, (List<ABCDEF>) -> Unit>? = null
10+
11+
fun a(a: ABCDEF, s: String): ABCDEF {}
12+
fun b(s: String): ABCDEF {}
13+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"})
2+
public final class NonExistentType {
3+
@org.jetbrains.annotations.Nullable()
4+
private static final error.NonExistentClass a = null;
5+
@org.jetbrains.annotations.Nullable()
6+
private static final java.util.List<error.NonExistentClass> b = null;
7+
@org.jetbrains.annotations.NotNull()
8+
private static final kotlin.jvm.functions.Function1<error.NonExistentClass, kotlin.Unit> c = null;
9+
@org.jetbrains.annotations.Nullable()
10+
private static final error.NonExistentClass d = null;
11+
public static final NonExistentType INSTANCE = null;
12+
13+
@org.jetbrains.annotations.Nullable()
14+
public final error.NonExistentClass getA() {
15+
return null;
16+
}
17+
18+
@org.jetbrains.annotations.Nullable()
19+
public final java.util.List<error.NonExistentClass> getB() {
20+
return null;
21+
}
22+
23+
@org.jetbrains.annotations.NotNull()
24+
public final kotlin.jvm.functions.Function1<error.NonExistentClass, kotlin.Unit> getC() {
25+
return null;
26+
}
27+
28+
@org.jetbrains.annotations.Nullable()
29+
public final error.NonExistentClass getD() {
30+
return null;
31+
}
32+
33+
@org.jetbrains.annotations.NotNull()
34+
public final error.NonExistentClass a(@org.jetbrains.annotations.NotNull()
35+
error.NonExistentClass a, @org.jetbrains.annotations.NotNull()
36+
java.lang.String s) {
37+
return null;
38+
}
39+
40+
@org.jetbrains.annotations.NotNull()
41+
public final error.NonExistentClass b(@org.jetbrains.annotations.NotNull()
42+
java.lang.String s) {
43+
return null;
44+
}
45+
46+
private NonExistentType() {
47+
super();
48+
}
49+
}
50+
51+
////////////////////
52+
53+
package error;
54+
55+
public final class NonExistentClass {
56+
}

0 commit comments

Comments
 (0)