From 69dfb70209b3a3529abab38abd0c0b2d6ebe464f Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 2 Nov 2017 15:51:11 +0100 Subject: [PATCH] fix possible NPE because Kotlin toBoolean doesn't accept null opposite to Java API. --- .../ProjectInspectionProfileManager.kt | 23 +++---------- .../settings/ParameterNameHintsSettings.kt | 32 +++++++++---------- .../intellij/execution/impl/RunManagerImpl.kt | 5 +-- .../RunnerAndConfigurationSettingsImpl.kt | 7 ++-- .../src/com/intellij/util/jdom.kt | 20 +++--------- 5 files changed, 30 insertions(+), 57 deletions(-) diff --git a/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt b/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt index e6e471c033f0..b4d827f3d75f 100644 --- a/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt +++ b/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.profile.codeInspection import com.intellij.codeInspection.InspectionProfile @@ -37,6 +23,7 @@ import com.intellij.project.isDirectoryBased import com.intellij.psi.search.scope.packageSet.NamedScopeManager import com.intellij.psi.search.scope.packageSet.NamedScopesHolder import com.intellij.util.containers.ContainerUtil +import com.intellij.util.getAttributeBooleanValue import com.intellij.util.loadElement import com.intellij.util.xmlb.Accessor import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters @@ -238,10 +225,8 @@ class ProjectInspectionProfileManager(val project: Project, if (data != null && data.getChild("version")?.getAttributeValue("value") != VERSION) { for (o in data.getChildren("option")) { if (o.getAttributeValue("name") == "USE_PROJECT_LEVEL_SETTINGS") { - if (o.getAttributeValue("value").toBoolean()) { - if (newState.projectProfile != null) { - currentProfile.convert(data, project) - } + if (o.getAttributeBooleanValue("value") && newState.projectProfile != null) { + currentProfile.convert(data, project) } break } diff --git a/platform/lang-api/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsSettings.kt b/platform/lang-api/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsSettings.kt index 4aec8b961ba3..3cb1f471f23d 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsSettings.kt +++ b/platform/lang-api/src/com/intellij/codeInsight/hints/settings/ParameterNameHintsSettings.kt @@ -1,18 +1,16 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2017 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 com.intellij.codeInsight.hints.settings import com.intellij.lang.Language @@ -20,6 +18,7 @@ import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.components.State import com.intellij.openapi.components.Storage +import com.intellij.util.getAttributeBooleanValue import org.jdom.Element @@ -133,8 +132,7 @@ class ParameterNameHintsSettings : PersistentStateComponent { state.getChildren("option").forEach { val id = it.getAttributeValue("id") - val value = it.getAttributeValue("value").toBoolean() - options[id] = value + options[id] = it.getAttributeBooleanValue("value") } } diff --git a/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt b/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt index fdc792ce54d3..83f4c7eddf93 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt +++ b/platform/lang-impl/src/com/intellij/execution/impl/RunManagerImpl.kt @@ -27,6 +27,7 @@ import com.intellij.openapi.util.registry.Registry import com.intellij.util.IconUtil import com.intellij.util.SmartList import com.intellij.util.containers.* +import com.intellij.util.getAttributeBooleanValue import com.intellij.util.text.UniqueNameGenerator import gnu.trove.THashMap import org.jdom.Element @@ -605,7 +606,7 @@ open class RunManagerImpl(internal val project: Project) : RunManagerEx(), Persi for (element in parentNode.children) { val config = loadConfiguration(element, false) - if (selectedConfigurationId == null && element.getAttributeValue(SELECTED_ATTR).toBoolean()) { + if (selectedConfigurationId == null && element.getAttributeBooleanValue(SELECTED_ATTR)) { selectedConfigurationId = config.uniqueID } } @@ -693,7 +694,7 @@ open class RunManagerImpl(internal val project: Project) : RunManagerEx(), Persi } else { addConfiguration(settings) - if (element.getAttributeValue(SELECTED_ATTR).toBoolean()) { + if (element.getAttributeBooleanValue(SELECTED_ATTR)) { // to support old style selectedConfiguration = settings } diff --git a/platform/lang-impl/src/com/intellij/execution/impl/RunnerAndConfigurationSettingsImpl.kt b/platform/lang-impl/src/com/intellij/execution/impl/RunnerAndConfigurationSettingsImpl.kt index d167aa2ce179..622e99001b89 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/RunnerAndConfigurationSettingsImpl.kt +++ b/platform/lang-impl/src/com/intellij/execution/impl/RunnerAndConfigurationSettingsImpl.kt @@ -22,6 +22,7 @@ import com.intellij.openapi.util.* import com.intellij.openapi.util.text.StringUtil import com.intellij.util.PathUtilRt import com.intellij.util.SmartList +import com.intellij.util.getAttributeBooleanValue import gnu.trove.THashMap import gnu.trove.THashSet import org.jdom.Element @@ -148,16 +149,16 @@ class RunnerAndConfigurationSettingsImpl @JvmOverloads constructor(private val m override fun getFolderName() = folderName fun readExternal(element: Element, isShared: Boolean) { - isTemplate = element.getAttributeValue(TEMPLATE_FLAG_ATTRIBUTE).toBoolean() + isTemplate = element.getAttributeBooleanValue(TEMPLATE_FLAG_ATTRIBUTE) if (isShared) { level = RunConfigurationLevel.PROJECT } else { - level = if (element.getAttributeValue(TEMPORARY_ATTRIBUTE).toBoolean() || TEMP_CONFIGURATION == element.name) RunConfigurationLevel.TEMPORARY else RunConfigurationLevel.WORKSPACE + level = if (element.getAttributeBooleanValue(TEMPORARY_ATTRIBUTE) || TEMP_CONFIGURATION == element.name) RunConfigurationLevel.TEMPORARY else RunConfigurationLevel.WORKSPACE } - isEditBeforeRun = (element.getAttributeValue(EDIT_BEFORE_RUN)).toBoolean() + isEditBeforeRun = (element.getAttributeBooleanValue(EDIT_BEFORE_RUN)) val value = element.getAttributeValue(ACTIVATE_TOOLWINDOW_BEFORE_RUN) isActivateToolWindowBeforeRun = value == null || value.toBoolean() folderName = element.getAttributeValue(FOLDER_NAME) diff --git a/platform/projectModel-api/src/com/intellij/util/jdom.kt b/platform/projectModel-api/src/com/intellij/util/jdom.kt index 7809bbcca5bf..711ad514e8d5 100644 --- a/platform/projectModel-api/src/com/intellij/util/jdom.kt +++ b/platform/projectModel-api/src/com/intellij/util/jdom.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util import com.intellij.openapi.util.JDOMUtil @@ -140,4 +126,6 @@ fun Parent.toBufferExposingByteArray(lineSeparator: String = "\n"): BufferExposi val out = BufferExposingByteArrayOutputStream(512) JDOMUtil.write(this, out, lineSeparator) return out -} \ No newline at end of file +} + +fun Element.getAttributeBooleanValue(name: String): Boolean = java.lang.Boolean.parseBoolean(getAttributeValue(name)) \ No newline at end of file