New J2K: do not save import statements in files as we print fqNames for calls

This may cause conflicts on shortening fq references post-processing step
This commit is contained in:
Ilya Kirillov
2019-09-24 11:52:19 +03:00
parent e3b7b41544
commit f9fac0acf5
54 changed files with 54 additions and 162 deletions
@@ -72,7 +72,6 @@ object ConversionsRunner {
BuiltinMembersConversion(context),
ImplicitCastsConversion(context),
LiteralConversion(context),
CollectImportsConversion(context),
FilterImportsConversion(context),
MoveInitBlocksToTheEndConversion(context),
AddElementsInfoConversion(context)
@@ -48,6 +48,7 @@ import org.jetbrains.kotlin.nj2k.types.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.kotlin.utils.addToStdlib.cast
@@ -74,23 +75,39 @@ class JavaToJKTreeBuilder constructor(
private fun PsiJavaFile.toJK(): JKFile =
JKFile(
packageStatement?.toJK() ?: JKPackageDeclaration(JKNameIdentifier("")),
importList.toJK(),
importList.toJK(saveImports = false),
with(declarationMapper) { classes.map { it.toJK() } }
)
private fun PsiImportList?.toJK(): JKImportList =
JKImportList(this?.allImportStatements?.mapNotNull { it.toJK() }.orEmpty())
private fun PsiImportList?.toJK(saveImports: Boolean): JKImportList =
JKImportList(this?.allImportStatements?.mapNotNull { it.toJK(saveImports) }.orEmpty()).also { importList ->
val innerComments = this?.collectDescendantsOfType<PsiComment>()?.map { comment ->
JKCommentElement(comment.text)
}.orEmpty()
importList.leftNonCodeElements += innerComments
}
private fun PsiPackageStatement.toJK(): JKPackageDeclaration =
JKPackageDeclaration(JKNameIdentifier(packageName))
.also {
it.assignNonCodeElements(this)
symbolProvider.provideUniverseSymbol(this, it)
}
private fun PsiImportStatementBase.toJK(): JKImportStatement? {
val target = resolve()
private fun PsiImportStatementBase.toJK(saveImports: Boolean): JKImportStatement? {
val target = when (this) {
is PsiImportStaticStatement -> resolveTargetClass()
else -> resolve()
}
val rawName = (importReference?.canonicalText ?: return null) + if (isOnDemand) ".*" else ""
// We will save only unresolved imports and print all static calls with fqNames
// to avoid name clashes in future
if (!saveImports) {
return if (target == null)
JKImportStatement(JKNameIdentifier(rawName))
else null
}
val name =
target.safeAs<KtLightElement<*, *>>()?.kotlinOrigin?.getKotlinFqName()?.asString()
?: target.safeAs<KtLightClass>()?.containingFile?.safeAs<KtFile>()?.packageFqName?.asString()?.let { "$it.*" }
@@ -944,7 +961,7 @@ class JavaToJKTreeBuilder constructor(
}.last()
fun buildTree(psi: PsiElement): JKTreeRoot? =
fun buildTree(psi: PsiElement, saveImports: Boolean): JKTreeRoot? =
when (psi) {
is PsiJavaFile -> psi.toJK()
is PsiExpression -> with(expressionTreeMapper) { psi.toJK() }
@@ -953,8 +970,8 @@ class JavaToJKTreeBuilder constructor(
is PsiField -> with(declarationMapper) { psi.toJK() }
is PsiMethod -> with(declarationMapper) { psi.toJK() }
is PsiAnnotation -> with(declarationMapper) { psi.toJK() }
is PsiImportList -> psi.toJK()
is PsiImportStatementBase -> psi.toJK()
is PsiImportList -> psi.toJK(saveImports)
is PsiImportStatementBase -> psi.toJK(saveImports)
is PsiJavaCodeReferenceElement ->
if (psi.parent is PsiReferenceList) {
val factory = JavaPsiFacade.getInstance(psi.project).elementFactory
@@ -21,16 +21,12 @@ import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.module.Module
import com.intellij.openapi.progress.EmptyProgressIndicator
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Computable
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiStatement
import com.intellij.psi.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.jetbrains.kotlin.idea.core.util.EDT
@@ -108,9 +104,18 @@ class NewJavaToKotlinConverter(
symbolProvider.preBuildTree(inputElements)
val importStorage = ImportStorage()
val treeBuilder = JavaToJKTreeBuilder(symbolProvider, typeFactory, converterServices, importStorage)
// we want to leave all imports as is in the case when user is converting only imports
val saveImports = inputElements.all { element ->
element is PsiComment || element is PsiWhiteSpace
|| element is PsiImportStatementBase || element is PsiImportList
|| element is PsiPackageStatement
}
val asts = inputElements.mapIndexed { i, element ->
processor.updateState(i, 1, phaseDescription)
element to treeBuilder.buildTree(element)
element to treeBuilder.buildTree(element, saveImports)
}
val context = NewJ2kConverterContext(
@@ -1,48 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.nj2k.conversions
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
import org.jetbrains.kotlin.nj2k.symbols.JKSymbol
import org.jetbrains.kotlin.nj2k.symbols.fqNameToImport
import org.jetbrains.kotlin.nj2k.tree.*
import org.jetbrains.kotlin.nj2k.types.JKClassType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class CollectImportsConversion(context: NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
when (element) {
is JKClassAccessExpression -> addSymbol(element.identifier)
is JKFieldAccessExpression -> addSymbol(element.identifier)
is JKCallExpression -> addSymbol(element.identifier)
is JKAnnotation -> addSymbol(element.classSymbol)
is JKNewExpression -> addSymbol(element.classSymbol)
is JKInheritanceInfo -> {
element.implements
}
is JKTypeElement -> {
element.type.safeAs<JKClassType>()?.also {
addSymbol(it.classReference)
}
}
}
return recurse(element)
}
private fun addSymbol(symbol: JKSymbol) {
symbol.fqNameToImport()?.also { fqName ->
context.importStorage.addImport(FqName(fqName))
}
}
}
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
import org.jetbrains.kotlin.nj2k.tree.JKImportList
import org.jetbrains.kotlin.nj2k.tree.JKTreeElement
class FilterImportsConversion(context : NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
class FilterImportsConversion(context: NewJ2kConverterContext) : RecursiveApplicableConversionBase(context) {
override fun applyToElement(element: JKTreeElement): JKTreeElement {
if (element !is JKImportList) return recurse(element)
element.imports = element.imports.filter { import ->
@@ -1,7 +1,6 @@
package to
import java.io.File
import java.util.ArrayList
class JavaClass {
@@ -1,7 +1,5 @@
package to
import java.util.ArrayList
class JavaClass {
internal fun foo() {
+2 -1
View File
@@ -1,3 +1,4 @@
// ERROR: Unresolved reference: Test
// !forceNotNullTypes: false
// !specifyLocalVariableTypeByDefault: true
package test
@@ -19,7 +20,7 @@ class Test(str: String) {
val test: String = "String2"
sout(test)
sout(dummy(test))
Test(test)
test.Test(test)
}
init {
+2 -1
View File
@@ -1,3 +1,4 @@
// ERROR: Unresolved reference: Test
// !specifyLocalVariableTypeByDefault: true
package test
@@ -16,7 +17,7 @@ class Test(str: String?) {
val test: String = "String2"
sout(test)
sout(dummy(test))
Test(test)
test.Test(test)
}
init {
@@ -1,5 +1,3 @@
import java.util.HashSet
internal class Foo {
fun foo(o: HashSet<*>) {
val o2: HashSet<*> = o
@@ -1,5 +1,3 @@
import java.util.HashSet
internal class Foo {
fun foo(o: HashSet<*>) {
var foo = 0
-2
View File
@@ -1,7 +1,5 @@
package demo
import java.util.HashMap
internal class Test {
constructor() {}
constructor(s: String?) {}
+4 -2
View File
@@ -3,10 +3,12 @@ package test
object Test {
@JvmStatic
fun main(args: Array<String>) {
println()// Comment
println()
// Comment
// Comment1
foo()
// Comment1
// Comment2
.indexOf("s")
}
@@ -1,5 +1,3 @@
import A.Nested
internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD)) {
internal class Nested(p: Int) {
companion object {
@@ -9,5 +7,5 @@ internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD
}
internal class B {
var nested: Nested? = null
var nested: A.Nested? = null
}
@@ -1,7 +1,5 @@
package pack
import pack.A.Nested
internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD)) {
internal class Nested(p: Int) {
companion object {
@@ -11,5 +9,5 @@ internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD
}
internal class B {
var nested: Nested? = null
var nested: A.Nested? = null
}
@@ -1,7 +1,5 @@
package pack
import pack.A.Nested
internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD)) {
internal class Nested(p: Int) {
companion object {
@@ -11,5 +9,5 @@ internal class A @JvmOverloads constructor(nested: Nested? = Nested(Nested.FIELD
}
internal class B {
var nested: Nested? = null
var nested: A.Nested? = null
}
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
private val list1 = ArrayList<String>()
private val list2: MutableList<String> = ArrayList()
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
private val field1: List<String> = ArrayList()
val field2: List<String> = ArrayList()
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class C {
fun foo1(list: MutableList<String?>) {
for (i in list.indices) {
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
internal object Test {
@JvmStatic
fun main(args: Array<String>) {
-1
View File
@@ -1 +0,0 @@
import java.util.Arrays
-1
View File
@@ -1,3 +1,2 @@
// ERROR: Unresolved reference: ArrayBlockingQueue
import java.util.Arrays
import java.util.concurrent.ArrayBlockingQueue
-2
View File
@@ -1,8 +1,6 @@
// ERROR: The integer literal does not conform to the expected type CapturedType(*)
// ERROR: The integer literal does not conform to the expected type CapturedType(*)
// ERROR: Type argument is not within its bounds: should be subtype of 'String?'
import java.util.HashMap
internal class G<T : String?>(t: T)
class Java {
internal fun test() {
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal object A {
@JvmStatic
fun main(args: Array<String>) {
-2
View File
@@ -1,7 +1,5 @@
package test
import java.util.HashMap
class TestPrimitiveFromMap {
fun foo(map: HashMap<String?, Int?>): Int {
return map["zzz"]!!
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
class TestMethodReference {
private val hashMap = HashMap<String, String>()
fun update(key: String, msg: String) {
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
class TestMethodReference {
private val hashMap = HashMap<String, String>()
fun update(key: String, msg: String) {
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal interface FooInterface {
fun foo(): ArrayList<out Foo.SomeClass?>?
}
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
class TestClass {
private val hashMap: Map<String, List<Int>> = HashMap()
}
-1
View File
@@ -1,4 +1,3 @@
import java.util.ArrayList
import java.util.function.Consumer
internal class Test {
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
var list: List<String> = ArrayList()
}
@@ -1,7 +1,7 @@
import kotlinApi.KotlinClass.Companion.CONST
import kotlinApi.KotlinClass
class C {
internal fun bar() {
println(CONST)
println(KotlinClass.CONST)
}
}
-2
View File
@@ -1,6 +1,4 @@
// ERROR: Unresolved reference: LinkedList
import java.util.ArrayList
class ForEach {
fun test() {
val xs = ArrayList<Any>()
-2
View File
@@ -1,6 +1,4 @@
// ERROR: Unresolved reference: LinkedList
import java.util.ArrayList
class Lists {
fun test() {
val xs: MutableList<Any?> = ArrayList()
@@ -1,5 +1,3 @@
import java.util.HashMap
internal enum class E { A, B, C }
internal class A {
fun foo(list: List<String?>, collection: Collection<Int?>, map: Map<Int, Int?>) {
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
fun createCollection(): MutableCollection<String> {
return ArrayList()
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
private val collection: MutableCollection<String>
fun createCollection(): MutableCollection<String> {
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
internal class IteratorTest {
var mutableMap1: MutableMap<String, String> = HashMap()
var mutableMap2: MutableMap<String, String> = HashMap()
-3
View File
@@ -1,8 +1,5 @@
import java.util.ArrayList
class TestMutableCollection {
val list: MutableList<String> = ArrayList()
fun test() {
val it = list.iterator()
while (it.hasNext()) {
@@ -1,5 +1,3 @@
import java.util.ArrayList
class Test {
internal var list: List<MutableList<Int>> = ArrayList()
fun test() {
@@ -1,5 +1,3 @@
import java.util.ArrayList
class Owner {
var list: MutableList<String> = ArrayList()
}
@@ -8,4 +6,4 @@ class Updater {
fun update(owner: Owner) {
owner.list.add("")
}
}
}
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class User {
fun main() {
val list: List<String> = ArrayList()
@@ -1,7 +1,5 @@
package org.test
import java.util.ArrayList
internal class Member
internal class User {
fun main() {
-2
View File
@@ -1,7 +1,5 @@
package test
import java.util.ArrayList
class Test {
private var myProp: String? = null
private var myIntProp: Int? = null
-1
View File
@@ -1,6 +1,5 @@
import javaApi.JavaClass
import kotlinApi.KotlinClass
import java.util.HashMap
internal class X {
operator fun get(index: Int): Int {
@@ -1,5 +1,3 @@
import java.util.HashMap
internal class Test {
fun test(map: HashMap<String?, String?>) {
map.forEach { (key: String?, value: String?) -> foo(key, value) }
-2
View File
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class C<T> {
fun foo1(src: Collection<T>) {
val t = src.iterator().next()
-2
View File
@@ -1,7 +1,5 @@
package demo
import java.util.HashMap
internal class Test {
fun main() {
val commonMap = HashMap<String, Int>()
-2
View File
@@ -1,7 +1,5 @@
package demo
import java.util.ArrayList
internal class Test {
fun main() {
val common: List<String> = ArrayList()
@@ -1,5 +1,3 @@
import java.util.ArrayList
internal class A {
private val field1: List<String> = ArrayList()
val field2: List<String> = ArrayList()
@@ -1,7 +1,5 @@
package demo
import java.util.ArrayList
class TestJava {
fun f(result: Function1<String?, Unit>) {
result.invoke("a")
+1 -3
View File
@@ -1,4 +1,2 @@
import java.util.ArrayList
internal interface I<T : List<Iterator<String?>?>?>
internal class C : I<ArrayList<Iterator<String?>?>?>
internal class C : I<ArrayList<Iterator<String?>?>?>
-2
View File
@@ -1,5 +1,3 @@
import java.util.HashMap
internal class A {
fun foo() {
val map1 = getMap1<String, Int>()
+1 -2
View File
@@ -2,8 +2,7 @@
// ERROR: Type mismatch: inferred type is (CapturedType(*)!!..Any?) but String? was expected
// ERROR: Type mismatch: inferred type is Any? but String? was expected
// ERROR: Type mismatch: inferred type is (CapturedType(*)!!..Any?) but String? was expected
// ERROR: Type mismatch: inferred type is HashMap<Any?, Any?> but Map<String?, String?> was expected
import java.util.HashMap
// ERROR: Type mismatch: inferred type is kotlin.collections.HashMap<Any?, Any?> /* = java.util.HashMap<Any?, Any?> */ but Map<String?, String?> was expected
import java.util.Properties
internal object A {