Skip to content

Commit a58bdc7

Browse files
committed
Make it safer
1 parent ad8dd4b commit a58bdc7

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎open-java-format-idea-plugin/src/main/java/com/palantir/javaformat/intellij/FormatterProvider.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
import com.github.benmanes.caffeine.cache.Caffeine;
2020
import com.github.benmanes.caffeine.cache.LoadingCache;
21-
import com.intellij.formatting.service.FormattingService;
2221
import com.intellij.openapi.extensions.PluginDescriptor;
2322
import com.intellij.openapi.project.Project;
2423
import com.intellij.openapi.util.SystemInfo;
2524
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
2625
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
2726
import com.palantir.javaformat.java.FormatterService;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
2830
import java.io.IOException;
2931
import java.io.UncheckedIOException;
3032
import java.net.URI;
@@ -35,8 +37,9 @@
3537
import java.util.Optional;
3638
import java.util.stream.Collectors;
3739
import java.util.stream.Stream;
38-
import org.slf4j.Logger;
39-
import org.slf4j.LoggerFactory;
40+
41+
import static com.intellij.formatting.service.FormattingService.EP_NAME;
42+
import static java.util.Optional.ofNullable;
4043

4144
final class FormatterProvider {
4245
private static final Logger log = LoggerFactory.getLogger(FormatterProvider.class);
@@ -51,10 +54,9 @@ final class FormatterProvider {
5154
* extension point finds the service by class. Every way of looking a plugin up by id or by class became
5255
* {@code @ApiStatus.Internal} in 2026.2; PluginAware and the extension point are public API in every supported IDE.
5356
*/
54-
static PluginDescriptor getPluginDescriptor() {
55-
return FormattingService.EP_NAME
56-
.findExtensionOrFail(PalantirJavaFormatFormattingService.class)
57-
.getPluginDescriptor();
57+
static Optional<PluginDescriptor> getPluginDescriptor() {
58+
return ofNullable(EP_NAME.findExtension(PalantirJavaFormatFormattingService.class))
59+
.map(PalantirJavaFormatFormattingService::getPluginDescriptor);
5860
}
5961

6062
Optional<FormatterService> get(Project project, PalantirJavaFormatSettings settings) {
@@ -90,9 +92,10 @@ private static List<Path> getProvidedImplementationUrls(List<URI> implementation
9092
@SuppressWarnings("for-rollout:Slf4jLogsafeArgs")
9193
private static List<Path> getBundledImplementationUrls() {
9294
// Load from the jars bundled with the plugin.
93-
PluginDescriptor ourPlugin = getPluginDescriptor();
94-
Path implDir = ourPlugin.getPluginPath().resolve("impl");
95+
Path implDir = getPluginDescriptor().map(PluginDescriptor::getPluginPath).orElseThrow().resolve("impl");
96+
9597
log.debug("Using open-java-format implementation bundled with plugin: {}", implDir);
98+
9699
return listDirAsUrlsUnchecked(implDir);
97100
}
98101

‎open-java-format-idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatSettings.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import com.intellij.openapi.components.PersistentStateComponent;
2020
import com.intellij.openapi.components.State;
2121
import com.intellij.openapi.components.Storage;
22+
import com.intellij.openapi.extensions.PluginDescriptor;
2223
import com.intellij.openapi.project.Project;
2324
import com.palantir.javaformat.java.FormatterService;
2425
import com.palantir.javaformat.java.JavaFormatterOptions;
26+
27+
import javax.annotation.Nullable;
2528
import java.io.IOException;
2629
import java.io.UncheckedIOException;
2730
import java.net.URI;
@@ -30,7 +33,8 @@
3033
import java.util.jar.JarFile;
3134
import java.util.stream.Collectors;
3235
import java.util.stream.Stream;
33-
import javax.annotation.Nullable;
36+
37+
import static com.palantir.javaformat.intellij.FormatterProvider.getPluginDescriptor;
3438

3539
@SuppressWarnings("for-rollout:SameNameButDifferent")
3640
@State(
@@ -109,7 +113,7 @@ boolean injectedVersionIsOutdated() {
109113
}
110114

111115
Optional<String> getImplementationVersion() {
112-
return Optional.ofNullable(FormatterProvider.getPluginDescriptor().getVersion());
116+
return getPluginDescriptor().map(PluginDescriptor::getVersion);
113117
}
114118

115119
Optional<String> computeFormatterVersion() {

‎open-java-format-idea-plugin/src/test/java/com/palantir/javaformat/intellij/PalantirJavaFormatFormattingServiceTest.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.palantir.javaformat.intellij;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
2119
import com.google.common.collect.ImmutableList;
2220
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
2321
import com.intellij.formatting.service.AsyncFormattingRequest;
@@ -39,16 +37,20 @@
3937
import com.intellij.testFramework.fixtures.JavaTestFixtureFactory;
4038
import com.intellij.testFramework.fixtures.TestFixtureBuilder;
4139
import com.palantir.javaformat.intellij.PalantirJavaFormatSettings.State;
40+
import org.jetbrains.annotations.NotNull;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.BeforeEach;
43+
import org.junit.jupiter.api.Test;
44+
4245
import java.io.File;
4346
import java.io.IOException;
4447
import java.io.UncheckedIOException;
4548
import java.nio.file.Files;
4649
import java.nio.file.Path;
4750
import java.nio.file.Paths;
48-
import org.jetbrains.annotations.NotNull;
49-
import org.junit.jupiter.api.AfterEach;
50-
import org.junit.jupiter.api.BeforeEach;
51-
import org.junit.jupiter.api.Test;
51+
52+
import static com.palantir.javaformat.intellij.FormatterProvider.getPluginDescriptor;
53+
import static org.assertj.core.api.Assertions.assertThat;
5254

5355
public class PalantirJavaFormatFormattingServiceTest {
5456
private JavaCodeInsightTestFixture fixture;
@@ -65,7 +67,7 @@ public void setUp() throws Exception {
6567
delegatingFormatter = new DelegatingFormatter();
6668
// Only the service the platform creates from plugin.xml is handed the plugin descriptor. This one is created
6769
// here, so it takes the descriptor from that service before masking it.
68-
delegatingFormatter.setPluginDescriptor(FormatterProvider.getPluginDescriptor());
70+
delegatingFormatter.setPluginDescriptor(getPluginDescriptor().orElseThrow());
6971
ExtensionTestUtil.maskExtensions(
7072
FormattingService.EP_NAME, ImmutableList.of(delegatingFormatter), fixture.getProjectDisposable());
7173

0 commit comments

Comments
 (0)