Skip to content

Commit a5c8b4d

Browse files
committed
Fix classLoader issue
1 parent bcab5b4 commit a5c8b4d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/src/cleanthat/java/com/diffplug/spotless/glue/java/JavaCleanthatRefactorerFunc.java

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

18+
import java.io.IOException;
1819
import java.util.Arrays;
1920
import java.util.Collections;
2021
import java.util.List;
@@ -25,6 +26,7 @@
2526
import com.diffplug.spotless.FormatterFunc;
2627

2728
import eu.solven.cleanthat.config.pojo.CleanthatEngineProperties;
29+
import eu.solven.cleanthat.config.pojo.SourceCodeProperties;
2830
import eu.solven.cleanthat.engine.java.IJdkVersionConstants;
2931
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorer;
3032
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorerProperties;
@@ -53,8 +55,25 @@ public JavaCleanthatRefactorerFunc() {
5355

5456
@Override
5557
public String apply(String input) throws Exception {
58+
// https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader
59+
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
60+
try {
61+
// Ensure CleanThat main Thread has its custom classLoader while executing its refactoring
62+
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
63+
return doApply(input);
64+
} finally {
65+
// Restore the originalClassLoader
66+
Thread.currentThread().setContextClassLoader(originalClassLoader);
67+
}
68+
}
69+
70+
private String doApply(String input) throws InterruptedException, IOException {
71+
// call some API that uses reflection without taking ClassLoader param
5672
CleanthatEngineProperties engineProperties = CleanthatEngineProperties.builder().engineVersion(jdkVersion).build();
5773

74+
// Spotless will push us LF content
75+
engineProperties.setSourceCode(SourceCodeProperties.builder().lineEnding(LineEnding.LF).build());
76+
5877
JavaRefactorerProperties refactorerProperties = new JavaRefactorerProperties();
5978

6079
refactorerProperties.setIncluded(included);

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,21 @@ void testLiteralsFirstInComparisons() throws Exception {
3535
}
3636

3737
@Test
38-
void testMultipleMutators() throws Exception {
38+
void testMultipleMutators_defaultIsJdk7() throws Exception {
3939
writePomWithJavaSteps(
4040
"<cleanthat>",
4141
"</cleanthat>");
4242

43+
runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java");
44+
}
45+
46+
@Test
47+
void testMultipleMutators_Jdk11IntroducedOptionalisPresent() throws Exception {
48+
writePomWithJavaSteps(
49+
"<cleanthat>",
50+
"<sourceJdk>11</sourceJdk>",
51+
"</cleanthat>");
52+
4353
runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.java");
4454
}
4555

@@ -70,7 +80,8 @@ void testIncludeOnlyLiteralsFirstInComparisons() throws Exception {
7080
private void runTest(String dirtyPath, String cleanPath) throws Exception {
7181
String path = "src/main/java/test.java";
7282
setFile(path).toResource("java/cleanthat/" + dirtyPath);
73-
Assertions.assertThat(mavenRunner().withArguments("spotless:apply").withRemoteDebug(21654).runNoError().stdOutUtf8()).doesNotContain("[ERROR]");
83+
// .withRemoteDebug(21654)
84+
Assertions.assertThat(mavenRunner().withArguments("spotless:apply").runNoError().stdOutUtf8()).doesNotContain("[ERROR]");
7485
assertFile(path).sameAsResource("java/cleanthat/" + cleanPath);
7586
}
7687
}

testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class LiteralsFirstInComparisonsCases {
66

77
public boolean isHardcoded(String input) {
8-
return input.equals("hardcoded");
8+
return "hardcoded".equals(input);
99
}
1010

1111
public boolean isPresent(Optional<?> optional) {

0 commit comments

Comments
 (0)