Add prefix information for Java source root (KT-9167 in progress)
#KT-9167 In Progress
This commit is contained in:
committed by
Nikolay Krasko
parent
77138ecdb7
commit
c0739ef53c
+5
-4
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.cli.common.modules
|
||||
|
||||
import org.jetbrains.kotlin.modules.Module
|
||||
import org.jetbrains.kotlin.modules.JavaRootPath
|
||||
import java.util.*
|
||||
|
||||
public class ModuleBuilder(
|
||||
@@ -26,7 +27,7 @@ public class ModuleBuilder(
|
||||
) : Module {
|
||||
private val sourceFiles = ArrayList<String>()
|
||||
private val classpathRoots = ArrayList<String>()
|
||||
private val javaSourceRoots = ArrayList<String>()
|
||||
private val javaSourceRoots = ArrayList<JavaRootPath>()
|
||||
|
||||
public fun addSourceFiles(pattern: String) {
|
||||
sourceFiles.add(pattern)
|
||||
@@ -36,12 +37,12 @@ public class ModuleBuilder(
|
||||
classpathRoots.add(name)
|
||||
}
|
||||
|
||||
public fun addJavaSourceRoot(name: String) {
|
||||
javaSourceRoots.add(name)
|
||||
public fun addJavaSourceRoot(rootPath: JavaRootPath) {
|
||||
javaSourceRoots.add(rootPath)
|
||||
}
|
||||
|
||||
override fun getOutputDirectory(): String = outputDir
|
||||
override fun getJavaSourceRoots(): List<String> = javaSourceRoots
|
||||
override fun getJavaSourceRoots(): List<JavaRootPath> = javaSourceRoots
|
||||
override fun getSourceFiles(): List<String> = sourceFiles
|
||||
override fun getClasspathRoots(): List<String> = classpathRoots
|
||||
override fun getModuleName(): String = name
|
||||
|
||||
+11
-1
@@ -19,9 +19,11 @@ package org.jetbrains.kotlin.cli.common.modules;
|
||||
import com.intellij.openapi.util.io.StreamUtil;
|
||||
import com.intellij.util.SmartList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil;
|
||||
import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil;
|
||||
import org.jetbrains.kotlin.modules.JavaRootPath;
|
||||
import org.jetbrains.kotlin.modules.Module;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
@@ -47,6 +49,7 @@ public class ModuleXmlParser {
|
||||
public static final String OUTPUT_DIR = "outputDir";
|
||||
public static final String SOURCES = "sources";
|
||||
public static final String JAVA_SOURCE_ROOTS = "javaSourceRoots";
|
||||
public static final String JAVA_SOURCE_PACKAGE_PREFIX = "packagePrefix";
|
||||
public static final String PATH = "path";
|
||||
public static final String CLASSPATH = "classpath";
|
||||
|
||||
@@ -165,7 +168,8 @@ public class ModuleXmlParser {
|
||||
}
|
||||
else if (JAVA_SOURCE_ROOTS.equalsIgnoreCase(qName)) {
|
||||
String path = getAttribute(attributes, PATH, qName);
|
||||
moduleBuilder.addJavaSourceRoot(path);
|
||||
String packagePrefix = getNullableAttribute(attributes, JAVA_SOURCE_PACKAGE_PREFIX);
|
||||
moduleBuilder.addJavaSourceRoot(new JavaRootPath(path, packagePrefix));
|
||||
}
|
||||
else {
|
||||
throw createError(qName);
|
||||
@@ -189,6 +193,12 @@ public class ModuleXmlParser {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getNullableAttribute(Attributes attributes, String qName) throws SAXException {
|
||||
return attributes.getValue(qName);
|
||||
}
|
||||
|
||||
|
||||
private static SAXException createError(String qName) throws SAXException {
|
||||
return new SAXException("Unexpected tag: " + qName);
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -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) }
|
||||
Reference in New Issue
Block a user