Add prefix information for Java source root (KT-9167 in progress)

#KT-9167 In Progress
This commit is contained in:
Nikolay Krasko
2015-11-08 19:54:46 +03:00
committed by Nikolay Krasko
parent 77138ecdb7
commit c0739ef53c
12 changed files with 99 additions and 32 deletions
@@ -24,7 +24,7 @@ import java.util.ArrayList
import java.util.EnumSet
import java.util.HashMap
public data class JavaRoot(public val file: VirtualFile, public val type: JavaRoot.RootType) {
public data class JavaRoot(public val file: VirtualFile, public val type: JavaRoot.RootType, public val prefixFqName: FqName? = null) {
public enum class RootType {
SOURCE,
BINARY
@@ -45,7 +45,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
private val roots: List<JavaRoot> by lazy { _roots.toList() }
private val maxIndex: Int
get() = roots.size()
get() = roots.size
// each "Cache" object corresponds to a package
private class Cache {
@@ -112,10 +112,12 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
if (request !is FindClassRequest || lastClassSearch == null) {
return doSearch()
}
val (cachedRequest, cachedResult) = lastClassSearch!!
if (cachedRequest.classId != request.classId) {
return doSearch()
}
when (cachedResult) {
is SearchResult.NotFound -> {
val limitedRootTypes = request.acceptedRootTypes.toHashSet()
@@ -165,7 +167,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
}
// a list of package sub names, ["org", "jb", "kotlin"]
val packagesPath = request.packageFqName.pathSegments().map { it.getIdentifier() }
val packagesPath = request.packageFqName.pathSegments().map { it.identifier }
// a list of caches corresponding to packages, [default, "org", "org.jb", "org.jb.kotlin"]
val caches = cachesPath(packagesPath)
@@ -190,6 +192,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
}
processedRootsUpTo = cache.rootIndices.lastOrNull() ?: processedRootsUpTo
}
return notFound()
}
@@ -197,7 +200,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
// possibly filling "Cache" objects with new information
private fun travelPath(rootIndex: Int, packagesPath: List<String>, fillCachesAfter: Int, cachesPath: List<Cache>): VirtualFile? {
if (rootIndex >= maxIndex) {
for (i in (fillCachesAfter + 1)..cachesPath.size() - 1) {
for (i in (fillCachesAfter + 1)..(cachesPath.size - 1)) {
// we all know roots that contain this package by now
cachesPath[i].rootIndices.add(maxIndex)
cachesPath[i].rootIndices.trimToSize()
@@ -215,6 +218,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
cachesPath[correspondingCacheIndex].rootIndices.add(rootIndex)
}
}
return currentFile
}
@@ -231,7 +235,7 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
private data class FindClassRequest(val classId: ClassId, override val acceptedRootTypes: Set<JavaRoot.RootType>) : SearchRequest {
override val packageFqName: FqName
get() = classId.getPackageFqName()
get() = classId.packageFqName
}
private data class TraverseRequest(
@@ -251,5 +255,5 @@ public class JvmDependenciesIndex(_roots: List<JavaRoot>) {
}
}
private fun IntArrayList.lastOrNull() = if (isEmpty()) null else get(size() - 1)
private val IntArrayList.indices: IntRange get() = 0..size()-1
private fun IntArrayList.lastOrNull() = if (isEmpty) null else get(size() - 1)
private val IntArrayList.indices: IntRange get() = 0..(size() - 1)
@@ -75,6 +75,8 @@ import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinderFactory
import org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.isValidJavaFqName
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.parsing.KotlinScriptDefinitionProvider
import org.jetbrains.kotlin.psi.KtFile
@@ -167,12 +169,24 @@ public class KotlinCoreEnvironment private constructor(
val virtualFile = contentRootToVirtualFile(javaRoot) ?: continue
projectEnvironment.addSourcesToClasspath(virtualFile)
val prefixPackageFqName = (javaRoot as? JavaSourceRoot)?.packagePrefix?.let {
if (isValidJavaFqName(it)) {
FqName(it)
}
else {
report(WARNING, "Invalid package prefix name is ignored: $it")
null
}
}
val rootType = when (javaRoot) {
is JavaSourceRoot -> JavaRoot.RootType.SOURCE
is JvmClasspathRoot -> JavaRoot.RootType.BINARY
else -> throw IllegalStateException()
}
javaRoots.add(JavaRoot(virtualFile, rootType))
javaRoots.add(JavaRoot(virtualFile, rootType, prefixPackageFqName))
}
}
@@ -49,6 +49,7 @@ import org.jetbrains.kotlin.idea.MainFunctionDetector;
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager;
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache;
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents;
import org.jetbrains.kotlin.modules.JavaRootPath;
import org.jetbrains.kotlin.modules.Module;
import org.jetbrains.kotlin.modules.TargetId;
import org.jetbrains.kotlin.modules.TargetIdKt;
@@ -183,8 +184,8 @@ public class KotlinToJVMBytecodeCompiler {
}
for (Module module : chunk) {
for (String javaSourceRoot : module.getJavaSourceRoots()) {
JvmContentRootsKt.addJavaSourceRoot(configuration, new File(javaSourceRoot));
for (JavaRootPath javaRootPath : module.getJavaSourceRoots()) {
JvmContentRootsKt.addJavaSourceRoot(configuration, new File(javaRootPath.getPath()), javaRootPath.getPackagePrefix());
}
}
@@ -23,7 +23,7 @@ import java.io.File
public data class JvmClasspathRoot(public override val file: File): JvmContentRoot
public data class JavaSourceRoot(public override val file: File): JvmContentRoot
public data class JavaSourceRoot(public override val file: File, public val packagePrefix: String?): JvmContentRoot
public interface JvmContentRoot : ContentRoot {
public val file: File
@@ -40,8 +40,10 @@ public val CompilerConfiguration.jvmClasspathRoots: List<File>
return get(CommonConfigurationKeys.CONTENT_ROOTS)?.filterIsInstance<JvmClasspathRoot>()?.map { it.file } ?: emptyList()
}
public fun CompilerConfiguration.addJavaSourceRoot(file: File) {
add(CommonConfigurationKeys.CONTENT_ROOTS, JavaSourceRoot(file))
@JvmOverloads
public fun CompilerConfiguration.addJavaSourceRoot(file: File, packagePrefix: String? = null) {
add(CommonConfigurationKeys.CONTENT_ROOTS, JavaSourceRoot(file, packagePrefix))
}
public fun CompilerConfiguration.addJavaSourceRoots(files: List<File>): Unit = files.forEach { addJavaSourceRoot(it) }
@JvmOverloads
public fun CompilerConfiguration.addJavaSourceRoots(files: List<File>, packagePrefix: String? = null): Unit = files.forEach { addJavaSourceRoot(it, packagePrefix) }