From fce9ac07897716ae4d12aa8524f72c756353af8b Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 30 Jul 2024 15:55:41 +0200 Subject: [PATCH] IJPL-159035 refactor ResourceTextDescriptor GitOrigin-RevId: 224beea83c42094a2fffb07b4996da7af3d45506 --- .../impl/config/ResourceTextDescriptor.kt | 72 ++++++------------- 1 file changed, 23 insertions(+), 49 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/intention/impl/config/ResourceTextDescriptor.kt b/platform/lang-impl/src/com/intellij/codeInsight/intention/impl/config/ResourceTextDescriptor.kt index 79d3074d3aed..674026f395e0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/intention/impl/config/ResourceTextDescriptor.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/intention/impl/config/ResourceTextDescriptor.kt @@ -1,60 +1,34 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.codeInsight.intention.impl.config; +package com.intellij.codeInsight.intention.impl.config -import com.intellij.l10n.LocalizationUtil; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.text.Strings; -import com.intellij.util.ResourceUtil; -import org.jetbrains.annotations.NotNull; +import com.intellij.l10n.LocalizationUtil.getResourceAsStream +import com.intellij.openapi.diagnostic.thisLogger +import com.intellij.util.ResourceUtil +import java.io.IOException -import java.io.IOException; -import java.io.InputStream; -import java.util.Objects; - -final class ResourceTextDescriptor implements TextDescriptor { - private static final Logger LOG = Logger.getInstance(ResourceTextDescriptor.class); - private final ClassLoader loader; - private final String resourcePath; - - ResourceTextDescriptor(ClassLoader loader, @NotNull String resourcePath) { - this.loader = loader; - this.resourcePath = resourcePath; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - ResourceTextDescriptor resource = (ResourceTextDescriptor)o; - return Objects.equals(loader, resource.loader) && - Objects.equals(resourcePath, resource.resourcePath); - } - - @Override - public int hashCode() { - return Objects.hash(loader, resourcePath); - } - - @Override - public @NotNull String getText() throws IOException { - InputStream inputStream = LocalizationUtil.INSTANCE.getResourceAsStream(loader, resourcePath); +internal data class ResourceTextDescriptor( + private val loader: ClassLoader, + private val resourcePath: String, +) : TextDescriptor { + @Throws(IOException::class) + override fun getText(): String { + val inputStream = getResourceAsStream(loader, resourcePath) if (inputStream != null) { - try (inputStream) { - return ResourceUtil.loadText(inputStream); //NON-NLS + try { + inputStream.use { + return ResourceUtil.loadText(inputStream) + } } - catch (IOException e) { - LOG.error("Cannot find localized resource: " + resourcePath, e); + catch (e: IOException) { + thisLogger().error("Cannot find localized resource: $resourcePath", e) } } - InputStream stream = loader.getResourceAsStream(resourcePath); - if (stream == null) { - throw new IOException("Resource not found: " + resourcePath + "; loader: " + loader); - } - return ResourceUtil.loadText(stream); //NON-NLS + + val stream = loader.getResourceAsStream(resourcePath) ?: throw IOException("Resource not found: $resourcePath; loader: $loader") + return ResourceUtil.loadText(stream) } - @Override - public @NotNull String getFileName() { - return Strings.trimEnd(resourcePath.substring(resourcePath.lastIndexOf('/') + 1), BeforeAfterActionMetaData.EXAMPLE_USAGE_URL_SUFFIX); + override fun getFileName(): String { + return resourcePath.substring(resourcePath.lastIndexOf('/') + 1).removeSuffix(BeforeAfterActionMetaData.EXAMPLE_USAGE_URL_SUFFIX) } } \ No newline at end of file