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
@@ -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()
}
}