Skip to content

Commit

Permalink
Use guaranteed binary and source compatibility between releases of Sc…
Browse files Browse the repository at this point in the history
…alafmt
  • Loading branch information
Joan Goyeau committed Jul 9, 2018
1 parent 4979069 commit 3ae984a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,34 @@ FormatterFunc createFormat() throws Exception {

Class<?> optionCls = classLoader.loadClass("scala.Option");
Class<?> configCls = classLoader.loadClass("org.scalafmt.config.Config");
Class<?> scalafmtCls = classLoader.loadClass("org.scalafmt.Scalafmt");

Object either;

try {
// scalafmt >= v0.7.0-RC1
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);
// scalafmt >= 1.6.0
Method parseHoconConfig = scalafmtCls.getMethod("parseHoconConfig", String.class);

String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
Object configured = parseHoconConfig.invoke(null, configStr);
either = invokeNoArg(configured, "toEither");
} catch (NoSuchMethodException e) {
// In case of a NoSuchMethodException try old configuration API
// scalafmt <= v0.6.8
Method fromHocon = configCls.getMethod("fromHocon", String.class, optionCls);
Object fromHoconEmptyPath = configCls.getMethod("fromHocon$default$2").invoke(null);
// scalafmt >= v0.7.0-RC1 && scalafmt < 1.6.0
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);

String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);

Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
either = invokeNoArg(configured, "toEither");
}

config = invokeNoArg(invokeNoArg(either, "right"), "get");
}
return input -> {
Object resultInsideFormatted = formatMethod.invoke(null, input, config, emptyRange);
String result = (String) formattedGet.invoke(resultInsideFormatted);
return result;
return (String) formattedGet.invoke(resultInsideFormatted);
};
}
}
Expand Down

0 comments on commit 3ae984a

Please sign in to comment.