FIR: introduce Java type resolve and JavaSymbolProvider #KT-24098 Fixed

This commit is contained in:
Simon Ogorodnik
2018-04-05 12:47:28 +03:00
committed by Mikhail Glukhikh
parent 5c53bdb142
commit 02bedeca05
13 changed files with 144 additions and 11 deletions
+20
View File
@@ -0,0 +1,20 @@
plugins {
kotlin("jvm")
id("jps-compatible")
}
jvmTarget = "1.6"
dependencies {
compile(project(":compiler:frontend.java"))
compile(project(":compiler:fir:resolve"))
compileOnly(intellijCoreDep()) { includeJars("intellij-core", "annotations") }
}
sourceSets {
"main" { projectDefault() }
"test" {}
}
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.fir.java
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.fir.java.symbols.JavaClassSymbol
import org.jetbrains.kotlin.fir.resolve.FirSymbolProvider
import org.jetbrains.kotlin.fir.symbols.ConeSymbol
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade
class JavaSymbolProvider(val project: Project) : FirSymbolProvider {
// TODO: Concrete scope here
private val allScope = GlobalSearchScope.allScope(project)
private val classCache = mutableMapOf<ClassId, ConeSymbol?>()
private val packageCache = mutableMapOf<FqName, FqName?>()
private inline fun <K, V : Any?> MutableMap<K, V>.lookupCacheOrCalculate(key: K, l: (K) -> V): V? {
return if (key in this.keys) {
this[key]
} else {
val calculated = l(key)
this[key] = calculated
calculated
}
}
override fun getSymbolByFqName(classId: ClassId): ConeSymbol? {
return classCache.lookupCacheOrCalculate(classId) {
val facade = KotlinJavaPsiFacade.getInstance(project)
val foundClass = facade.findClass(classId, allScope)
foundClass?.let { javaClass -> JavaClassSymbol(javaClass) }
}
}
override fun getPackage(fqName: FqName): FqName? {
return packageCache.lookupCacheOrCalculate(fqName) {
val facade = KotlinJavaPsiFacade.getInstance(project)
val javaPackage = facade.findPackage(fqName.asString(), allScope) ?: return@lookupCacheOrCalculate null
FqName(javaPackage.qualifiedName)
}
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.fir.java.symbols
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.symbols.ConeClassSymbol
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterSymbol
import org.jetbrains.kotlin.fir.types.ConeClassErrorType
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.load.java.structure.JavaClass
import org.jetbrains.kotlin.load.java.structure.classId
import org.jetbrains.kotlin.name.ClassId
class JavaClassSymbol(javaClass: JavaClass) : ConeClassSymbol {
override val classId: ClassId = javaClass.classId ?: error("!")
override val typeParameters: List<ConeTypeParameterSymbol> =
javaClass.typeParameters.map { JavaTypeParameterSymbol(it.name) }
override val kind: ClassKind = when {
javaClass.isEnum -> ClassKind.ENUM_CLASS
javaClass.isInterface -> ClassKind.INTERFACE
javaClass.isAnnotationType -> ClassKind.ANNOTATION_CLASS
else -> ClassKind.CLASS
}
override val superTypes: List<ConeClassLikeType> =
listOf(ConeClassErrorType("Not supported: Java class supertypes"))
}
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.fir.java.symbols
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterSymbol
import org.jetbrains.kotlin.name.Name
class JavaTypeParameterSymbol(override val name: Name) : ConeTypeParameterSymbol