27
27
import com .diffplug .spotless .JarState ;
28
28
import com .diffplug .spotless .LineEnding ;
29
29
import com .diffplug .spotless .Provisioner ;
30
+ import com .diffplug .spotless .ThrowingEx .BiFunction ;
30
31
import com .diffplug .spotless .ThrowingEx .Function ;
31
32
32
33
/** Wraps up <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */
@@ -35,6 +36,7 @@ public class GoogleJavaFormatStep {
35
36
private GoogleJavaFormatStep () {}
36
37
37
38
private static final String DEFAULT_STYLE = "GOOGLE" ;
39
+ private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false ;
38
40
static final String NAME = "google-java-format" ;
39
41
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:" ;
40
42
static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter" ;
@@ -56,6 +58,9 @@ private GoogleJavaFormatStep() {}
56
58
private static final String IMPORT_ORDERER_CLASS = "com.google.googlejavaformat.java.ImportOrderer" ;
57
59
private static final String IMPORT_ORDERER_METHOD = "reorderImports" ;
58
60
61
+ private static final String STRING_WRAPPER_CLASS = "com.google.googlejavaformat.java.StringWrapper" ;
62
+ private static final String STRING_WRAPPER_METHOD = "wrap" ;
63
+
59
64
/** Creates a step which formats everything - code, import order, and unused imports. */
60
65
public static FormatterStep create (Provisioner provisioner ) {
61
66
return create (defaultVersion (), provisioner );
@@ -68,11 +73,16 @@ public static FormatterStep create(String version, Provisioner provisioner) {
68
73
69
74
/** Creates a step which formats everything - code, import order, and unused imports. */
70
75
public static FormatterStep create (String version , String style , Provisioner provisioner ) {
76
+ return create (version , style , provisioner , DEFAULT_REFLOW_LONG_STRINGS );
77
+ }
78
+
79
+ /** Creates a step which formats everything - code, import order, and unused imports - and optionally reflows long strings. */
80
+ public static FormatterStep create (String version , String style , Provisioner provisioner , boolean reflowLongStrings ) {
71
81
Objects .requireNonNull (version , "version" );
72
82
Objects .requireNonNull (style , "style" );
73
83
Objects .requireNonNull (provisioner , "provisioner" );
74
84
return FormatterStep .createLazy (NAME ,
75
- () -> new State (NAME , version , style , provisioner ),
85
+ () -> new State (NAME , version , style , provisioner , reflowLongStrings ),
76
86
State ::createFormat );
77
87
}
78
88
@@ -106,6 +116,10 @@ public static String defaultStyle() {
106
116
return DEFAULT_STYLE ;
107
117
}
108
118
119
+ public static boolean defaultReflowLongStrings () {
120
+ return DEFAULT_REFLOW_LONG_STRINGS ;
121
+ }
122
+
109
123
static final class State implements Serializable {
110
124
private static final long serialVersionUID = 1L ;
111
125
@@ -114,16 +128,22 @@ static final class State implements Serializable {
114
128
final String stepName ;
115
129
final String version ;
116
130
final String style ;
131
+ final boolean reflowLongStrings ;
117
132
118
133
State (String stepName , String version , Provisioner provisioner ) throws IOException {
119
134
this (stepName , version , DEFAULT_STYLE , provisioner );
120
135
}
121
136
122
137
State (String stepName , String version , String style , Provisioner provisioner ) throws IOException {
138
+ this (stepName , version , style , provisioner , DEFAULT_REFLOW_LONG_STRINGS );
139
+ }
140
+
141
+ State (String stepName , String version , String style , Provisioner provisioner , boolean reflowLongStrings ) throws IOException {
123
142
this .jarState = JarState .from (MAVEN_COORDINATE + version , provisioner );
124
143
this .stepName = stepName ;
125
144
this .version = version ;
126
145
this .style = style ;
146
+ this .reflowLongStrings = reflowLongStrings ;
127
147
}
128
148
129
149
@ SuppressWarnings ({"unchecked" , "rawtypes" })
@@ -153,11 +173,14 @@ FormatterFunc createFormat() throws Exception {
153
173
Class <?> importOrdererClass = classLoader .loadClass (IMPORT_ORDERER_CLASS );
154
174
Method importOrdererMethod = importOrdererClass .getMethod (IMPORT_ORDERER_METHOD , String .class );
155
175
176
+ BiFunction <String , Object , String > reflowLongStrings = this .reflowLongStrings ? constructReflowLongStringsFunction (classLoader , formatterClazz ) : (s , f ) -> s ;
177
+
156
178
return suggestJre11 (input -> {
157
179
String formatted = (String ) formatterMethod .invoke (formatter , input );
158
180
String removedUnused = removeUnused .apply (formatted );
159
181
String sortedImports = (String ) importOrdererMethod .invoke (null , removedUnused );
160
- return fixWindowsBug (sortedImports , version );
182
+ String reflowedLongStrings = reflowLongStrings .apply (sortedImports , formatter );
183
+ return fixWindowsBug (reflowedLongStrings , version );
161
184
});
162
185
}
163
186
@@ -191,6 +214,20 @@ private static Function<String, String> constructRemoveUnusedFunction(ClassLoade
191
214
}
192
215
return removeUnused ;
193
216
}
217
+
218
+ private static BiFunction <String , Object , String > constructReflowLongStringsFunction (ClassLoader classLoader , Class <?> formatterClazz ) throws NoSuchMethodException {
219
+ Class <?> stringWrapperClass ;
220
+ try {
221
+ stringWrapperClass = classLoader .loadClass (STRING_WRAPPER_CLASS );
222
+ } catch (ClassNotFoundException e ) {
223
+ // google-java-format 1.7 or lower, which happen to be LATEST_VERSION_JRE_8, so rely on suggestJre11 for the error
224
+ return (s , f ) -> {
225
+ throw e ;
226
+ };
227
+ }
228
+ Method stringWrapperMethod = stringWrapperClass .getMethod (STRING_WRAPPER_METHOD , String .class , formatterClazz );
229
+ return (s , f ) -> (String ) stringWrapperMethod .invoke (null , s , f );
230
+ }
194
231
}
195
232
196
233
private static final boolean IS_WINDOWS = LineEnding .PLATFORM_NATIVE .str ().equals ("\r \n " );
0 commit comments