use of org.jetbrains.kotlin.config.CompilerConfiguration in project kotlin by JetBrains.
the class AbstractLoadJavaTest method doTestJavaAgainstKotlin.
protected void doTestJavaAgainstKotlin(String expectedFileName) throws Exception {
File expectedFile = new File(expectedFileName);
File sourcesDir = new File(expectedFileName.replaceFirst("\\.txt$", ""));
FileUtil.copyDir(sourcesDir, new File(tmpdir, "test"), new FileFilter() {
@Override
public boolean accept(@NotNull File pathname) {
return pathname.getName().endsWith(".java");
}
});
CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, getJdkKind());
ContentRootsKt.addKotlinSourceRoot(configuration, sourcesDir.getAbsolutePath());
JvmContentRootsKt.addJavaSourceRoot(configuration, new File("compiler/testData/loadJava/include"));
JvmContentRootsKt.addJavaSourceRoot(configuration, tmpdir);
final KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForTests(getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
AnalysisResult result = TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(environment.getProject(), environment.getSourceFiles(), new CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace(), configuration, new Function1<GlobalSearchScope, PackagePartProvider>() {
@Override
public PackagePartProvider invoke(GlobalSearchScope scope) {
return new JvmPackagePartProvider(environment, scope);
}
});
PackageViewDescriptor packageView = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME);
checkJavaPackage(expectedFile, packageView, result.getBindingContext(), COMPARATOR_CONFIGURATION);
}
use of org.jetbrains.kotlin.config.CompilerConfiguration in project kotlin by JetBrains.
the class KotlinAsJavaTestBase method createEnvironment.
@Override
protected KotlinCoreEnvironment createEnvironment() {
CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK);
for (File root : getKotlinSourceRoots()) {
ContentRootsKt.addKotlinSourceRoot(configuration, root.getPath());
}
extraConfiguration(configuration);
return KotlinCoreEnvironment.createForTests(getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
}
use of org.jetbrains.kotlin.config.CompilerConfiguration in project kotlin by JetBrains.
the class CompileKotlinAgainstCustomBinariesTest method createEnvironment.
@NotNull
private KotlinCoreEnvironment createEnvironment(@NotNull List<File> extraClassPath) {
List<File> extras = new ArrayList<File>();
extras.addAll(extraClassPath);
extras.add(KotlinTestUtils.getAnnotationsJar());
CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, extras.toArray(new File[extras.size()]));
return KotlinCoreEnvironment.createForTests(getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
}
use of org.jetbrains.kotlin.config.CompilerConfiguration in project kotlin by JetBrains.
the class ExecuteKotlinScriptMojo method executeScriptFile.
private void executeScriptFile(File scriptFile) throws MojoExecutionException {
initCompiler();
Disposable rootDisposable = Disposer.newDisposable();
try {
MavenPluginLogMessageCollector messageCollector = new MavenPluginLogMessageCollector(getLog());
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
List<File> deps = new ArrayList<File>();
deps.addAll(PathUtil.getJdkClassesRoots());
deps.addAll(getDependenciesForScript());
for (File item : deps) {
if (item.exists()) {
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new JvmClasspathRoot(item));
getLog().debug("Adding to classpath: " + item.getAbsolutePath());
} else {
getLog().debug("Skipping non-existing dependency: " + item.getAbsolutePath());
}
}
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(scriptFile.getAbsolutePath()));
configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
K2JVMCompiler.Companion.configureScriptDefinitions(scriptTemplates.toArray(new String[scriptTemplates.size()]), configuration, messageCollector, new HashMap<String, Object>());
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
GenerationState state = KotlinToJVMBytecodeCompiler.INSTANCE.analyzeAndGenerate(environment);
if (state == null) {
throw new ScriptExecutionException(scriptFile, "compile error");
}
GeneratedClassLoader classLoader = new GeneratedClassLoader(state.getFactory(), getClass().getClassLoader());
KtScript script = environment.getSourceFiles().get(0).getScript();
FqName nameForScript = script.getFqName();
try {
Class<?> klass = classLoader.loadClass(nameForScript.asString());
ExecuteKotlinScriptMojo.INSTANCE = this;
if (ReflectionUtilKt.tryConstructClassFromStringArgs(klass, scriptArguments) == null)
throw new ScriptExecutionException(scriptFile, "unable to construct script");
} catch (ClassNotFoundException e) {
throw new ScriptExecutionException(scriptFile, "internal error", e);
}
} finally {
rootDisposable.dispose();
ExecuteKotlinScriptMojo.INSTANCE = null;
}
}
use of org.jetbrains.kotlin.config.CompilerConfiguration in project kotlin by JetBrains.
the class ResolveDescriptorsFromExternalLibraries method parseLibraryFileChunk.
private static boolean parseLibraryFileChunk(File jar, String libDescription, ZipInputStream zip, int classesPerChunk) throws IOException {
Disposable junk = new Disposable() {
@Override
public void dispose() {
}
};
KotlinCoreEnvironment environment;
if (jar != null) {
environment = KotlinCoreEnvironment.createForTests(junk, KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), jar), EnvironmentConfigFiles.JVM_CONFIG_FILES);
} else {
CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.FULL_JDK);
environment = KotlinCoreEnvironment.createForTests(junk, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
if (!findRtJar().equals(jar)) {
throw new RuntimeException("rt.jar mismatch: " + jar + ", " + findRtJar());
}
}
ModuleDescriptor module = JvmResolveUtil.analyze(environment).getModuleDescriptor();
boolean hasErrors;
try {
hasErrors = false;
for (int count = 0; count < classesPerChunk; ) {
ZipEntry entry = zip.getNextEntry();
if (entry == null) {
break;
}
if (count == 0) {
System.err.println("chunk from " + entry.getName());
}
System.err.println(entry.getName());
String entryName = entry.getName();
if (!entryName.endsWith(".class")) {
continue;
}
if (entryName.matches("(.*/|)package-info\\.class")) {
continue;
}
if (entryName.contains("$")) {
continue;
}
String className = entryName.substring(0, entryName.length() - ".class".length()).replace("/", ".");
try {
ClassDescriptor clazz = DescriptorUtilsKt.resolveTopLevelClass(module, new FqName(className), NoLookupLocation.FROM_TEST);
if (clazz == null) {
throw new IllegalStateException("class not found by name " + className + " in " + libDescription);
}
DescriptorUtils.getAllDescriptors(clazz.getDefaultType().getMemberScope());
} catch (Exception e) {
System.err.println("failed to resolve " + className);
e.printStackTrace();
//throw new RuntimeException("failed to resolve " + className + ": " + e, e);
hasErrors = true;
}
++count;
}
} finally {
Disposer.dispose(junk);
}
return hasErrors;
}
Aggregations