Add -Xuse-old-class-files-reading CLI argument

By default we use the fast implementation in CLI compiler,
but in the most of the tests the old one is enabled

Also add tests on CompiledJava with the fast class reading
implementation
This commit is contained in:
Denis Zharkov
2017-04-20 16:38:16 +03:00
parent af202ef5bc
commit a5c352dc9f
13 changed files with 1824 additions and 11 deletions
@@ -107,6 +107,13 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
@Argument(value = "-Xskip-runtime-version-check", description = "Allow Kotlin runtime libraries of incompatible versions in the classpath")
public boolean skipRuntimeVersionCheck;
@Argument(
value = "-Xuse-old-class-files-reading",
description = "Use old class files reading implementation " +
"(may slow down the build and should be used in case of problems with the new implementation)"
)
public boolean useOldClassFilesReading;
@Argument(
value = "-Xdump-declarations-to",
valueDescription = "<path>",
@@ -308,6 +308,13 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize)
configuration.put(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, arguments.inheritMultifileParts)
configuration.put(JVMConfigurationKeys.SKIP_RUNTIME_VERSION_CHECK, arguments.skipRuntimeVersionCheck)
configuration.put(JVMConfigurationKeys.USE_FAST_CLASS_FILES_READING, !arguments.useOldClassFilesReading)
if (arguments.useOldClassFilesReading) {
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
.report(INFO, "Using the old java class files reading implementation")
}
configuration.put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
configuration.put(CLIConfigurationKeys.REPORT_PERF, arguments.reportPerf)
configuration.put(JVMConfigurationKeys.USE_SINGLE_MODULE, arguments.singleModule)
@@ -47,9 +47,11 @@ class KotlinCliJavaFileManagerImpl(private val myPsiManager: PsiManager) : CoreJ
private var index: JvmDependenciesIndex by Delegates.notNull()
private val topLevelClassesCache: MutableMap<FqName, VirtualFile?> = THashMap()
private val allScope = GlobalSearchScope.allScope(myPsiManager.project)
private var useFastClassFilesReading = false
fun initIndex(packagesCache: JvmDependenciesIndex) {
fun initialize(packagesCache: JvmDependenciesIndex, useFastClassFilesReading: Boolean) {
this.index = packagesCache
this.useFastClassFilesReading = useFastClassFilesReading
}
private fun findPsiClass(classId: ClassId, searchScope: GlobalSearchScope): PsiClass? = perfCounter.time {
@@ -72,7 +74,7 @@ class KotlinCliJavaFileManagerImpl(private val myPsiManager: PsiManager) : CoreJ
override fun findClass(classId: ClassId, searchScope: GlobalSearchScope): JavaClass? {
val virtualFile = findVirtualFileForTopLevelClass(classId, searchScope) ?: return null
if (virtualFile.extension == "class") {
if (useFastClassFilesReading && virtualFile.extension == "class") {
// We return all class files' names in the directory in knownClassNamesInPackage method, so one may request an inner class
return binaryCache.getOrPut(classId) {
// Note that currently we implicitly suppose that searchScope for binary classes is constant and we do not use it
@@ -209,8 +209,8 @@ class KotlinCoreEnvironment private constructor(
rootsIndex = indexFactory.makeIndexFor(initialRoots)
updateClasspathFromRootsIndex(rootsIndex)
(ServiceManager.getService(project, CoreJavaFileManager::class.java)
as KotlinCliJavaFileManagerImpl).initIndex(rootsIndex)
(ServiceManager.getService(project, CoreJavaFileManager::class.java) as KotlinCliJavaFileManagerImpl)
.initialize(rootsIndex, configuration.getBoolean(JVMConfigurationKeys.USE_FAST_CLASS_FILES_READING))
val finderFactory = CliVirtualFileFinderFactory(rootsIndex)
project.registerService(MetadataFinderFactory::class.java, finderFactory)
@@ -107,4 +107,7 @@ public class JVMConfigurationKeys {
public static final CompilerConfigurationKey<Boolean> IR =
CompilerConfigurationKey.create("IR");
public static final CompilerConfigurationKey<Boolean> USE_FAST_CLASS_FILES_READING =
CompilerConfigurationKey.create("use fast class files reading implementation [experimental]");
}
+1
View File
@@ -6,6 +6,7 @@ where advanced options include:
-Xreport-perf Report detailed performance statistics
-Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade
-Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath
-Xuse-old-class-files-reading Use old class files reading implementation (may slow down the build and should be used in case of problems with the new implementation)
-Xdump-declarations-to=<path> Path to JSON file to dump Java to Kotlin declaration mappings
-Xsingle-module Combine modules for source files and binary dependencies into a single module
-Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)
@@ -123,7 +123,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
Assert.assertEquals("test", packageFromSource.getName().asString());
PackageViewDescriptor packageFromBinary = LoadDescriptorUtil.loadTestPackageAndBindingContextFromJavaRoot(
tmpdir, getTestRootDisposable(), getJdkKind(), configurationKind, true
tmpdir, getTestRootDisposable(), getJdkKind(), configurationKind, true, false
).first;
for (DeclarationDescriptor descriptor : DescriptorUtils.getAllDescriptors(packageFromBinary.getMemberScope())) {
@@ -138,6 +138,10 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
compareDescriptors(packageFromSource, packageFromBinary, comparatorConfiguration, txtFile);
}
protected boolean useFastClassFilesReading() {
return false;
}
protected void doTestJavaAgainstKotlin(String expectedFileName) throws Exception {
File expectedFile = new File(expectedFileName);
File sourcesDir = new File(expectedFileName.replaceFirst("\\.txt$", ""));
@@ -207,8 +211,8 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
FileUtil.copy(originalJavaFile, new File(testPackageDir, originalJavaFile.getName()));
Pair<PackageViewDescriptor, BindingContext> javaPackageAndContext = loadTestPackageAndBindingContextFromJavaRoot(
tmpdir, getTestRootDisposable(), getJdkKind(), ConfigurationKind.JDK_ONLY, false
);
tmpdir, getTestRootDisposable(), getJdkKind(), ConfigurationKind.JDK_ONLY, false,
false);
checkJavaPackage(
expectedFile, javaPackageAndContext.first, javaPackageAndContext.second,
@@ -253,7 +257,8 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
@NotNull ConfigurationKind configurationKind
) throws IOException {
compileJavaWithAnnotationsJar(javaFiles, outDir);
return loadTestPackageAndBindingContextFromJavaRoot(outDir, myTestRootDisposable, getJdkKind(), configurationKind, true);
return loadTestPackageAndBindingContextFromJavaRoot(outDir, myTestRootDisposable, getJdkKind(), configurationKind, true,
useFastClassFilesReading());
}
private static void checkJavaPackage(
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.jvm.compiler
abstract class AbstractLoadJavaWithFastClassReadingTest : AbstractLoadJavaTest() {
override fun useFastClassFilesReading() = true
}
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.codegen.GenerationUtils;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.config.CompilerConfiguration;
import org.jetbrains.kotlin.config.JVMConfigurationKeys;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor;
import org.jetbrains.kotlin.name.FqName;
@@ -70,7 +71,8 @@ public class LoadDescriptorUtil {
@NotNull Disposable disposable,
@NotNull TestJdkKind testJdkKind,
@NotNull ConfigurationKind configurationKind,
boolean isBinaryRoot
boolean isBinaryRoot,
boolean useFastClassReading
) {
List<File> javaBinaryRoots = new ArrayList<>();
javaBinaryRoots.add(KotlinTestUtils.getAnnotationsJar());
@@ -85,6 +87,7 @@ public class LoadDescriptorUtil {
}
CompilerConfiguration configuration =
KotlinTestUtils.newConfiguration(configurationKind, testJdkKind, javaBinaryRoots, javaSourceRoots);
configuration.put(JVMConfigurationKeys.USE_FAST_CLASS_FILES_READING, useFastClassReading);
KotlinCoreEnvironment environment =
KotlinCoreEnvironment.createForTests(disposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
@@ -107,7 +107,7 @@ abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
}
val expected = LoadDescriptorUtil.loadTestPackageAndBindingContextFromJavaRoot(
tmpdir, testRootDisposable, jdkKind, ConfigurationKind.ALL, true
tmpdir, testRootDisposable, jdkKind, ConfigurationKind.ALL, true, false
).first
RecursiveDescriptorComparator.validateAndCompareDescriptors(expected, actual, comparatorConfiguration, null)
@@ -200,7 +200,10 @@ class KotlinCliJavaFileManagerTest : KotlinTestWithEnvironment() {
val coreJavaFileManager = ServiceManager.getService(project, CoreJavaFileManager::class.java) as KotlinCliJavaFileManagerImpl
val root = environment.contentRootToVirtualFile(JavaSourceRoot(javaFilesDir!!, null))!!
coreJavaFileManager.initIndex(JvmDependenciesIndexImpl(listOf(JavaRoot(root, JavaRoot.RootType.SOURCE))))
coreJavaFileManager.initialize(
JvmDependenciesIndexImpl(listOf(JavaRoot(root, JavaRoot.RootType.SOURCE))),
useFastClassFilesReading = true
)
return coreJavaFileManager
}
@@ -327,6 +327,10 @@ fun main(args: Array<String>) {
model("loadJava/compiledJava", extension = "java", excludeDirs = listOf("sam", "kotlinSignature/propagation"))
}
testClass<AbstractLoadJavaWithFastClassReadingTest> {
model("loadJava/compiledJava", extension = "java", testMethod = "doTestCompiledJava")
}
testClass<AbstractCompileJavaAgainstKotlinTest> {
model("compileJavaAgainstKotlin")
}