Skip to content

Commit a8ef331

Browse files
committed
Fix spotbugs issues.
1 parent 68e1ca9 commit a8ef331

File tree

8 files changed

+43
-38
lines changed

8 files changed

+43
-38
lines changed

lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18-
import java.lang.reflect.Field;
1918
import java.lang.reflect.InvocationTargetException;
2019
import java.lang.reflect.Method;
2120
import java.nio.file.Files;
@@ -125,7 +124,7 @@ public String format(
125124
String unix,
126125
Path path,
127126
Path editorConfigPath,
128-
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
127+
Map<String, Object> editorConfigOverrideMap) {
129128
final FormatterCallback formatterCallback = new FormatterCallback();
130129

131130
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -150,10 +149,7 @@ public String format(
150149

151150
// create Code and then set the content to match previous steps in the Spotless pipeline
152151
Code code = Code.Companion.fromPath(path);
153-
Field contentField = code.getClass().getDeclaredField("content");
154-
contentField.setAccessible(true);
155-
contentField.set(code, unix);
156-
152+
KtLintCompatAdapter.setCodeContent(code, unix);
157153
return new KtLintRuleEngine(
158154
allRuleProviders,
159155
editorConfig,

lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18-
import java.lang.reflect.Field;
1918
import java.nio.file.Files;
2019
import java.nio.file.Path;
2120
import java.util.List;
@@ -87,7 +86,7 @@ public String format(
8786
String unix,
8887
Path path,
8988
Path editorConfigPath,
90-
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
89+
Map<String, Object> editorConfigOverrideMap) {
9190
final FormatterCallback formatterCallback = new FormatterCallback();
9291

9392
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -112,10 +111,7 @@ public String format(
112111

113112
// create Code and then set the content to match previous steps in the Spotless pipeline
114113
Code code = Code.Companion.fromPath(path);
115-
Field contentField = code.getClass().getDeclaredField("content");
116-
contentField.setAccessible(true);
117-
contentField.set(code, unix);
118-
114+
KtLintCompatAdapter.setCodeContent(code, unix);
119115
return new KtLintRuleEngine(
120116
allRuleProviders,
121117
editorConfig,

lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18-
import java.lang.reflect.Field;
1918
import java.nio.file.Path;
2019
import java.util.List;
2120
import java.util.Map;
@@ -86,7 +85,7 @@ public String format(
8685
String unix,
8786
Path path,
8887
Path editorConfigPath,
89-
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException {
88+
Map<String, Object> editorConfigOverrideMap) {
9089
final FormatterCallback formatterCallback = new FormatterCallback();
9190

9291
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
@@ -107,10 +106,7 @@ public String format(
107106

108107
// create Code and then set the content to match previous steps in the Spotless pipeline
109108
Code code = Code.Companion.fromPath(path);
110-
Field contentField = code.getClass().getDeclaredField("content");
111-
contentField.setAccessible(true);
112-
contentField.set(code, unix);
113-
109+
KtLintCompatAdapter.setCodeContent(code, unix);
114110
return new KtLintRuleEngine(
115111
allRuleProviders,
116112
editorConfig,

lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.lang.reflect.Field;
1819
import java.nio.file.Path;
20+
import java.security.AccessController;
21+
import java.security.PrivilegedAction;
1922
import java.util.Map;
2023

2124
public interface KtLintCompatAdapter {
@@ -25,4 +28,18 @@ String format(
2528
Path path,
2629
Path editorConfigPath,
2730
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException;
31+
32+
static void setCodeContent(Object code, String content) {
33+
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
34+
try {
35+
Field contentField = code.getClass().getDeclaredField("content");
36+
contentField.setAccessible(true);
37+
contentField.set(code, content);
38+
} catch (NoSuchFieldException | IllegalAccessException e) {
39+
// Handle exceptions as needed
40+
throw new RuntimeException("Failed to set content field", e);
41+
}
42+
return null;
43+
});
44+
}
2845
}

lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,19 +33,19 @@ public class KtLintCompat0Dot48Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
36-
loadAndWriteText(path, "empty_class_body.kt");
36+
var content = loadAndWriteText(path, "empty_class_body.kt");
3737
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt");
3838

3939
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
4040

41-
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class empty_class_body\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
48-
loadAndWriteText(path, "fails_no_semicolons.kt");
48+
var content = loadAndWriteText(path, "fails_no_semicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt");
5050

5151
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
@@ -54,7 +54,7 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
5454
// ktlint_filename is an invalid rule in ktlint 0.48.0
5555
editorConfigOverrideMap.put("ktlint_filename", "disabled");
5656

57-
String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
57+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5858
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
5959
}
6060

lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,26 +33,26 @@ public class KtLintCompat0Dot49Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

3939
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
4040

41-
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

5151
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
5252
editorConfigOverrideMap.put("indent_style", "tab");
5353
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
5454

55-
String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,26 +33,26 @@ public class KtLintCompat0Dot50Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

3939
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
4040

41-
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

5151
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
5252
editorConfigOverrideMap.put("indent_style", "tab");
5353
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
5454

55-
String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,26 +33,26 @@ public class KtLintCompat1Dot0Dot0AdapterTest {
3333
@Test
3434
public void testDefaults(@TempDir Path path) throws IOException {
3535
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
36-
loadAndWriteText(path, "EmptyClassBody.kt");
36+
var content = loadAndWriteText(path, "EmptyClassBody.kt");
3737
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
3838

3939
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
4040

41-
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
41+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
4242
assertEquals("class EmptyClassBody\n", formatted);
4343
}
4444

4545
@Test
4646
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
4747
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
48-
loadAndWriteText(path, "FailsNoSemicolons.kt");
48+
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
4949
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
5050

5151
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
5252
editorConfigOverrideMap.put("indent_style", "tab");
5353
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
5454

55-
String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
55+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
5656
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
5757
}
5858

0 commit comments

Comments
 (0)