IJPL-159035 refactor ResourceTextDescriptor

GitOrigin-RevId: 224beea83c42094a2fffb07b4996da7af3d45506
This commit is contained in:
Vladimir Krivosheev
2024-07-30 15:55:41 +02:00
committed by intellij-monorepo-bot
parent bfc4e59ef7
commit fce9ac0789

View File

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