11/*
2- * Copyright 2023 DiffPlug
2+ * Copyright 2023-2024 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package com .diffplug .spotless .java ;
1717
18- import java .io .IOException ;
1918import java .io .Serializable ;
2019import java .lang .reflect .Constructor ;
2120import java .lang .reflect .Method ;
2726import com .diffplug .spotless .JarState ;
2827import com .diffplug .spotless .Jvm ;
2928import com .diffplug .spotless .Provisioner ;
29+ import com .diffplug .spotless .RoundedStep ;
3030
3131/**
3232 * Enables CleanThat as a SpotLess step.
3333 *
3434 * @author Benoit Lacelle
3535 */
3636// https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-to-add-a-new-formatterstep
37- public final class CleanthatJavaStep {
38-
37+ public final class CleanthatJavaStep implements RoundedStep {
38+ private static final long serialVersionUID = 1L ;
3939 private static final String NAME = "cleanthat" ;
4040 private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java" ;
41-
42- // CleanThat changelog is available at https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD
41+ /**
42+ * CleanThat changelog is available at <a href="https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD">here</a>.
43+ */
4344 private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (11 , "2.16" );
4445
45- // prevent direct instantiation
46- private CleanthatJavaStep () {}
46+ private final JarState .Promised jarState ;
47+ private final String version ;
48+ private final String sourceJdkVersion ;
49+ private final List <String > included ;
50+ private final List <String > excluded ;
51+ private final boolean includeDraft ;
52+
53+ private CleanthatJavaStep (JarState .Promised jarState ,
54+ String version ,
55+ String sourceJdkVersion ,
56+ List <String > included ,
57+ List <String > excluded ,
58+ boolean includeDraft ) {
59+ this .jarState = jarState ;
60+ this .version = version ;
61+
62+ this .sourceJdkVersion = sourceJdkVersion ;
63+ this .included = included ;
64+ this .excluded = excluded ;
65+ this .includeDraft = includeDraft ;
66+ }
4767
48- /** Creates a step which apply default CleanThat mutators. */
68+ /** Creates a step that applies default CleanThat mutators. */
4969 public static FormatterStep create (Provisioner provisioner ) {
5070 return create (defaultVersion (), provisioner );
5171 }
5272
53- /** Creates a step which apply default CleanThat mutators. */
73+ /** Creates a step that applies default CleanThat mutators. */
5474 public static FormatterStep create (String version , Provisioner provisioner ) {
5575 return create (MAVEN_COORDINATE , version , defaultSourceJdk (), defaultMutators (), defaultExcludedMutators (), defaultIncludeDraft (), provisioner );
5676 }
@@ -64,7 +84,6 @@ public static String defaultSourceJdk() {
6484
6585 /**
6686 * By default, we include only safe and consensual mutators
67- * @return
6887 */
6988 public static List <String > defaultMutators () {
7089 // see ICleanthatStepParametersProperties.SAFE_AND_CONSENSUAL
@@ -79,7 +98,7 @@ public static boolean defaultIncludeDraft() {
7998 return false ;
8099 }
81100
82- /** Creates a step which apply selected CleanThat mutators. */
101+ /** Creates a step that applies selected CleanThat mutators. */
83102 public static FormatterStep create (String groupArtifact ,
84103 String version ,
85104 String sourceJdkVersion ,
@@ -93,57 +112,51 @@ public static FormatterStep create(String groupArtifact,
93112 }
94113 Objects .requireNonNull (version , "version" );
95114 Objects .requireNonNull (provisioner , "provisioner" );
96- return FormatterStep .createLazy (NAME ,
97- () -> new JavaRefactorerState (NAME , groupArtifact , version , sourceJdkVersion , included , excluded , includeDraft , provisioner ),
98- JavaRefactorerState ::createFormat );
115+ return FormatterStep .create (NAME ,
116+ new CleanthatJavaStep (JarState .promise (() -> JarState .from (groupArtifact + ":" + version , provisioner )), version , sourceJdkVersion , included , excluded , includeDraft ),
117+ CleanthatJavaStep ::equalityState ,
118+ State ::createFormat );
99119 }
100120
101121 /** Get default formatter version */
102122 public static String defaultVersion () {
103- return JVM_SUPPORT .getRecommendedFormatterVersion ();
123+ return Objects . requireNonNull ( JVM_SUPPORT .getRecommendedFormatterVersion () );
104124 }
105125
106126 public static String defaultGroupArtifact () {
107127 return MAVEN_COORDINATE ;
108128 }
109129
110- static final class JavaRefactorerState implements Serializable {
111- private static final long serialVersionUID = 1L ;
112-
113- final JarState jarState ;
114- final String stepName ;
115- final String version ;
130+ private State equalityState () {
131+ return new State (jarState .get (), version , sourceJdkVersion , included , excluded , includeDraft );
132+ }
116133
117- final String sourceJdkVersion ;
118- final List <String > included ;
119- final List <String > excluded ;
120- final boolean includeDraft ;
134+ private static final class State implements Serializable {
135+ private static final long serialVersionUID = 1L ;
121136
122- JavaRefactorerState (String stepName , String version , Provisioner provisioner ) throws IOException {
123- this (stepName , MAVEN_COORDINATE , version , defaultSourceJdk (), defaultMutators (), defaultExcludedMutators (), defaultIncludeDraft (), provisioner );
124- }
137+ private final JarState jarState ;
138+ private final String version ;
139+ private final String sourceJdkVersion ;
140+ private final List <String > included ;
141+ private final List <String > excluded ;
142+ private final boolean includeDraft ;
125143
126- JavaRefactorerState (String stepName ,
127- String groupArtifact ,
144+ State (JarState jarState ,
128145 String version ,
129146 String sourceJdkVersion ,
130147 List <String > included ,
131148 List <String > excluded ,
132- boolean includeDraft ,
133- Provisioner provisioner ) throws IOException {
149+ boolean includeDraft ) {
134150 JVM_SUPPORT .assertFormatterSupported (version );
135151 ModuleHelper .doOpenInternalPackagesIfRequired ();
136- this .jarState = JarState .from (groupArtifact + ":" + version , provisioner );
137- this .stepName = stepName ;
152+ this .jarState = jarState ;
138153 this .version = version ;
139-
140154 this .sourceJdkVersion = sourceJdkVersion ;
141155 this .included = included ;
142156 this .excluded = excluded ;
143157 this .includeDraft = includeDraft ;
144158 }
145159
146- @ SuppressWarnings ("PMD.UseProperClassLoader" )
147160 FormatterFunc createFormat () {
148161 ClassLoader classLoader = jarState .getClassLoader ();
149162
@@ -159,9 +172,7 @@ FormatterFunc createFormat() {
159172 throw new IllegalStateException ("Issue executing the formatter" , e );
160173 }
161174
162- return JVM_SUPPORT .suggestLaterVersionOnError (version , input -> {
163- return (String ) formatterMethod .invoke (formatter , input );
164- });
175+ return JVM_SUPPORT .suggestLaterVersionOnError (version , input -> (String ) formatterMethod .invoke (formatter , input ));
165176 }
166177 }
167178}
0 commit comments