From f3b7ae379f15e4bacb88990150eb7e918bc17c05 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 19 Aug 2014 20:49:48 +0400 Subject: [PATCH] Passing services via type-safe container instead of map. --- .../buildtools/ant/Kotlin2JsCompilerTask.kt | 3 +- .../jetbrains/jet/cli/common/CLICompiler.java | 11 ++--- .../jetbrains/jet/cli/js/K2JSCompiler.java | 4 +- .../jetbrains/jet/cli/jvm/K2JVMCompiler.java | 4 +- .../jet/preloading/ClassPreloadingUtils.java | 3 +- .../jetbrains/jet/config/CompilerServices.kt | 44 +++++++++++++++++++ .../compiler/runner/CompilerEnvironment.java | 19 +++----- .../compiler/runner/CompilerRunnerUtil.java | 7 ++- .../jet/jps/build/KotlinBuilder.java | 8 +++- 9 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 compiler/util/src/org/jetbrains/jet/config/CompilerServices.kt diff --git a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/Kotlin2JsCompilerTask.kt b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/Kotlin2JsCompilerTask.kt index e7b522069dc..17e80ed281b 100644 --- a/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/Kotlin2JsCompilerTask.kt +++ b/build-tools/ant/src/org/jetbrains/jet/buildtools/ant/Kotlin2JsCompilerTask.kt @@ -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") diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java index 48138c4f797..6401b6043e8 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java @@ -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 { @NotNull public ExitCode exec(@NotNull PrintStream errStream, @NotNull String... args) { - return exec(errStream, Collections.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 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 { @NotNull private ExitCode exec( @NotNull PrintStream errStream, - @NotNull Map services, + @NotNull CompilerServices services, @NotNull MessageRenderer messageRenderer, @NotNull String[] args ) { @@ -134,7 +135,7 @@ public abstract class CLICompiler { } @NotNull - public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull Map 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 { @NotNull protected abstract ExitCode doExecute( @NotNull A arguments, - @NotNull Map services, + @NotNull CompilerServices services, @NotNull MessageCollector messageCollector, @NotNull Disposable rootDisposable ); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java index f9ae7666e97..2c906af07fb 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -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 { @Override protected ExitCode doExecute( @NotNull K2JSCompilerArguments arguments, - @NotNull Map services, + @NotNull CompilerServices services, @NotNull MessageCollector messageCollector, @NotNull Disposable rootDisposable ) { diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index d4c23c7127e..2b17ea79307 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -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 { @NotNull protected ExitCode doExecute( @NotNull K2JVMCompilerArguments arguments, - @NotNull Map services, + @NotNull CompilerServices services, @NotNull MessageCollector messageCollector, @NotNull Disposable rootDisposable ) { diff --git a/compiler/preloader/src/org/jetbrains/jet/preloading/ClassPreloadingUtils.java b/compiler/preloader/src/org/jetbrains/jet/preloading/ClassPreloadingUtils.java index 30530903f34..3b5898df090 100644 --- a/compiler/preloader/src/org/jetbrains/jet/preloading/ClassPreloadingUtils.java +++ b/compiler/preloader/src/org/jetbrains/jet/preloading/ClassPreloadingUtils.java @@ -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); } diff --git a/compiler/util/src/org/jetbrains/jet/config/CompilerServices.kt b/compiler/util/src/org/jetbrains/jet/config/CompilerServices.kt new file mode 100644 index 00000000000..45c24b15966 --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/config/CompilerServices.kt @@ -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, Any>) { + + public fun get(interfaceClass: Class): T { + return map.get(interfaceClass) as T + } + + public class Builder { + private val map = HashMap, Any>() + + public fun register(interfaceClass: Class, 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() + } +} diff --git a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerEnvironment.java b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerEnvironment.java index 1df624439a2..b2adcd155a4 100644 --- a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerEnvironment.java +++ b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerEnvironment.java @@ -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 servicesMap = new HashMap(); - 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 services; + private final CompilerServices services; private CompilerEnvironment( @NotNull KotlinPaths kotlinPaths, @Nullable File output, @Nullable ClassLoader parentClassLoader, - @NotNull Map services + @NotNull CompilerServices services ) { this.kotlinPaths = kotlinPaths; this.output = output; @@ -98,7 +91,7 @@ public final class CompilerEnvironment { } @NotNull - public Map getServices() { + public CompilerServices getServices() { return services; } } diff --git a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java index c0c30346136..410c3ac1d3f 100644 --- a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java +++ b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java @@ -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); } diff --git a/jps-plugin/src/org/jetbrains/jet/jps/build/KotlinBuilder.java b/jps-plugin/src/org/jetbrains/jet/jps/build/KotlinBuilder.java index 3de567053f6..a09cbed0ddf 100644 --- a/jps-plugin/src/org/jetbrains/jet/jps/build/KotlinBuilder.java +++ b/jps-plugin/src/org/jetbrains/jet/jps/build/KotlinBuilder.java @@ -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;