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 .java ;
17
17
18
- import java .io .IOException ;
19
18
import java .io .Serializable ;
20
19
import java .lang .reflect .Constructor ;
21
20
import java .lang .reflect .Method ;
27
26
import com .diffplug .spotless .JarState ;
28
27
import com .diffplug .spotless .Jvm ;
29
28
import com .diffplug .spotless .Provisioner ;
29
+ import com .diffplug .spotless .RoundedStep ;
30
30
31
31
/**
32
32
* Enables CleanThat as a SpotLess step.
33
33
*
34
34
* @author Benoit Lacelle
35
35
*/
36
36
// https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-to-add-a-new-formatterstep
37
- public final class CleanthatJavaStep {
38
-
37
+ public final class CleanthatJavaStep implements RoundedStep {
38
+ private static final long serialVersionUID = 1L ;
39
39
private static final String NAME = "cleanthat" ;
40
40
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java" ;
41
-
42
- // CleanThat changelog is available at https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD
41
+ /**
42
+ * CleanThat changelog is available at <a href="https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD">here</a>.
43
+ */
43
44
private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (11 , "2.16" );
44
45
45
- // prevent direct instantiation
46
- private CleanthatJavaStep () {}
46
+ private final JarState .Promised jarState ;
47
+ private final String version ;
48
+ private final String sourceJdkVersion ;
49
+ private final List <String > included ;
50
+ private final List <String > excluded ;
51
+ private final boolean includeDraft ;
52
+
53
+ private CleanthatJavaStep (JarState .Promised jarState ,
54
+ String version ,
55
+ String sourceJdkVersion ,
56
+ List <String > included ,
57
+ List <String > excluded ,
58
+ boolean includeDraft ) {
59
+ this .jarState = jarState ;
60
+ this .version = version ;
61
+
62
+ this .sourceJdkVersion = sourceJdkVersion ;
63
+ this .included = included ;
64
+ this .excluded = excluded ;
65
+ this .includeDraft = includeDraft ;
66
+ }
47
67
48
- /** Creates a step which apply default CleanThat mutators. */
68
+ /** Creates a step that applies default CleanThat mutators. */
49
69
public static FormatterStep create (Provisioner provisioner ) {
50
70
return create (defaultVersion (), provisioner );
51
71
}
52
72
53
- /** Creates a step which apply default CleanThat mutators. */
73
+ /** Creates a step that applies default CleanThat mutators. */
54
74
public static FormatterStep create (String version , Provisioner provisioner ) {
55
75
return create (MAVEN_COORDINATE , version , defaultSourceJdk (), defaultMutators (), defaultExcludedMutators (), defaultIncludeDraft (), provisioner );
56
76
}
@@ -64,7 +84,6 @@ public static String defaultSourceJdk() {
64
84
65
85
/**
66
86
* By default, we include only safe and consensual mutators
67
- * @return
68
87
*/
69
88
public static List <String > defaultMutators () {
70
89
// see ICleanthatStepParametersProperties.SAFE_AND_CONSENSUAL
@@ -79,7 +98,7 @@ public static boolean defaultIncludeDraft() {
79
98
return false ;
80
99
}
81
100
82
- /** Creates a step which apply selected CleanThat mutators. */
101
+ /** Creates a step that applies selected CleanThat mutators. */
83
102
public static FormatterStep create (String groupArtifact ,
84
103
String version ,
85
104
String sourceJdkVersion ,
@@ -93,57 +112,51 @@ public static FormatterStep create(String groupArtifact,
93
112
}
94
113
Objects .requireNonNull (version , "version" );
95
114
Objects .requireNonNull (provisioner , "provisioner" );
96
- return FormatterStep .createLazy (NAME ,
97
- () -> new JavaRefactorerState (NAME , groupArtifact , version , sourceJdkVersion , included , excluded , includeDraft , provisioner ),
98
- JavaRefactorerState ::createFormat );
115
+ return FormatterStep .create (NAME ,
116
+ new CleanthatJavaStep (JarState .promise (() -> JarState .from (groupArtifact + ":" + version , provisioner )), version , sourceJdkVersion , included , excluded , includeDraft ),
117
+ CleanthatJavaStep ::equalityState ,
118
+ State ::createFormat );
99
119
}
100
120
101
121
/** Get default formatter version */
102
122
public static String defaultVersion () {
103
- return JVM_SUPPORT .getRecommendedFormatterVersion ();
123
+ return Objects . requireNonNull ( JVM_SUPPORT .getRecommendedFormatterVersion () );
104
124
}
105
125
106
126
public static String defaultGroupArtifact () {
107
127
return MAVEN_COORDINATE ;
108
128
}
109
129
110
- static final class JavaRefactorerState implements Serializable {
111
- private static final long serialVersionUID = 1L ;
112
-
113
- final JarState jarState ;
114
- final String stepName ;
115
- final String version ;
130
+ private State equalityState () {
131
+ return new State (jarState .get (), version , sourceJdkVersion , included , excluded , includeDraft );
132
+ }
116
133
117
- final String sourceJdkVersion ;
118
- final List <String > included ;
119
- final List <String > excluded ;
120
- final boolean includeDraft ;
134
+ private static final class State implements Serializable {
135
+ private static final long serialVersionUID = 1L ;
121
136
122
- JavaRefactorerState (String stepName , String version , Provisioner provisioner ) throws IOException {
123
- this (stepName , MAVEN_COORDINATE , version , defaultSourceJdk (), defaultMutators (), defaultExcludedMutators (), defaultIncludeDraft (), provisioner );
124
- }
137
+ private final JarState jarState ;
138
+ private final String version ;
139
+ private final String sourceJdkVersion ;
140
+ private final List <String > included ;
141
+ private final List <String > excluded ;
142
+ private final boolean includeDraft ;
125
143
126
- JavaRefactorerState (String stepName ,
127
- String groupArtifact ,
144
+ State (JarState jarState ,
128
145
String version ,
129
146
String sourceJdkVersion ,
130
147
List <String > included ,
131
148
List <String > excluded ,
132
- boolean includeDraft ,
133
- Provisioner provisioner ) throws IOException {
149
+ boolean includeDraft ) {
134
150
JVM_SUPPORT .assertFormatterSupported (version );
135
151
ModuleHelper .doOpenInternalPackagesIfRequired ();
136
- this .jarState = JarState .from (groupArtifact + ":" + version , provisioner );
137
- this .stepName = stepName ;
152
+ this .jarState = jarState ;
138
153
this .version = version ;
139
-
140
154
this .sourceJdkVersion = sourceJdkVersion ;
141
155
this .included = included ;
142
156
this .excluded = excluded ;
143
157
this .includeDraft = includeDraft ;
144
158
}
145
159
146
- @ SuppressWarnings ("PMD.UseProperClassLoader" )
147
160
FormatterFunc createFormat () {
148
161
ClassLoader classLoader = jarState .getClassLoader ();
149
162
@@ -159,9 +172,7 @@ FormatterFunc createFormat() {
159
172
throw new IllegalStateException ("Issue executing the formatter" , e );
160
173
}
161
174
162
- return JVM_SUPPORT .suggestLaterVersionOnError (version , input -> {
163
- return (String ) formatterMethod .invoke (formatter , input );
164
- });
175
+ return JVM_SUPPORT .suggestLaterVersionOnError (version , input -> (String ) formatterMethod .invoke (formatter , input ));
165
176
}
166
177
}
167
178
}
0 commit comments