1
1
/*
2
- * Copyright 2023 DiffPlug
2
+ * Copyright 2023-2024 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .spotless .glue .java ;
17
17
18
+ import java .io .File ;
18
19
import java .io .IOException ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collections ;
31
32
import eu .solven .cleanthat .engine .java .refactorer .JavaRefactorer ;
32
33
import eu .solven .cleanthat .engine .java .refactorer .JavaRefactorerProperties ;
33
34
import eu .solven .cleanthat .formatter .LineEnding ;
35
+ import eu .solven .cleanthat .formatter .PathAndContent ;
34
36
35
37
/**
36
38
* The glue for CleanThat: it is build over the version in build.gradle, but at runtime it will be executed over
37
39
* the version loaded in JarState, which is by default defined in com.diffplug.spotless.java.CleanthatJavaStep#JVM_SUPPORT
38
40
*/
39
- public class JavaCleanthatRefactorerFunc implements FormatterFunc {
41
+ public class JavaCleanthatRefactorerFunc implements FormatterFunc . NeedsFile {
40
42
private static final Logger LOGGER = LoggerFactory .getLogger (JavaCleanthatRefactorerFunc .class );
41
43
42
44
private String jdkVersion ;
@@ -56,20 +58,20 @@ public JavaCleanthatRefactorerFunc() {
56
58
}
57
59
58
60
@ Override
59
- public String apply (String input ) throws Exception {
61
+ public String applyWithFile (String unix , File file ) throws Exception {
60
62
// https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader
61
63
ClassLoader originalClassLoader = Thread .currentThread ().getContextClassLoader ();
62
64
try {
63
65
// Ensure CleanThat main Thread has its custom classLoader while executing its refactoring
64
66
Thread .currentThread ().setContextClassLoader (getClass ().getClassLoader ());
65
- return doApply (input );
67
+ return doApply (unix , file );
66
68
} finally {
67
69
// Restore the originalClassLoader
68
70
Thread .currentThread ().setContextClassLoader (originalClassLoader );
69
71
}
70
72
}
71
73
72
- private String doApply (String input ) throws InterruptedException , IOException {
74
+ private String doApply (String input , File file ) throws IOException {
73
75
// call some API that uses reflection without taking ClassLoader param
74
76
CleanthatEngineProperties engineProperties = CleanthatEngineProperties .builder ().engineVersion (jdkVersion ).build ();
75
77
@@ -88,7 +90,9 @@ private String doApply(String input) throws InterruptedException, IOException {
88
90
LOGGER .debug ("Processing sourceJdk={} included={} excluded={}" , jdkVersion , included , excluded , includeDraft );
89
91
LOGGER .debug ("Available mutators: {}" , JavaRefactorer .getAllIncluded ());
90
92
91
- return refactorer .doFormat (input );
93
+ PathAndContent pathAndContent = new PathAndContent (file .toPath (), input );
94
+
95
+ return refactorer .doFormat (pathAndContent );
92
96
}
93
97
94
98
}
0 commit comments