Cleanup test utilities in JvmResolveUtil and GenerationUtils
Simplify usages and fix some warnings along the way. Rename: - analyzeFilesWithJavaIntegration, analyzeOneFileWithJavaIntegration -> analyze - analyzeFilesWithJavaIntegrationAndCheckForErrors, analyzeOneFileWithJavaIntegrationAndCheckForErrors -> analyzeAndCheckForErrors - compileFilesGetGenerationState, compileManyFilesGetGenerationStateForTest -> compileFiles - compileFileGetGenerationStateForTest -> compileFile - compileFileGetClassFileFactoryForTest -> compileFileTo
This commit is contained in:
@@ -25,7 +25,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JvmContentRootsKt;
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||
@@ -97,10 +96,7 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
|
||||
|
||||
loadMultiFiles(files);
|
||||
|
||||
classFileFactory = GenerationUtils.compileManyFilesGetGenerationStateForTest(
|
||||
myEnvironment.getProject(), myFiles.getPsiFiles(), new JvmPackagePartProvider(myEnvironment),
|
||||
myEnvironment.getConfiguration()
|
||||
).getFactory();
|
||||
classFileFactory = GenerationUtils.compileFiles(myFiles.getPsiFiles(), myEnvironment).getFactory();
|
||||
|
||||
if (javaSourceDir != null) {
|
||||
// If there are Java files, they should be compiled against the class files produced by Kotlin, so we dump them to the disk
|
||||
|
||||
+5
-6
@@ -44,9 +44,8 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
|
||||
public abstract class AbstractCheckLocalVariablesTableTest extends TestCaseWithTmpdir {
|
||||
|
||||
private File ktFile;
|
||||
private KotlinCoreEnvironment jetCoreEnvironment;
|
||||
private KotlinCoreEnvironment environment;
|
||||
|
||||
public AbstractCheckLocalVariablesTableTest() {
|
||||
}
|
||||
@@ -54,12 +53,12 @@ public abstract class AbstractCheckLocalVariablesTableTest extends TestCaseWithT
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
jetCoreEnvironment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable);
|
||||
environment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
jetCoreEnvironment = null;
|
||||
environment = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@@ -67,9 +66,9 @@ public abstract class AbstractCheckLocalVariablesTableTest extends TestCaseWithT
|
||||
ktFile = new File(ktFileName);
|
||||
String text = FileUtil.loadFile(ktFile, true);
|
||||
|
||||
KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), text, jetCoreEnvironment.getProject());
|
||||
KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), text, environment.getProject());
|
||||
|
||||
OutputFileCollection outputFiles = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, jetCoreEnvironment);
|
||||
OutputFileCollection outputFiles = GenerationUtils.compileFile(psiFile, environment);
|
||||
|
||||
String classAndMethod = parseClassAndMethodSignature();
|
||||
String[] split = classAndMethod.split("\\.");
|
||||
|
||||
+7
-8
@@ -22,7 +22,6 @@ import kotlin.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.cli.common.modules.ModuleBuilder;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
|
||||
@@ -85,7 +84,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends CodegenTest
|
||||
return new Pair<ClassFileFactory, ClassFileFactory>(factoryA, factoryB);
|
||||
}
|
||||
|
||||
protected void invokeBox(@NotNull String className) throws Exception {
|
||||
private void invokeBox(@NotNull String className) throws Exception {
|
||||
Method box = createGeneratedClassLoader().loadClass(className).getMethod("box");
|
||||
String result = (String) box.invoke(null);
|
||||
assertEquals("OK", result);
|
||||
@@ -100,7 +99,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends CodegenTest
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected ClassFileFactory compileA(@NotNull String fileName, @NotNull String content, List<TestFile> files) throws IOException {
|
||||
private ClassFileFactory compileA(@NotNull String fileName, @NotNull String content, List<TestFile> files) throws IOException {
|
||||
Disposable compileDisposable = createDisposable("compileA");
|
||||
KotlinCoreEnvironment environment =
|
||||
KotlinTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(compileDisposable, ConfigurationKind.ALL, getJdkKind(files));
|
||||
@@ -108,7 +107,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends CodegenTest
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected ClassFileFactory compileB(@NotNull String fileName, @NotNull String content, List<TestFile> files) throws IOException {
|
||||
private ClassFileFactory compileB(@NotNull String fileName, @NotNull String content, List<TestFile> files) throws IOException {
|
||||
CompilerConfiguration configurationWithADirInClasspath = KotlinTestUtils
|
||||
.compilerConfigurationForTests(ConfigurationKind.ALL, getJdkKind(files), KotlinTestUtils.getAnnotationsJar(), aDir);
|
||||
|
||||
@@ -132,11 +131,11 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends CodegenTest
|
||||
) throws IOException {
|
||||
KtFile psiFile = KotlinTestUtils.createFile(fileName, content, environment.getProject());
|
||||
|
||||
ModuleVisibilityManager.SERVICE.getInstance(environment.getProject()).addModule(new ModuleBuilder("module for test", tmpdir.getAbsolutePath(), "test"));
|
||||
ModuleVisibilityManager.SERVICE.getInstance(environment.getProject()).addModule(
|
||||
new ModuleBuilder("module for test", tmpdir.getAbsolutePath(), "test")
|
||||
);
|
||||
|
||||
ClassFileFactory outputFiles = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, environment);
|
||||
|
||||
OutputUtilsKt.writeAllTo(outputFiles, outputDir);
|
||||
ClassFileFactory outputFiles = GenerationUtils.compileFileTo(psiFile, environment, outputDir);
|
||||
|
||||
Disposer.dispose(disposable);
|
||||
return outputFiles;
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -51,18 +50,14 @@ abstract class AbstractDumpDeclarationsTest : CodegenTestCase() {
|
||||
}
|
||||
|
||||
private fun compileManyFilesGetDeclarationsDump(files: List<KtFile>): File {
|
||||
val project = myEnvironment.project
|
||||
val packagePartProvider = JvmPackagePartProvider(myEnvironment)
|
||||
|
||||
val analysisResult = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
project, files, packagePartProvider)
|
||||
val analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(files, myEnvironment)
|
||||
|
||||
analysisResult.throwIfError()
|
||||
|
||||
val dumpToFile = KotlinTestUtils.tmpDirForTest(this).resolve(this.name + ".json")
|
||||
|
||||
val state = GenerationState(
|
||||
project, ClassBuilderFactories.TEST,
|
||||
myEnvironment.project, ClassBuilderFactories.TEST,
|
||||
analysisResult.moduleDescriptor, analysisResult.bindingContext,
|
||||
files,
|
||||
disableCallAssertions = false,
|
||||
|
||||
@@ -25,11 +25,8 @@ import kotlin.collections.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.backend.common.output.OutputFile;
|
||||
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
@@ -47,15 +44,9 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
||||
|
||||
private static final String LINE_NUMBER_FUN = "lineNumber";
|
||||
private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$");
|
||||
|
||||
@NotNull
|
||||
private static String getTestDataPath() {
|
||||
return KotlinTestUtils.getTestDataPathBase() + "/lineNumber";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private KotlinCoreEnvironment createEnvironment() {
|
||||
return KotlinCoreEnvironment.createForTests(
|
||||
@@ -70,13 +61,13 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
||||
super.setUp();
|
||||
|
||||
KotlinCoreEnvironment environment = createEnvironment();
|
||||
KtFile psiFile = KotlinTestUtils.createFile(LINE_NUMBER_FUN + ".kt",
|
||||
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n",
|
||||
environment.getProject());
|
||||
KtFile psiFile = KotlinTestUtils.createFile(
|
||||
LINE_NUMBER_FUN + ".kt",
|
||||
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n",
|
||||
environment.getProject()
|
||||
);
|
||||
|
||||
OutputFileCollection outputFiles =
|
||||
GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, environment);
|
||||
OutputUtilsKt.writeAllTo(outputFiles, tmpdir);
|
||||
GenerationUtils.compileFileTo(psiFile, environment, tmpdir);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -92,7 +83,9 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
||||
throw ExceptionUtilsKt.rethrow(e);
|
||||
}
|
||||
|
||||
return new Pair(KotlinTestUtils.createFile(file.getName(), text, environment.getProject()), environment);
|
||||
return new Pair<KtFile, KotlinCoreEnvironment>(
|
||||
KotlinTestUtils.createFile(file.getName(), text, environment.getProject()), environment
|
||||
);
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String filename, boolean custom) {
|
||||
@@ -100,17 +93,17 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
||||
KtFile psiFile = fileAndEnv.getFirst();
|
||||
KotlinCoreEnvironment environment = fileAndEnv.getSecond();
|
||||
|
||||
GenerationState state = GenerationUtils.compileFileGetGenerationStateForTest(psiFile, environment);
|
||||
ClassFileFactory classFileFactory = GenerationUtils.compileFile(psiFile, environment);
|
||||
|
||||
if (custom) {
|
||||
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(state, false);
|
||||
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false);
|
||||
String text = psiFile.getText();
|
||||
String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers);
|
||||
KotlinTestUtils.assertEqualsToFile(new File(filename), newFileText);
|
||||
}
|
||||
else {
|
||||
List<Integer> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile);
|
||||
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(state, true);
|
||||
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true);
|
||||
assertSameElements(actualLineNumbers, expectedLineNumbers);
|
||||
}
|
||||
|
||||
@@ -126,8 +119,7 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<Integer> extractActualLineNumbersFromBytecode(@NotNull GenerationState state, boolean testFunInvoke) {
|
||||
ClassFileFactory factory = state.getFactory();
|
||||
private static List<Integer> extractActualLineNumbersFromBytecode(@NotNull ClassFileFactory factory, boolean testFunInvoke) {
|
||||
List<Integer> actualLineNumbers = Lists.newArrayList();
|
||||
for (OutputFile outputFile : ClassFileUtilsKt.getClassFiles(factory)) {
|
||||
ClassReader cr = new ClassReader(outputFile.asByteArray());
|
||||
|
||||
@@ -21,7 +21,6 @@ import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
||||
@@ -48,11 +47,7 @@ public class CodegenTestUtil {
|
||||
|
||||
@NotNull
|
||||
public static ClassFileFactory generateFiles(@NotNull KotlinCoreEnvironment environment, @NotNull CodegenTestFiles files) {
|
||||
AnalysisResult analysisResult = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
environment.getProject(),
|
||||
files.getPsiFiles(),
|
||||
new JvmPackagePartProvider(environment)
|
||||
);
|
||||
AnalysisResult analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(files.getPsiFiles(), environment);
|
||||
analysisResult.throwIfError();
|
||||
AnalyzingUtils.throwExceptionOnErrors(analysisResult.getBindingContext());
|
||||
CompilerConfiguration configuration = environment.getConfiguration();
|
||||
|
||||
@@ -16,109 +16,79 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||
import org.jetbrains.kotlin.config.CompilerConfigurationKey;
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys;
|
||||
import org.jetbrains.kotlin.descriptors.PackagePartProvider;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerationUtils {
|
||||
|
||||
private GenerationUtils() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ClassFileFactory compileFileGetClassFileFactoryForTest(
|
||||
@NotNull KtFile psiFile,
|
||||
@NotNull KotlinCoreEnvironment environment
|
||||
) {
|
||||
return compileFileGetGenerationStateForTest(psiFile, environment).getFactory();
|
||||
public static ClassFileFactory compileFileTo(@NotNull KtFile ktFile, @NotNull KotlinCoreEnvironment environment, @NotNull File output) {
|
||||
ClassFileFactory factory = compileFile(ktFile, environment);
|
||||
OutputUtilsKt.writeAllTo(factory, output);
|
||||
return factory;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileFileGetGenerationStateForTest(
|
||||
@NotNull KtFile psiFile,
|
||||
@NotNull KotlinCoreEnvironment environment
|
||||
) {
|
||||
public static ClassFileFactory compileFile(@NotNull KtFile ktFile, @NotNull KotlinCoreEnvironment environment) {
|
||||
AnalysisResult analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(ktFile, environment);
|
||||
GenerationState state = compileFiles(analysisResult, Collections.singletonList(ktFile), false, environment.getConfiguration());
|
||||
return state.getFactory();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileFiles(@NotNull List<KtFile> files, @Nullable KotlinCoreEnvironment environment) {
|
||||
PackagePartProvider packagePartProvider =
|
||||
environment == null ? PackagePartProvider.Companion.getEMPTY() : new JvmPackagePartProvider(environment);
|
||||
CompilerConfiguration configuration =
|
||||
environment == null ? CompilerConfiguration.EMPTY : environment.getConfiguration();
|
||||
AnalysisResult analysisResult =
|
||||
JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile, new JvmPackagePartProvider(environment));
|
||||
return compileFilesGetGenerationState(psiFile.getProject(), analysisResult, Collections.singletonList(psiFile), false, null);
|
||||
JvmResolveUtil.analyzeAndCheckForErrors(CollectionsKt.first(files).getProject(), files, configuration, packagePartProvider);
|
||||
return compileFiles(analysisResult, files, false, configuration);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileManyFilesGetGenerationStateForTest(@NotNull Project project, @NotNull List<KtFile> files) {
|
||||
return compileManyFilesGetGenerationStateForTest(project, files, PackagePartProvider.Companion.getEMPTY(), null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileManyFilesGetGenerationStateForTest(
|
||||
@NotNull Project project,
|
||||
@NotNull List<KtFile> files,
|
||||
@NotNull PackagePartProvider packagePartProvider,
|
||||
@Nullable CompilerConfiguration configuration
|
||||
) {
|
||||
AnalysisResult analysisResult = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
project, files, packagePartProvider);
|
||||
return compileFilesGetGenerationState(project, analysisResult, files, false, configuration);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileFilesGetGenerationState(
|
||||
@NotNull Project project,
|
||||
@NotNull AnalysisResult analysisResult,
|
||||
@NotNull List<KtFile> files,
|
||||
boolean useTypeTableInSerializer
|
||||
) {
|
||||
return compileFilesGetGenerationState(project, analysisResult, files, useTypeTableInSerializer, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GenerationState compileFilesGetGenerationState(
|
||||
@NotNull Project project,
|
||||
public static GenerationState compileFiles(
|
||||
@NotNull AnalysisResult analysisResult,
|
||||
@NotNull List<KtFile> files,
|
||||
boolean useTypeTableInSerializer,
|
||||
@Nullable CompilerConfiguration configuration
|
||||
@NotNull CompilerConfiguration configuration
|
||||
) {
|
||||
analysisResult.throwIfError();
|
||||
GenerationState state = new GenerationState(
|
||||
project, ClassBuilderFactories.TEST,
|
||||
CollectionsKt.first(files).getProject(), ClassBuilderFactories.TEST,
|
||||
analysisResult.getModuleDescriptor(), analysisResult.getBindingContext(),
|
||||
files,
|
||||
getConfigurationValueOrDefault(configuration, JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, false),
|
||||
getConfigurationValueOrDefault(configuration, JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, false),
|
||||
configuration.get(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, false),
|
||||
configuration.get(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, false),
|
||||
GenerationState.GenerateClassFilter.GENERATE_ALL,
|
||||
getConfigurationValueOrDefault(configuration, JVMConfigurationKeys.DISABLE_INLINE, false),
|
||||
getConfigurationValueOrDefault(configuration, JVMConfigurationKeys.DISABLE_OPTIMIZATION, false),
|
||||
configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false),
|
||||
configuration.get(JVMConfigurationKeys.DISABLE_OPTIMIZATION, false),
|
||||
useTypeTableInSerializer,
|
||||
getConfigurationValueOrDefault(configuration, JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, false),
|
||||
configuration.get(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, false),
|
||||
Collections.<FqName>emptySet(),
|
||||
Collections.<FqName>emptySet(),
|
||||
null,
|
||||
configuration == null ? null : configuration.get(JVMConfigurationKeys.MODULE_NAME)
|
||||
configuration.get(JVMConfigurationKeys.MODULE_NAME)
|
||||
);
|
||||
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||
return state;
|
||||
}
|
||||
|
||||
private static <T> T getConfigurationValueOrDefault(
|
||||
@Nullable CompilerConfiguration configuration,
|
||||
@NotNull CompilerConfigurationKey<T> key,
|
||||
T defaultValue
|
||||
) {
|
||||
if (configuration == null) return defaultValue;
|
||||
return configuration.get(key, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
+8
-11
@@ -54,18 +54,17 @@ import static org.jetbrains.kotlin.test.InTextDirectivesUtils.findStringWithPref
|
||||
* FLAGS: ACC_STATIC, ACC_FINAL, ACC_PRIVATE
|
||||
*/
|
||||
public abstract class AbstractWriteFlagsTest extends KtUsefulTestCase {
|
||||
|
||||
private KotlinCoreEnvironment jetCoreEnvironment;
|
||||
private KotlinCoreEnvironment environment;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
jetCoreEnvironment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable, ConfigurationKind.JDK_ONLY);
|
||||
environment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(myTestRootDisposable, ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
jetCoreEnvironment = null;
|
||||
environment = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@@ -75,19 +74,17 @@ public abstract class AbstractWriteFlagsTest extends KtUsefulTestCase {
|
||||
|
||||
String fileText = FileUtil.loadFile(ktFile, true);
|
||||
|
||||
KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), fileText, jetCoreEnvironment.getProject());
|
||||
KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), fileText, environment.getProject());
|
||||
|
||||
OutputFileCollection outputFiles = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, jetCoreEnvironment);
|
||||
OutputFileCollection outputFiles = GenerationUtils.compileFile(psiFile, environment);
|
||||
|
||||
List<TestedObject> testedObjects = parseExpectedTestedObject(fileText);
|
||||
for (TestedObject testedObject : testedObjects) {
|
||||
String className = null;
|
||||
for (OutputFile outputFile : outputFiles.asList()) {
|
||||
String filePath = outputFile.getRelativePath();
|
||||
if (testedObject.isFullContainingClassName && filePath.equals(testedObject.containingClass + ".class")) {
|
||||
className = filePath;
|
||||
}
|
||||
else if (!testedObject.isFullContainingClassName && filePath.startsWith(testedObject.containingClass)) {
|
||||
if (testedObject.isFullContainingClassName && filePath.equals(testedObject.containingClass + ".class") ||
|
||||
!testedObject.isFullContainingClassName && filePath.startsWith(testedObject.containingClass)) {
|
||||
className = filePath;
|
||||
}
|
||||
}
|
||||
@@ -118,7 +115,7 @@ public abstract class AbstractWriteFlagsTest extends KtUsefulTestCase {
|
||||
|
||||
private static List<TestedObject> parseExpectedTestedObject(String testDescription) {
|
||||
String[] testObjectData = testDescription.substring(testDescription.indexOf("// TESTED_OBJECT_KIND")).split("\n\n");
|
||||
ArrayList<TestedObject> objects = new ArrayList<TestedObject>();
|
||||
List<TestedObject> objects = new ArrayList<TestedObject>();
|
||||
|
||||
for (String testData : testObjectData) {
|
||||
if (testData.isEmpty()) continue;
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.config.ContentRootsKt;
|
||||
import org.jetbrains.kotlin.context.ModuleContext;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||
@@ -189,12 +190,10 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
|
||||
compilerConfigurationForTests(ConfigurationKind.JDK_ONLY, getJdkKind(), getAnnotationsJar(), libraryOut),
|
||||
EnvironmentConfigFiles.JVM_CONFIG_FILES);
|
||||
|
||||
KtFile jetFile = KotlinTestUtils.createFile(kotlinSrc.getPath(), FileUtil.loadFile(kotlinSrc, true), environment.getProject());
|
||||
KtFile ktFile = KotlinTestUtils.createFile(kotlinSrc.getPath(), FileUtil.loadFile(kotlinSrc, true), environment.getProject());
|
||||
|
||||
AnalysisResult result = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
environment.getProject(), Collections.singleton(jetFile)
|
||||
);
|
||||
PackageViewDescriptor packageView = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME);
|
||||
ModuleDescriptor module = JvmResolveUtil.analyzeAndCheckForErrors(Collections.singleton(ktFile), environment).getModuleDescriptor();
|
||||
PackageViewDescriptor packageView = module.getPackage(TEST_PACKAGE_FQNAME);
|
||||
assertFalse(packageView.isEmpty());
|
||||
|
||||
validateAndCompareDescriptorWithFile(packageView, DONT_INCLUDE_METHODS_OF_OBJECT.withValidationStrategy(
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.codegen.GenerationUtils;
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
|
||||
@@ -67,12 +66,11 @@ public final class LoadDescriptorUtil {
|
||||
@NotNull ConfigurationKind configurationKind,
|
||||
boolean useTypeTableInSerializer
|
||||
) {
|
||||
KtFilesAndAnalysisResult
|
||||
filesAndResult = KtFilesAndAnalysisResult.createJetFilesAndAnalyze(kotlinFiles, disposable, configurationKind);
|
||||
KtFilesAndAnalysisResult filesAndResult =
|
||||
KtFilesAndAnalysisResult.createJetFilesAndAnalyze(kotlinFiles, disposable, configurationKind);
|
||||
AnalysisResult result = filesAndResult.getAnalysisResult();
|
||||
List<KtFile> files = filesAndResult.getKtFiles();
|
||||
GenerationState state = GenerationUtils.compileFilesGetGenerationState(
|
||||
files.get(0).getProject(), result, files, useTypeTableInSerializer
|
||||
GenerationState state = GenerationUtils.compileFiles(
|
||||
result, filesAndResult.getKtFiles(), useTypeTableInSerializer, CompilerConfiguration.EMPTY
|
||||
);
|
||||
OutputUtilsKt.writeAllTo(state.getFactory(), outDir);
|
||||
return result;
|
||||
@@ -131,22 +129,22 @@ public final class LoadDescriptorUtil {
|
||||
@NotNull Disposable disposable,
|
||||
@NotNull ConfigurationKind configurationKind
|
||||
) {
|
||||
final KotlinCoreEnvironment jetCoreEnvironment = createEnvironmentWithMockJdkAndIdeaAnnotations(disposable, configurationKind);
|
||||
List<KtFile> jetFiles = ContainerUtil.map(kotlinFiles, new Function<File, KtFile>() {
|
||||
final KotlinCoreEnvironment environment = createEnvironmentWithMockJdkAndIdeaAnnotations(disposable, configurationKind);
|
||||
List<KtFile> ktFiles = ContainerUtil.map(kotlinFiles, new Function<File, KtFile>() {
|
||||
@Override
|
||||
public KtFile fun(File kotlinFile) {
|
||||
try {
|
||||
return KotlinTestUtils.createFile(
|
||||
kotlinFile.getName(), FileUtil.loadFile(kotlinFile, true), jetCoreEnvironment.getProject());
|
||||
kotlinFile.getName(), FileUtil.loadFile(kotlinFile, true), environment.getProject()
|
||||
);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
AnalysisResult result = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
jetCoreEnvironment.getProject(), jetFiles, new JvmPackagePartProvider(jetCoreEnvironment));
|
||||
return new KtFilesAndAnalysisResult(jetFiles, result);
|
||||
AnalysisResult result = JvmResolveUtil.analyzeAndCheckForErrors(ktFiles, environment);
|
||||
return new KtFilesAndAnalysisResult(ktFiles, result);
|
||||
}
|
||||
|
||||
private final List<KtFile> ktFiles;
|
||||
|
||||
+2
-3
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.kotlin.jvm.runtime
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.writeAllTo
|
||||
import org.jetbrains.kotlin.codegen.GenerationUtils
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -138,8 +137,8 @@ abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
|
||||
val environment = KotlinTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(
|
||||
myTestRootDisposable, ConfigurationKind.ALL, jdkKind
|
||||
)
|
||||
val jetFile = KotlinTestUtils.createFile(file.path, text, environment.project)
|
||||
GenerationUtils.compileFileGetClassFileFactoryForTest(jetFile, environment).writeAllTo(tmpdir)
|
||||
val ktFile = KotlinTestUtils.createFile(file.path, text, environment.project)
|
||||
GenerationUtils.compileFileTo(ktFile, environment, tmpdir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiQualifiedNamedElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
@@ -139,9 +137,7 @@ public abstract class ExpectedResolveData {
|
||||
return BindingContext.EMPTY;
|
||||
}
|
||||
|
||||
Project project = files.iterator().next().getProject();
|
||||
AnalysisResult analysisResult = JvmResolveUtil.analyzeFilesWithJavaIntegration(project, files, environment);
|
||||
return analysisResult.getBindingContext();
|
||||
return JvmResolveUtil.analyze(files, environment).getBindingContext();
|
||||
}
|
||||
|
||||
public final void checkResult(BindingContext bindingContext) {
|
||||
|
||||
+3
-3
@@ -50,10 +50,10 @@ abstract class AbstractResolvedCallsTest : KotlinTestWithEnvironment() {
|
||||
fun doTest(filePath: String) {
|
||||
val text = KotlinTestUtils.doLoadFile(File(filePath))!!
|
||||
|
||||
val jetFile = KtPsiFactory(project).createFile(text.replace("<caret>", ""))
|
||||
val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegration(jetFile, environment).bindingContext
|
||||
val ktFile = KtPsiFactory(project).createFile(text.replace("<caret>", ""))
|
||||
val bindingContext = JvmResolveUtil.analyze(ktFile, environment).bindingContext
|
||||
|
||||
val (element, cachedCall) = buildCachedCall(bindingContext, jetFile, text)
|
||||
val (element, cachedCall) = buildCachedCall(bindingContext, ktFile, text)
|
||||
|
||||
val resolvedCall = if (cachedCall !is VariableAsFunctionResolvedCall) cachedCall
|
||||
else if ("(" == element?.text) cachedCall.functionCall
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ abstract class AbstractConstraintSystemTest : KotlinTestWithEnvironment() {
|
||||
val fileName = "declarations.kt"
|
||||
|
||||
val psiFile = KotlinTestUtils.createFile(fileName, KotlinTestUtils.doLoadFile(testDataPath, fileName), project)
|
||||
val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile).bindingContext
|
||||
val bindingContext = JvmResolveUtil.analyzeAndCheckForErrors(psiFile, environment).bindingContext
|
||||
return ConstraintSystemTestData(bindingContext, project, typeResolver)
|
||||
}
|
||||
|
||||
|
||||
@@ -35,55 +35,32 @@ public class JvmResolveUtil {
|
||||
public static String TEST_MODULE_NAME = "java-integration-test";
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeOneFileWithJavaIntegrationAndCheckForErrors(@NotNull KtFile file) {
|
||||
return analyzeOneFileWithJavaIntegrationAndCheckForErrors(file, PackagePartProvider.Companion.getEMPTY());
|
||||
public static AnalysisResult analyzeAndCheckForErrors(@NotNull KtFile file, @NotNull KotlinCoreEnvironment environment) {
|
||||
return analyzeAndCheckForErrors(Collections.singleton(file), environment);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeOneFileWithJavaIntegrationAndCheckForErrors(@NotNull KtFile file, @NotNull PackagePartProvider provider) {
|
||||
AnalyzingUtils.checkForSyntacticErrors(file);
|
||||
|
||||
AnalysisResult analysisResult = analyzeOneFileWithJavaIntegration(file, provider);
|
||||
|
||||
AnalyzingUtils.throwExceptionOnErrors(analysisResult.getBindingContext());
|
||||
|
||||
return analysisResult;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeOneFileWithJavaIntegration(@NotNull KtFile file, @NotNull KotlinCoreEnvironment environment) {
|
||||
return analyzeOneFileWithJavaIntegration(file, new JvmPackagePartProvider(environment));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static AnalysisResult analyzeOneFileWithJavaIntegration(@NotNull KtFile file, @NotNull PackagePartProvider provider) {
|
||||
return analyzeFilesWithJavaIntegration(file.getProject(), Collections.singleton(file), provider);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeOneFileWithJavaIntegration(@NotNull KtFile file) {
|
||||
return analyzeOneFileWithJavaIntegration(file, PackagePartProvider.Companion.getEMPTY());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
@NotNull Project project,
|
||||
@NotNull Collection<KtFile> files
|
||||
public static AnalysisResult analyzeAndCheckForErrors(
|
||||
@NotNull Collection<KtFile> files,
|
||||
@NotNull KotlinCoreEnvironment environment
|
||||
) {
|
||||
return analyzeFilesWithJavaIntegrationAndCheckForErrors(project, files, PackagePartProvider.Companion.getEMPTY());
|
||||
return analyzeAndCheckForErrors(
|
||||
environment.getProject(), files, environment.getConfiguration(), new JvmPackagePartProvider(environment)
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeFilesWithJavaIntegrationAndCheckForErrors(
|
||||
public static AnalysisResult analyzeAndCheckForErrors(
|
||||
@NotNull Project project,
|
||||
@NotNull Collection<KtFile> files,
|
||||
@NotNull CompilerConfiguration configuration,
|
||||
@NotNull PackagePartProvider packagePartProvider
|
||||
) {
|
||||
for (KtFile file : files) {
|
||||
AnalyzingUtils.checkForSyntacticErrors(file);
|
||||
}
|
||||
|
||||
AnalysisResult analysisResult = analyzeFilesWithJavaIntegration(project, files, packagePartProvider);
|
||||
AnalysisResult analysisResult = analyze(project, files, configuration, packagePartProvider);
|
||||
|
||||
AnalyzingUtils.throwExceptionOnErrors(analysisResult.getBindingContext());
|
||||
|
||||
@@ -91,23 +68,25 @@ public class JvmResolveUtil {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeFilesWithJavaIntegration(
|
||||
@NotNull Project project,
|
||||
@NotNull Collection<KtFile> files,
|
||||
@NotNull KotlinCoreEnvironment environment
|
||||
) {
|
||||
return analyzeFilesWithJavaIntegration(project, files, new JvmPackagePartProvider(environment));
|
||||
public static AnalysisResult analyze(@NotNull KtFile file, @NotNull KotlinCoreEnvironment environment) {
|
||||
return analyze(Collections.singleton(file), environment);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static AnalysisResult analyzeFilesWithJavaIntegration(
|
||||
public static AnalysisResult analyze(@NotNull Collection<KtFile> files, @NotNull KotlinCoreEnvironment environment) {
|
||||
return analyze(environment.getProject(), files, environment.getConfiguration(), new JvmPackagePartProvider(environment));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static AnalysisResult analyze(
|
||||
@NotNull Project project,
|
||||
@NotNull Collection<KtFile> files,
|
||||
@NotNull CompilerConfiguration configuration,
|
||||
@NotNull PackagePartProvider packagePartProvider
|
||||
) {
|
||||
return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationWithCustomContext(
|
||||
TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(project, TEST_MODULE_NAME),
|
||||
files, new CliLightClassGenerationSupport.CliBindingTrace(), CompilerConfiguration.EMPTY, packagePartProvider
|
||||
files, new CliLightClassGenerationSupport.CliBindingTrace(), configuration, packagePartProvider
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ public class KotlinTestUtils {
|
||||
|
||||
@NotNull
|
||||
public static AnalysisResult analyzeFile(@NotNull KtFile file, @NotNull KotlinCoreEnvironment environment) {
|
||||
return JvmResolveUtil.analyzeOneFileWithJavaIntegration(file, environment);
|
||||
return JvmResolveUtil.analyze(file, environment);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -467,7 +467,7 @@ public class KotlinTestUtils {
|
||||
|
||||
JvmContentRootsKt.addJvmClasspathRoots(configuration, classpath);
|
||||
|
||||
configuration.put(MODULE_NAME, "compilerConfigurationForTests");
|
||||
configuration.put(MODULE_NAME, JvmResolveUtil.TEST_MODULE_NAME);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user