Kapt: Prefer non-aliased imports over aliased. Make sure the imported short names are unique (KT-22083)
This commit is contained in:
+12
-4
@@ -172,9 +172,13 @@ class ClassFileToSourceStubConverter(
|
||||
}
|
||||
|
||||
private fun convertImports(file: KtFile, classDeclaration: JCClassDecl): JavacList<JCTree> {
|
||||
val imports = mutableListOf<JCTree>()
|
||||
val imports = mutableListOf<JCTree.JCImport>()
|
||||
val importedShortNames = mutableSetOf<String>()
|
||||
|
||||
for (importDirective in file.importDirectives) {
|
||||
// We prefer ordinary imports over aliased ones.
|
||||
val sortedImportDirectives = file.importDirectives.partition { it.aliasName == null }.run { first + second }
|
||||
|
||||
for (importDirective in sortedImportDirectives) {
|
||||
// Qualified name should be valid Java fq-name
|
||||
val importedFqName = importDirective.importedFqName?.takeIf { it.pathSegments().size > 1 } ?: continue
|
||||
if (!isValidQualifiedName(importedFqName)) continue
|
||||
@@ -193,6 +197,10 @@ class ClassFileToSourceStubConverter(
|
||||
imports += if (importDirective.isAllUnder) {
|
||||
treeMaker.Import(treeMaker.Select(importedExpr, treeMaker.nameTable.names.asterisk), false)
|
||||
} else {
|
||||
if (!importedShortNames.add(importedFqName.shortName().asString())) {
|
||||
continue
|
||||
}
|
||||
|
||||
treeMaker.Import(importedExpr, false)
|
||||
}
|
||||
}
|
||||
@@ -276,7 +284,7 @@ class ClassFileToSourceStubConverter(
|
||||
}?.desc ?: "()Z")
|
||||
|
||||
val args = mapJList(constructorArguments.drop(2)) { convertLiteralExpression(getDefaultValue(it)) }
|
||||
|
||||
|
||||
val def = data.correspondingClass?.let { convertClass(it, lineMappings, packageFqName, false) }
|
||||
|
||||
convertField(data.field, clazz, lineMappings, packageFqName, treeMaker.NewClass(
|
||||
@@ -407,7 +415,7 @@ class ClassFileToSourceStubConverter(
|
||||
ktTypeProvider = {
|
||||
val fieldOrigin = (kaptContext.origins[field]?.element as? KtCallableDeclaration)
|
||||
?.takeIf { it !is KtFunction }
|
||||
|
||||
|
||||
fieldOrigin?.typeReference
|
||||
},
|
||||
ifNonError = { signatureParser.parseFieldSignature(field.signature, treeMaker.Type(type)) })
|
||||
|
||||
+6
@@ -121,6 +121,12 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("importsKt22083.kt")
|
||||
public void testImportsKt22083() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/kapt3-compiler/testData/converter/importsKt22083.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inheritanceSimple.kt")
|
||||
public void testInheritanceSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/kapt3-compiler/testData/converter/inheritanceSimple.kt");
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// CORRECT_ERROR_TYPES
|
||||
// EXPECTED_ERROR(23;24) cannot find symbol
|
||||
|
||||
// FILE: a.kt
|
||||
package test
|
||||
|
||||
import java.io.File
|
||||
import java.io.File as JavaFile
|
||||
import java.io.IOException
|
||||
import java.io.IOException as JavaIOException
|
||||
|
||||
class TestA
|
||||
|
||||
// FILE: lib/File.java
|
||||
package lib;
|
||||
public class File {}
|
||||
|
||||
// FILE: lib/IOException.java
|
||||
package lib;
|
||||
public class IOException {}
|
||||
|
||||
// FILE: b.kt
|
||||
package test
|
||||
|
||||
import java.io.File
|
||||
import lib.File as LibFile
|
||||
import java.io.IOException
|
||||
import lib.IOException as LibIOException
|
||||
|
||||
interface TestB {
|
||||
fun a(): File
|
||||
fun b(): LibFile
|
||||
fun c(): IOException
|
||||
fun d(): LibIOException
|
||||
}
|
||||
|
||||
// FILE: c.kt
|
||||
@file:Suppress("UNRESOLVED_REFERENCE")
|
||||
package test
|
||||
|
||||
import java.io.File as JavaFile
|
||||
import lib.File as LibFile
|
||||
import java.io.IOException as JavaIOException
|
||||
import lib.IOException as LibIOException
|
||||
import lib.FooBar as LibFooBar
|
||||
|
||||
interface TestC {
|
||||
fun a(): JavaFile
|
||||
fun b(): LibFile
|
||||
fun c(): JavaIOException
|
||||
fun d(): LibIOException
|
||||
|
||||
fun e(): LibFooBar
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package lib;
|
||||
|
||||
public class File {
|
||||
|
||||
public File() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
|
||||
package lib;
|
||||
|
||||
public class IOException {
|
||||
|
||||
public IOException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
|
||||
package test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@kotlin.Metadata()
|
||||
public final class TestA {
|
||||
|
||||
public TestA() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
|
||||
package test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@kotlin.Metadata()
|
||||
public abstract interface TestB {
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract java.io.File a();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract lib.File b();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract java.io.IOException c();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract lib.IOException d();
|
||||
}
|
||||
|
||||
////////////////////
|
||||
|
||||
package test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import lib.FooBar;
|
||||
|
||||
@kotlin.Metadata()
|
||||
public abstract interface TestC {
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract java.io.File a();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract lib.File b();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract java.io.IOException c();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract lib.IOException d();
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public abstract lib.FooBar e();
|
||||
}
|
||||
Reference in New Issue
Block a user