Skip to content

Commit

Permalink
Make all steps with JarState serializable (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Apr 3, 2024
2 parents 7118f6a + 5a21446 commit 13fbf78
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,44 +15,54 @@
*/
package com.diffplug.spotless.antlr4;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.diffplug.spotless.*;

public class Antlr4FormatterStep {
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.RoundedStep;
import com.diffplug.spotless.ThrowingEx;

public class Antlr4FormatterStep implements RoundedStep {
private static final long serialVersionUID = 1L;
private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
private static final String DEFAULT_VERSION = "1.2.1";
public static final String NAME = "antlr4Formatter";

private Antlr4FormatterStep() {}
private final JarState.Promised jarState;

private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
private static final String DEFAULT_VERSION = "1.2.1";
private Antlr4FormatterStep(JarState.Promised jarState) {
this.jarState = jarState;
}

public static FormatterStep create(Provisioner provisioner) {
return create(defaultVersion(), provisioner);
}

public static FormatterStep create(String version, Provisioner provisioner) {
return FormatterStep.createLazy(NAME, () -> new State(version, provisioner), State::createFormat);
return FormatterStep.create(NAME,
new Antlr4FormatterStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner))),
Antlr4FormatterStep::equalityState,
State::createFormat);
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;
private State equalityState() {
return new State(jarState.get());
}

/**
* The jar that contains the formatter.
*/
final JarState jarState;
private static final class State implements Serializable {
private static final long serialVersionUID = 1L;
private final JarState jarState;

State(String version, Provisioner provisioner) throws IOException {
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
State(JarState jarState) {
this.jarState = jarState;
}

FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException {
Expand Down
33 changes: 25 additions & 8 deletions lib/src/main/java/com/diffplug/spotless/generic/Jsr223Step.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,35 +19,52 @@
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.RoundedStep;

public final class Jsr223Step {
// prevent direct instantiation
private Jsr223Step() {}
public final class Jsr223Step implements RoundedStep {
private static final long serialVersionUID = 1L;
@Nullable
private final JarState.Promised jarState;
private final String engine;
private final String script;

public static FormatterStep create(String name, String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) {
private Jsr223Step(@Nullable JarState.Promised jarState, String engine, String script) {
this.jarState = jarState;
this.engine = engine;
this.script = script;
}

public static FormatterStep create(String name, @Nullable String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(engine, "engine");
Objects.requireNonNull(script, "script");
return FormatterStep.createLazy(name,
() -> new State(dependency == null ? null : JarState.from(dependency, provisioner), engine, script),
return FormatterStep.create(name,
new Jsr223Step(dependency == null ? null : JarState.promise(() -> JarState.from(dependency, provisioner)), engine.toString(), script.toString()),
Jsr223Step::equalityState,
State::toFormatter);
}

private State equalityState() {
return new State(jarState == null ? null : jarState.get(), engine, script);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

@Nullable
private final JarState jarState;
private final String engine;
private final String script;

State(JarState jarState, CharSequence engine, CharSequence script) {
State(@Nullable JarState jarState, CharSequence engine, CharSequence script) {
this.jarState = jarState;
this.engine = engine.toString();
this.script = script.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/
package com.diffplug.spotless.gherkin;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -25,10 +24,21 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.RoundedStep;

public class GherkinUtilsStep {
public class GherkinUtilsStep implements RoundedStep {
private static final long serialVersionUID = 1L;
private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:";
private static final String DEFAULT_VERSION = "8.0.2";
public static final String NAME = "gherkinUtils";

private final JarState.Promised jarState;
private final GherkinUtilsConfig gherkinSimpleConfig;

private GherkinUtilsStep(JarState.Promised jarState, GherkinUtilsConfig gherkinSimpleConfig) {
this.jarState = jarState;
this.gherkinSimpleConfig = gherkinSimpleConfig;
}

public static String defaultVersion() {
return DEFAULT_VERSION;
Expand All @@ -37,7 +47,14 @@ public static String defaultVersion() {
public static FormatterStep create(GherkinUtilsConfig gherkinSimpleConfig,
String formatterVersion, Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner cannot be null");
return FormatterStep.createLazy("gherkin", () -> new GherkinUtilsStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinUtilsStep.State::toFormatter);
return FormatterStep.create(NAME,
new GherkinUtilsStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner)), gherkinSimpleConfig),
GherkinUtilsStep::equalityState,
GherkinUtilsStep.State::toFormatter);
}

private State equalityState() {
return new State(jarState.get(), gherkinSimpleConfig);
}

private static final class State implements Serializable {
Expand All @@ -46,9 +63,9 @@ private static final class State implements Serializable {
private final GherkinUtilsConfig gherkinSimpleConfig;
private final JarState jarState;

private State(GherkinUtilsConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException {
State(JarState jarState, GherkinUtilsConfig gherkinSimpleConfig) {
this.jarState = jarState;
this.gherkinSimpleConfig = gherkinSimpleConfig;
this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner);
}

FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
Expand All @@ -58,8 +75,4 @@ FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException
return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig);
}
}

private GherkinUtilsStep() {
// cannot be directly instantiated
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* A DTO holding the basic for Jackson-based formatters
Expand All @@ -36,7 +37,7 @@ public class JacksonConfig implements Serializable {
DEFAULT_FEATURE_TOGGLES = defaultFeatureToggles;
}

protected Map<String, Boolean> featureToToggle = new LinkedHashMap<>(DEFAULT_FEATURE_TOGGLES);
protected Map<String, Boolean> featureToToggle = new TreeMap<>(DEFAULT_FEATURE_TOGGLES);

public Map<String, Boolean> getFeatureToToggle() {
return Collections.unmodifiableMap(featureToToggle);
Expand Down
42 changes: 25 additions & 17 deletions lib/src/main/java/com/diffplug/spotless/json/JacksonJsonStep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/
package com.diffplug.spotless.json;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -25,48 +24,57 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.RoundedStep;

/**
* Simple YAML formatter which reformats the file according to Jackson YAMLFactory.
*/
// https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson
public class JacksonJsonStep {
static final String MAVEN_COORDINATE = "com.fasterxml.jackson.core:jackson-databind:";
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
static final String DEFAULT_VERSION = "2.14.2";
public class JacksonJsonStep implements RoundedStep {
private static final long serialVersionUID = 1L;
private static final String MAVEN_COORDINATE = "com.fasterxml.jackson.core:jackson-databind:";
private static final String DEFAULT_VERSION = "2.14.2";
public static final String NAME = "jacksonJson";

private JacksonJsonStep() {}
private final JarState.Promised jarState;
private final JacksonConfig jacksonConfig;

private JacksonJsonStep(JarState.Promised jarState, JacksonConfig jacksonConfig) {
this.jarState = jarState;
this.jacksonConfig = jacksonConfig;
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

public static FormatterStep create(Provisioner provisioner) {
return create(new JacksonJsonConfig(), defaultVersion(), provisioner);
}

public static FormatterStep create(JacksonJsonConfig jacksonConfig,
String jacksonVersion,
Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner cannot be null");
return FormatterStep.createLazy("json",
() -> new State(jacksonConfig, jacksonVersion, provisioner),
return FormatterStep.create(NAME,
new JacksonJsonStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + jacksonVersion, provisioner)), jacksonConfig),
JacksonJsonStep::equalityState,
State::toFormatter);
}

public static FormatterStep create(Provisioner provisioner) {
return create(new JacksonJsonConfig(), defaultVersion(), provisioner);
private State equalityState() {
return new State(jarState.get(), jacksonConfig);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

private final JacksonConfig jacksonConfig;

private final JarState jarState;

private State(JacksonConfig jacksonConfig,
String jacksonVersion,
Provisioner provisioner) throws IOException {
State(JarState jarState, JacksonConfig jacksonConfig) {
this.jarState = jarState;
this.jacksonConfig = jacksonConfig;

this.jarState = JarState.from(JacksonJsonStep.MAVEN_COORDINATE + jacksonVersion, provisioner);
}

FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
Expand Down

0 comments on commit 13fbf78

Please sign in to comment.