Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of FormatterFunc accepting source file as parameter. #290

Merged
merged 2 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,8 +72,7 @@ private WTP(String input, final String expectation, Function<Provisioner, Eclips

@Parameters(name = "{0}")
public static Iterable<WTP> 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)
Expand Down
27 changes: 24 additions & 3 deletions lib/src/main/java/com/diffplug/spotless/FormatterFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@
*/
package com.diffplug.spotless;

import java.io.File;
import java.util.Objects;

/** A `Function<String, String>` which can throw an exception. */
public interface FormatterFunc extends ThrowingEx.Function<String, String> {
/** A `Function<String, String>` whose implementation requires a resource which should be released when the function is no longer needed. */
/**
* A `Function<String, String>` which can throw an exception.
* Also the `BiFunction<String, File, String>` is supported, whereas the default
* implementation only requires the `Function<String, String>` implementation.
*/
public interface FormatterFunc
extends ThrowingEx.Function<String, String>, ThrowingEx.BiFunction<String, File, String> {

@Override
default String apply(String input, File source) throws Exception {
return apply(input);
}

/**
* `Function<String, String>` and `BiFunction<String, File, String>` whose implementation
* requires a resource which should be released when the function is no longer needed.
*/
interface Closeable extends FormatterFunc, AutoCloseable {
@Override
void close();
Expand All @@ -34,11 +49,17 @@ 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));
}
};
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/ThrowingEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public interface Function<T, R> {
R apply(T input) throws Exception;
}

/** A bi-function that can throw any exception. */
@FunctionalInterface
public interface BiFunction<T1, T2, R> {
R apply(T1 input1, T2 input2) throws Exception;
}

/** A supplier that can throw any exception. */
@FunctionalInterface
public interface Supplier<T> {
Expand Down