Passing services via type-safe container instead of map.

This commit is contained in:
Evgeny Gerashchenko
2014-08-19 20:49:48 +04:00
parent 184ddbc9e1
commit f3b7ae379f
9 changed files with 75 additions and 28 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.jet.cli.js.K2JSCompiler
import java.io.File
import org.apache.tools.ant.BuildException
import org.jetbrains.jet.cli.common.ExitCode
import org.jetbrains.jet.config.CompilerServices
/**
* Kotlin JavaScript compiler Ant task.
@@ -89,7 +90,7 @@ public class Kotlin2JsCompilerTask : Task() {
log("Compiling ${arguments.freeArgs} => [${arguments.outputFile}]");
val compiler = K2JSCompiler()
val exitCode = compiler.exec(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR, mapOf(), arguments)
val exitCode = compiler.exec(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR, CompilerServices.empty, arguments)
if (exitCode != ExitCode.OK) {
throw BuildException("Compilation finished with exit code $exitCode")
@@ -27,6 +27,7 @@ import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.jet.cli.common.messages.*;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentException;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.config.CompilerServices;
import java.io.PrintStream;
import java.util.Collections;
@@ -50,12 +51,12 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
public ExitCode exec(@NotNull PrintStream errStream, @NotNull String... args) {
return exec(errStream, Collections.<Class, Object>emptyMap(), MessageRenderer.PLAIN_WITH_RELATIVE_PATH, args);
return exec(errStream, new CompilerServices.Builder().build(), MessageRenderer.PLAIN_WITH_RELATIVE_PATH, args);
}
@SuppressWarnings("UnusedDeclaration") // Used via reflection in CompilerRunnerUtil#invokeExecMethod
@NotNull
public ExitCode execAndOutputHtml(@NotNull PrintStream errStream, @NotNull Map<Class, Object> services, @NotNull String... args) {
public ExitCode execAndOutputHtml(@NotNull PrintStream errStream, @NotNull CompilerServices services, @NotNull String... args) {
return exec(errStream, services, MessageRenderer.TAGS, args);
}
@@ -101,7 +102,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
private ExitCode exec(
@NotNull PrintStream errStream,
@NotNull Map<Class, Object> services,
@NotNull CompilerServices services,
@NotNull MessageRenderer messageRenderer,
@NotNull String[] args
) {
@@ -134,7 +135,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
}
@NotNull
public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull Map<Class, Object> services, @NotNull A arguments) {
public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull CompilerServices services, @NotNull A arguments) {
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
try {
Disposable rootDisposable = Disposer.newDisposable();
@@ -160,7 +161,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
protected abstract ExitCode doExecute(
@NotNull A arguments,
@NotNull Map<Class, Object> services,
@NotNull CompilerServices services,
@NotNull MessageCollector messageCollector,
@NotNull Disposable rootDisposable
);
@@ -42,6 +42,7 @@ import org.jetbrains.jet.cli.common.output.outputUtils.OutputUtilsPackage;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.config.CompilerServices;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
import org.jetbrains.k2js.config.*;
@@ -51,7 +52,6 @@ import org.jetbrains.k2js.facade.MainCallParameters;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.cli.common.ExitCode.COMPILATION_ERROR;
import static org.jetbrains.jet.cli.common.ExitCode.OK;
@@ -74,7 +74,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
@Override
protected ExitCode doExecute(
@NotNull K2JSCompilerArguments arguments,
@NotNull Map<Class, Object> services,
@NotNull CompilerServices services,
@NotNull MessageCollector messageCollector,
@NotNull Disposable rootDisposable
) {
@@ -34,6 +34,7 @@ import org.jetbrains.jet.cli.jvm.repl.ReplFromTerminal;
import org.jetbrains.jet.codegen.CompilationException;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.config.CompilerServices;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.IncrementalCacheProvider;
import org.jetbrains.jet.utils.KotlinPaths;
@@ -43,7 +44,6 @@ import org.jetbrains.jet.utils.PathUtil;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.google.common.base.Predicates.in;
import static org.jetbrains.jet.cli.common.ExitCode.*;
@@ -59,7 +59,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
@NotNull
protected ExitCode doExecute(
@NotNull K2JVMCompilerArguments arguments,
@NotNull Map<Class, Object> services,
@NotNull CompilerServices services,
@NotNull MessageCollector messageCollector,
@NotNull Disposable rootDisposable
) {
@@ -77,7 +77,8 @@ public class ClassPreloadingUtils {
public Class<?> loadClass(String name) throws ClassNotFoundException {
// When compiler is invoked from JPS, we should use loaded incremental cache interface from its class loader,
// because implementation is loaded from it, as well
if (name.startsWith("org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.")) {
if (name.startsWith("org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.") ||
name.equals("org.jetbrains.jet.config.CompilerServices")) {
if (parent == null) {
return super.loadClass(name);
}
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2014 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.jet.config
import java.util.HashMap
public class CompilerServices private(private val map: Map<Class<*>, Any>) {
public fun <T> get(interfaceClass: Class<T>): T {
return map.get(interfaceClass) as T
}
public class Builder {
private val map = HashMap<Class<*>, Any>()
public fun <T> register(interfaceClass: Class<T>, implementation: T): Builder {
map.put(interfaceClass, implementation)
return this
}
public fun build(): CompilerServices {
return CompilerServices(map)
}
}
public class object {
public val empty: CompilerServices
get() = Builder().build()
}
}
@@ -19,12 +19,11 @@ package org.jetbrains.jet.compiler.runner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.config.CompilerServices;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.PathUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERROR;
@@ -35,15 +34,9 @@ public final class CompilerEnvironment {
@NotNull KotlinPaths kotlinPaths,
@Nullable File outputDir,
@Nullable ClassLoader parentClassLoader,
@NotNull Object... serviceImplementations
@NotNull CompilerServices compilerServices
) {
Map<Class, Object> servicesMap = new HashMap<Class, Object>();
for (Object serviceImplementation : serviceImplementations) {
for (Class<?> serviceInterface : serviceImplementation.getClass().getInterfaces()) {
servicesMap.put(serviceInterface, serviceImplementation);
}
}
return new CompilerEnvironment(kotlinPaths, outputDir, parentClassLoader, servicesMap);
return new CompilerEnvironment(kotlinPaths, outputDir, parentClassLoader, compilerServices);
}
@NotNull
@@ -53,13 +46,13 @@ public final class CompilerEnvironment {
@Nullable
private final ClassLoader parentClassLoader;
@NotNull
private final Map<Class, Object> services;
private final CompilerServices services;
private CompilerEnvironment(
@NotNull KotlinPaths kotlinPaths,
@Nullable File output,
@Nullable ClassLoader parentClassLoader,
@NotNull Map<Class, Object> services
@NotNull CompilerServices services
) {
this.kotlinPaths = kotlinPaths;
this.output = output;
@@ -98,7 +91,7 @@ public final class CompilerEnvironment {
}
@NotNull
public Map<Class, Object> getServices() {
public CompilerServices getServices() {
return services;
}
}
@@ -20,6 +20,7 @@ import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.config.CompilerServices;
import org.jetbrains.jet.preloading.ClassPreloadingUtils;
import org.jetbrains.jet.utils.KotlinPaths;
@@ -33,7 +34,6 @@ import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERROR;
@@ -123,7 +123,10 @@ public class CompilerRunnerUtil {
: getOrCreateClassLoader(environment.getKotlinPaths(), messageCollector);
Class<?> kompiler = Class.forName(compilerClassName, true, loader);
Method exec = kompiler.getMethod("execAndOutputHtml", PrintStream.class, Map.class, String[].class);
Method exec = kompiler.getMethod(
"execAndOutputHtml",
PrintStream.class,
Class.forName("org.jetbrains.jet.config.CompilerServices", true, loader), String[].class);
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
}
@@ -37,9 +37,11 @@ import org.jetbrains.jet.compiler.runner.CompilerEnvironment;
import org.jetbrains.jet.compiler.runner.CompilerRunnerConstants;
import org.jetbrains.jet.compiler.runner.OutputItemsCollectorImpl;
import org.jetbrains.jet.compiler.runner.SimpleOutputItem;
import org.jetbrains.jet.config.CompilerServices;
import org.jetbrains.jet.config.IncrementalCompilation;
import org.jetbrains.jet.jps.JpsKotlinCompilerSettings;
import org.jetbrains.jet.jps.incremental.*;
import org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.IncrementalCacheProvider;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.jps.ModuleChunk;
import org.jetbrains.jps.builders.DirtyFilesHolder;
@@ -125,10 +127,12 @@ public class KotlinBuilder extends ModuleLevelBuilder {
incrementalCaches.put(target, dataManager.getStorage(target, IncrementalCacheStorageProvider.INSTANCE$));
}
IncrementalCacheProviderImpl cacheProvider = new IncrementalCacheProviderImpl(incrementalCaches);
CompilerServices compilerServices = new CompilerServices.Builder()
.register(IncrementalCacheProvider.class, new IncrementalCacheProviderImpl(incrementalCaches))
.build();
CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, getClass().getClassLoader(), cacheProvider);
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, getClass().getClassLoader(), compilerServices);
if (!environment.success()) {
environment.reportErrorsTo(messageCollector);
return ExitCode.ABORT;