From b3582b7ba54d7471d1a920e121a73a9a3d3308de Mon Sep 17 00:00:00 2001 From: Frank Vennemeyer Date: Thu, 30 Aug 2018 13:01:01 +0200 Subject: [PATCH 1/2] Extension of FormatterFunc accepting source file as parameter. Finalized WTP integration into lib-extra. --- .../extra/wtp/WtpEclipseFormatterStep.java | 36 +++++++++++++++---- .../wtp/EclipseWtpFormatterStepTest.java | 4 +-- .../com/diffplug/spotless/FormatterFunc.java | 27 ++++++++++++-- .../diffplug/spotless/FormatterStepImpl.java | 4 +-- .../com/diffplug/spotless/ThrowingEx.java | 6 ++++ 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java index 5218737c6e..2ef228bb9f 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.extra.wtp; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Properties; @@ -39,30 +40,30 @@ public static String defaultVersion() { /** Provides default configuration for CSSformatter */ public static EclipseBasedStepBuilder createCssBuilder(Provisioner provisioner) { - return new EclipseBasedStepBuilder(NAME, " - css", provisioner, state -> apply("EclipseCssFormatterStepImpl", state)); + return new EclipseBasedStepBuilder(NAME, " - css", provisioner, state -> applyWithoutFile("EclipseCssFormatterStepImpl", state)); } /** Provides default configuration for HTML formatter */ public static EclipseBasedStepBuilder createHtmlBuilder(Provisioner provisioner) { - return new EclipseBasedStepBuilder(NAME, " - html", provisioner, state -> apply("EclipseHtmlFormatterStepImpl", state)); + return new EclipseBasedStepBuilder(NAME, " - html", provisioner, state -> applyWithoutFile("EclipseHtmlFormatterStepImpl", state)); } /** Provides default configuration for Java Script formatter */ public static EclipseBasedStepBuilder createJsBuilder(Provisioner provisioner) { - return new EclipseBasedStepBuilder(NAME, " - js", provisioner, state -> apply("EclipseJsFormatterStepImpl", state)); + return new EclipseBasedStepBuilder(NAME, " - js", provisioner, state -> applyWithoutFile("EclipseJsFormatterStepImpl", state)); } /** Provides default configuration for JSON formatter */ public static EclipseBasedStepBuilder createJsonBuilder(Provisioner provisioner) { - return new EclipseBasedStepBuilder(NAME, " - json", provisioner, state -> apply("EclipseJsonFormatterStepImpl", state)); + return new EclipseBasedStepBuilder(NAME, " - json", provisioner, state -> applyWithoutFile("EclipseJsonFormatterStepImpl", state)); } /** Provides default configuration for XML formatter */ public static EclipseBasedStepBuilder createXmlBuilder(Provisioner provisioner) { - return new EclipseBasedStepBuilder(NAME, " - xml", provisioner, state -> apply("EclipseXmlFormatterStepImpl", state)); + return new EclipseBasedStepBuilder(NAME, " - xml", provisioner, state -> applyWithFile("EclipseXmlFormatterStepImpl", state)); } - private static FormatterFunc apply(String className, EclipseBasedStepBuilder.State state) throws Exception { + private static FormatterFunc applyWithoutFile(String className, EclipseBasedStepBuilder.State state) throws Exception { Class formatterClazz = state.loadClass(FORMATTER_PACKAGE + className); Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences()); Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class); @@ -77,4 +78,27 @@ private static FormatterFunc apply(String className, EclipseBasedStepBuilder.Sta }; } + private static FormatterFuncWithFile applyWithFile(String className, EclipseBasedStepBuilder.State state) throws Exception { + Class formatterClazz = state.loadClass(FORMATTER_PACKAGE + className); + Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences()); + Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class, String.class); + return (input, source) -> { + try { + return (String) method.invoke(formatter, input, source.getAbsolutePath()); + } catch (InvocationTargetException exceptionWrapper) { + Throwable throwable = exceptionWrapper.getTargetException(); + Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null; + throw (null == exception) ? exceptionWrapper : exception; + } + }; + } + + private static interface FormatterFuncWithFile extends FormatterFunc { + @Override + default String apply(String input) throws Exception { + throw new UnsupportedOperationException("Formatter requires file path of source."); + } + + public String apply(String input, File source) throws Exception; + } } diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java index f8fcbfc1ea..4a4bd4f2a4 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java @@ -25,7 +25,6 @@ import java.util.Properties; import java.util.function.Consumer; import java.util.function.Function; -import java.util.stream.Collectors; import org.junit.Test; import org.junit.runner.RunWith; @@ -73,8 +72,7 @@ private WTP(String input, final String expectation, Function data() { - //TODO: XML is excluded. How to provide base location will be addressed by separate PR. - return Arrays.asList(WTP.values()).stream().filter(e -> e != WTP.XML).collect(Collectors.toList()); + return Arrays.asList(WTP.values()); } @Parameter(0) diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 6731649b2e..ab4ca219b1 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -15,11 +15,26 @@ */ package com.diffplug.spotless; +import java.io.File; import java.util.Objects; -/** A `Function` which can throw an exception. */ -public interface FormatterFunc extends ThrowingEx.Function { - /** A `Function` whose implementation requires a resource which should be released when the function is no longer needed. */ +/** + * A `Function` which can throw an exception. + * Also the `BiFunction` is supported, whereas the default + * implementation only requires the `Function` implementation. + */ +public interface FormatterFunc + extends ThrowingEx.Function, ThrowingEx.BiFunction { + + @Override + default String apply(String input, File source) throws Exception { + return apply(input); + } + + /** + * `Function` and `BiFunction` whose implementation + * requires a resource which should be released when the function is no longer needed. + */ interface Closeable extends FormatterFunc, AutoCloseable { @Override void close(); @@ -34,6 +49,11 @@ public void close() { ThrowingEx.run(closeable::close); } + @Override + public String apply(String input, File source) throws Exception { + return function.apply(Objects.requireNonNull(input), Objects.requireNonNull(source)); + } + @Override public String apply(String input) throws Exception { return function.apply(Objects.requireNonNull(input)); @@ -41,4 +61,5 @@ public String apply(String input) throws Exception { }; } } + } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 51eb11a141..4f25d3c1e4 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -75,7 +75,7 @@ protected String format(State state, String rawUnix, File file) throws Exception if (formatter == null) { formatter = stateToFormatter.apply(state()); } - return formatter.apply(rawUnix); + return formatter.apply(rawUnix, file); } void cleanupFormatterFunc() { @@ -107,7 +107,7 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); } } - return formatter.apply(rawUnix); + return formatter.apply(rawUnix, file); } } } diff --git a/lib/src/main/java/com/diffplug/spotless/ThrowingEx.java b/lib/src/main/java/com/diffplug/spotless/ThrowingEx.java index d0fe3a6f77..d0d69c5661 100644 --- a/lib/src/main/java/com/diffplug/spotless/ThrowingEx.java +++ b/lib/src/main/java/com/diffplug/spotless/ThrowingEx.java @@ -31,6 +31,12 @@ public interface Function { R apply(T input) throws Exception; } + /** A bi-function that can throw any exception. */ + @FunctionalInterface + public interface BiFunction { + R apply(T1 input1, T2 input2) throws Exception; + } + /** A supplier that can throw any exception. */ @FunctionalInterface public interface Supplier { From 49761ade71fad7868ba4b366ce22a6fb71a0e31e Mon Sep 17 00:00:00 2001 From: Frank Vennemeyer Date: Fri, 31 Aug 2018 09:13:48 +0200 Subject: [PATCH 2/2] Updated CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 88cebb33ed..3dc9965753 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ You might be looking for: ### Version 1.15.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) +* Integrated Eclipse WTP formatter ([#290](https://github.com/diffplug/spotless/pull/290)) * Updated JSR305 annotation from 3.0.0 to 3.0.2 ([#274](https://github.com/diffplug/spotless/pull/274)) * Migrated from FindBugs annotations 3.0.0 to SpotBugs annotations 3.1.6 ([#274](https://github.com/diffplug/spotless/pull/274)) * `Formatter` now implements `AutoCloseable`. This means that users of `Formatter` are expected to use the try-with-resources pattern. The reason for this change is so that `FormatterFunc.Closeable` actually works. ([#284](https://github.com/diffplug/spotless/pull/284))