diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt index 7235ebd3a75..c57c49c70a2 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.kt @@ -14,67 +14,40 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.compiler.configuration; +package org.jetbrains.kotlin.idea.compiler.configuration -import com.intellij.openapi.components.PersistentStateComponent; -import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters; -import com.intellij.util.xmlb.XmlSerializer; -import org.jdom.Element; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments; -import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; -import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; -import org.jetbrains.kotlin.config.SettingConstants; +import com.intellij.openapi.components.PersistentStateComponent +import com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_DIR +import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters +import com.intellij.util.xmlb.XmlSerializer +import org.jdom.Element +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.config.SettingConstants -import static com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_DIR; +abstract class BaseKotlinCompilerSettings protected constructor() : PersistentStateComponent, Cloneable { + @Suppress("LeakingThis") + var settings: T = createSettings() + private set -public abstract class BaseKotlinCompilerSettings implements PersistentStateComponent { - public static final String KOTLIN_COMPILER_SETTINGS_PATH = PROJECT_CONFIG_DIR + "/" + SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE; + protected abstract fun createSettings(): T - private static final SkipDefaultValuesSerializationFilters SKIP_DEFAULT_VALUES = new SkipDefaultValuesSerializationFilters( - CommonCompilerArguments.createDefaultInstance(), - K2JVMCompilerArguments.createDefaultInstance(), - K2JSCompilerArguments.createDefaultInstance() - ); - @NotNull - private T settings; + override fun getState() = XmlSerializer.serialize(settings, SKIP_DEFAULT_VALUES) - protected BaseKotlinCompilerSettings() { - //noinspection AbstractMethodCallInConstructor - this.settings = createSettings(); + override fun loadState(state: Element) { + settings = XmlSerializer.deserialize(state, settings.javaClass) ?: createSettings() } - @NotNull - public T getSettings() { - return settings; - } + public override fun clone(): Any = super.clone() - @NotNull - protected abstract T createSettings(); + companion object { + const val KOTLIN_COMPILER_SETTINGS_PATH = PROJECT_CONFIG_DIR + "/" + SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE - @Override - public Element getState() { - return XmlSerializer.serialize(settings, SKIP_DEFAULT_VALUES); - } - - @Override - public void loadState(Element state) { - //noinspection unchecked - T newSettings = (T) XmlSerializer.deserialize(state, settings.getClass()); - if (newSettings == null) - newSettings = createSettings(); - - settings = newSettings; - } - - @Override - @Nullable - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException e) { - return null; - } + private val SKIP_DEFAULT_VALUES = SkipDefaultValuesSerializationFilters( + CommonCompilerArguments.createDefaultInstance(), + K2JVMCompilerArguments.createDefaultInstance(), + K2JSCompilerArguments.createDefaultInstance() + ) } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt index 02fdbbf573f..7bd1ce6def1 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.kt @@ -14,70 +14,52 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.compiler.configuration; +package org.jetbrains.kotlin.idea.compiler.configuration -import com.intellij.openapi.components.*; -import com.intellij.openapi.project.Project; -import com.intellij.util.text.VersionComparatorUtil; -import org.jdom.Attribute; -import org.jdom.Element; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments; -import org.jetbrains.kotlin.config.FacetSerializationKt; -import org.jetbrains.kotlin.config.LanguageVersion; +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import com.intellij.util.text.VersionComparatorUtil +import org.jdom.Element +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.config.LanguageVersion +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION +import org.jetbrains.kotlin.config.getOption -import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION; - -@State( - name = KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION, - storages = { - @Storage(file = StoragePathMacros.PROJECT_FILE), - @Storage(file = BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED) - } -) -public class KotlinCommonCompilerArgumentsHolder extends BaseKotlinCompilerSettings { - private static String DEFAULT_LANGUAGE_VERSION = LanguageVersion.LATEST.getVersionString(); - - public static KotlinCommonCompilerArgumentsHolder getInstance(Project project) { - return ServiceManager.getService(project, KotlinCommonCompilerArgumentsHolder.class); - } - - private static void dropElementIfDefault(@Nullable Element element) { - if (element == null) return; - - Attribute versionAttribute = element.getAttribute("value"); - String version = versionAttribute != null ? versionAttribute.getValue() : null; - if (DEFAULT_LANGUAGE_VERSION.equals(version)) { - element.detach(); +@State(name = KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION, + storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE), + Storage(file = BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH, + scheme = StorageScheme.DIRECTORY_BASED))) +class KotlinCommonCompilerArgumentsHolder : BaseKotlinCompilerSettings() { + private fun Element.dropElementIfDefault() { + if (DEFAULT_LANGUAGE_VERSION == getAttribute("value")?.value) { + detach() } } - @Override - public Element getState() { - Element element = super.getState(); - if (element != null) { + override fun getState(): Element { + return super.getState().apply { // Do not serialize language/api version if they correspond to the default language version - dropElementIfDefault(FacetSerializationKt.getOption(element, "languageVersion")); - dropElementIfDefault(FacetSerializationKt.getOption(element, "apiVersion")); + getOption("languageVersion")?.dropElementIfDefault() + getOption("apiVersion")?.dropElementIfDefault() } - return element; } - @Override - public void loadState(Element state) { - super.loadState(state); + override fun loadState(state: Element) { + super.loadState(state) // To fix earlier configurations with incorrect combination of language and API version - CommonCompilerArguments settings = getSettings(); + val settings = settings if (VersionComparatorUtil.compare(settings.languageVersion, settings.apiVersion) < 0) { - settings.apiVersion = settings.languageVersion; + settings.apiVersion = settings.languageVersion } } - @NotNull - @Override - protected CommonCompilerArguments createSettings() { - return CommonCompilerArguments.createDefaultInstance(); + override fun createSettings() = CommonCompilerArguments.createDefaultInstance() + + companion object { + private val DEFAULT_LANGUAGE_VERSION = LanguageVersion.LATEST.versionString + + fun getInstance(project: Project) = + ServiceManager.getService(project, KotlinCommonCompilerArgumentsHolder::class.java)!! } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt index 4e9b94931fd..f783dcb7821 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerSettings.kt @@ -14,32 +14,20 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.compiler.configuration; +package org.jetbrains.kotlin.idea.compiler.configuration -import com.intellij.openapi.components.*; -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.config.CompilerSettings; +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.config.CompilerSettings +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMPILER_SETTINGS_SECTION -import static org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH; -import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMPILER_SETTINGS_SECTION; +@State(name = KOTLIN_COMPILER_SETTINGS_SECTION, + storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE), + Storage(file = BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED))) +class KotlinCompilerSettings : BaseKotlinCompilerSettings() { + override fun createSettings() = CompilerSettings() -@State( - name = KOTLIN_COMPILER_SETTINGS_SECTION, - storages = { - @Storage(file = StoragePathMacros.PROJECT_FILE), - @Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED) - } -) -public class KotlinCompilerSettings extends BaseKotlinCompilerSettings { - - public static KotlinCompilerSettings getInstance(Project project) { - return ServiceManager.getService(project, KotlinCompilerSettings.class); - } - - @NotNull - @Override - protected CompilerSettings createSettings() { - return new CompilerSettings(); + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, KotlinCompilerSettings::class.java)!! } } diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt index 3d6d88fe760..da344379a6a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.kt @@ -14,32 +14,21 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.compiler.configuration; +package org.jetbrains.kotlin.idea.compiler.configuration -import com.intellij.openapi.components.*; -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION +import org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.Companion.KOTLIN_COMPILER_SETTINGS_PATH -import static org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH; -import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION; +@State(name = KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION, + storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE), + Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED))) +class Kotlin2JsCompilerArgumentsHolder : BaseKotlinCompilerSettings() { + override fun createSettings() = K2JSCompilerArguments.createDefaultInstance() -@State( - name = KOTLIN_TO_JS_COMPILER_ARGUMENTS_SECTION, - storages = { - @Storage(file = StoragePathMacros.PROJECT_FILE), - @Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED) - } -) -public class Kotlin2JsCompilerArgumentsHolder extends BaseKotlinCompilerSettings { - - public static Kotlin2JsCompilerArgumentsHolder getInstance(Project project) { - return ServiceManager.getService(project, Kotlin2JsCompilerArgumentsHolder.class); - } - - @NotNull - @Override - protected K2JSCompilerArguments createSettings() { - return K2JSCompilerArguments.createDefaultInstance(); + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, Kotlin2JsCompilerArgumentsHolder::class.java)!! } } diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt index b74f54fa99e..1a4821d80d8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.kt @@ -14,32 +14,22 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.compiler.configuration; +package org.jetbrains.kotlin.idea.compiler.configuration -import com.intellij.openapi.components.*; -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; +import com.intellij.openapi.components.* +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments -import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION; -import static org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH; +import org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION +import org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.Companion.KOTLIN_COMPILER_SETTINGS_PATH -@State( - name = KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION, - storages = { - @Storage(file = StoragePathMacros.PROJECT_FILE), - @Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED) - } -) -public class Kotlin2JvmCompilerArgumentsHolder extends BaseKotlinCompilerSettings { +@State(name = KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION, + storages = arrayOf(Storage(file = StoragePathMacros.PROJECT_FILE), + Storage(file = KOTLIN_COMPILER_SETTINGS_PATH, scheme = StorageScheme.DIRECTORY_BASED))) +class Kotlin2JvmCompilerArgumentsHolder : BaseKotlinCompilerSettings() { + override fun createSettings() = K2JVMCompilerArguments.createDefaultInstance() - public static Kotlin2JvmCompilerArgumentsHolder getInstance(Project project) { - return ServiceManager.getService(project, Kotlin2JvmCompilerArgumentsHolder.class); - } - - @NotNull - @Override - protected K2JVMCompilerArguments createSettings() { - return K2JVMCompilerArguments.createDefaultInstance(); + companion object { + fun getInstance(project: Project) = ServiceManager.getService(project, Kotlin2JvmCompilerArgumentsHolder::class.java)!! } } diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java index fd7be0d867a..af73e23b250 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCompilerConfigurableTab.java @@ -175,10 +175,10 @@ public class KotlinCompilerConfigurableTab implements SearchableConfigurable, Co @SuppressWarnings("unused") public KotlinCompilerConfigurableTab(Project project) { this(project, - KotlinCommonCompilerArgumentsHolder.getInstance(project).getSettings(), - Kotlin2JsCompilerArgumentsHolder.getInstance(project).getSettings(), - Kotlin2JvmCompilerArgumentsHolder.getInstance(project).getSettings(), - KotlinCompilerSettings.getInstance(project).getSettings(), + KotlinCommonCompilerArgumentsHolder.Companion.getInstance(project).getSettings(), + Kotlin2JsCompilerArgumentsHolder.Companion.getInstance(project).getSettings(), + Kotlin2JvmCompilerArgumentsHolder.Companion.getInstance(project).getSettings(), + KotlinCompilerSettings.Companion.getInstance(project).getSettings(), ServiceManager.getService(project, KotlinCompilerWorkspaceSettings.class), true, false);