@@ -32,7 +32,7 @@ public class KtfmtStep {
32
32
// prevent direct instantiation
33
33
private KtfmtStep () {}
34
34
35
- private static final String DEFAULT_VERSION = "0.30 " ;
35
+ private static final String DEFAULT_VERSION = "0.31 " ;
36
36
static final String NAME = "ktfmt" ;
37
37
static final String PACKAGE = "com.facebook" ;
38
38
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:" ;
@@ -120,18 +120,16 @@ static final class State implements Serializable {
120
120
121
121
FormatterFunc createFormat () throws Exception {
122
122
ClassLoader classLoader = jarState .getClassLoader ();
123
- Class <?> formatterClazz = classLoader .loadClass (pkg + ".ktfmt.FormatterKt" );
124
123
return input -> {
125
124
try {
126
125
if (style == DEFAULT ) {
127
- Method formatterMethod = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
128
- return (String ) formatterMethod .invoke (formatterClazz , input );
126
+ Method formatterMethod = getFormatterClazz ( classLoader ) .getMethod (FORMATTER_METHOD , String .class );
127
+ return (String ) formatterMethod .invoke (getFormatterClazz ( classLoader ) , input );
129
128
} else {
130
- Class <?> formattingOptionsClazz = classLoader .loadClass (pkg + ".ktfmt.FormattingOptions" );
131
- Method formatterMethod = formatterClazz .getMethod (FORMATTER_METHOD , formattingOptionsClazz ,
129
+ Method formatterMethod = getFormatterClazz (classLoader ).getMethod (FORMATTER_METHOD , getFormattingOptionsClazz (classLoader ),
132
130
String .class );
133
131
Object formattingOptions = getCustomFormattingOptions (classLoader , style );
134
- return (String ) formatterMethod .invoke (formatterClazz , formattingOptions , input );
132
+ return (String ) formatterMethod .invoke (getFormatterClazz ( classLoader ) , formattingOptions , input );
135
133
}
136
134
} catch (InvocationTargetException e ) {
137
135
throw ThrowingEx .unwrapCause (e );
@@ -146,7 +144,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
146
144
147
145
try {
148
146
// ktfmt v0.19 and later
149
- return classLoader . loadClass ( pkg + ".ktfmt.FormatterKt" ).getField (style .getFormat ()).get (null );
147
+ return getFormatterClazz ( classLoader ).getField (style .getFormat ()).get (null );
150
148
} catch (NoSuchFieldException ignored ) {}
151
149
152
150
// fallback to old, pre-0.19 ktfmt interface.
@@ -159,5 +157,25 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
159
157
throw new IllegalStateException ("Versions pre-0.19 can only use Default and Dropbox styles" );
160
158
}
161
159
}
160
+
161
+ private Class <?> getFormatterClazz (ClassLoader classLoader ) throws Exception {
162
+ Class <?> formatterClazz ;
163
+ if (BadSemver .version (version ) >= BadSemver .version (0 , 31 )) {
164
+ formatterClazz = classLoader .loadClass (pkg + ".ktfmt.format.Formatter" );
165
+ } else {
166
+ formatterClazz = classLoader .loadClass (pkg + ".ktfmt.FormatterKt" );
167
+ }
168
+ return formatterClazz ;
169
+ }
170
+
171
+ private Class <?> getFormattingOptionsClazz (ClassLoader classLoader ) throws Exception {
172
+ Class <?> formattingOptionsClazz ;
173
+ if (BadSemver .version (version ) >= BadSemver .version (0 , 31 )) {
174
+ formattingOptionsClazz = classLoader .loadClass (pkg + ".ktfmt.format.FormattingOptions" );
175
+ } else {
176
+ formattingOptionsClazz = classLoader .loadClass (pkg + ".ktfmt.FormattingOptions" );
177
+ }
178
+ return formattingOptionsClazz ;
179
+ }
162
180
}
163
181
}
0 commit comments