diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/JetCoreEnvironment.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/JetCoreEnvironment.java index 06bbafbacde..794c86ebc9f 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/JetCoreEnvironment.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/JetCoreEnvironment.java @@ -76,7 +76,8 @@ public class JetCoreEnvironment { private boolean initialized = false; public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerConfiguration configuration) { - this.configuration = configuration; + this.configuration = configuration.copy(); + this.configuration.setReadOnly(true); this.applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable); applicationEnvironment.registerFileType(JetFileType.INSTANCE, "kt"); diff --git a/compiler/frontend/src/org/jetbrains/jet/config/CompilerConfiguration.java b/compiler/frontend/src/org/jetbrains/jet/config/CompilerConfiguration.java index c247b35f847..50b72b2a688 100644 --- a/compiler/frontend/src/org/jetbrains/jet/config/CompilerConfiguration.java +++ b/compiler/frontend/src/org/jetbrains/jet/config/CompilerConfiguration.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -32,16 +33,18 @@ import java.util.concurrent.CopyOnWriteArrayList; */ public class CompilerConfiguration { private final UserDataHolderBase holder = new UserDataHolderBase(); + private boolean readOnly = false; @Nullable public T get(@NotNull Key key) { - return holder.getUserData(key); + T data = holder.getUserData(key); + return data == null ? null : unmodifiable(data); } @NotNull public T get(@NotNull Key key, @NotNull T defaultValue) { T data = holder.getUserData(key); - return data == null ? defaultValue : data; + return data == null ? defaultValue : unmodifiable(data); } @NotNull @@ -56,15 +59,18 @@ public class CompilerConfiguration { } public void put(@NotNull Key key, @Nullable T value) { + checkReadOnly(); holder.putUserData(key, value); } public void add(@NotNull Key> key, @NotNull T value) { + checkReadOnly(); List list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList()); list.add(value); } public void addAll(@NotNull Key> key, @NotNull Collection value) { + checkReadOnly(); List list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList()); list.addAll(value); } @@ -74,4 +80,33 @@ public class CompilerConfiguration { holder.copyUserDataTo(copy.holder); return copy; } + + private void checkReadOnly() { + if (readOnly) { + throw new IllegalStateException("CompilerConfiguration is read-only"); + } + } + + public void setReadOnly(boolean readOnly) { + if (readOnly != this.readOnly) { + checkReadOnly(); + this.readOnly = readOnly; + } + } + + @NotNull + private static T unmodifiable(@NotNull T object) { + if (object instanceof List) { + return (T) Collections.unmodifiableList((List) object); + } + else if (object instanceof Map) { + return (T) Collections.unmodifiableMap((Map) object); + } + else if (object instanceof Collection) { + return (T) Collections.unmodifiableCollection((Collection) object); + } + else { + return object; + } + } }