1616package com .diffplug .spotless .java ;
1717
1818import java .io .Serializable ;
19- import java .lang .reflect .Method ;
19+ import java .lang .reflect .Constructor ;
2020import java .util .Objects ;
2121
2222import com .diffplug .spotless .*;
23- import com .diffplug .spotless .ThrowingEx .Function ;
2423
2524/** Wraps up <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> fork of
2625 * <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */
2726public class PalantirJavaFormatStep {
2827 // prevent direct instantiation
2928 private PalantirJavaFormatStep () {}
3029
31- private static final String DEFAULT_STYLE = "PALANTIR" ;
32- static final String NAME = "palantir-java-format" ;
33- static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format" ;
34- static final String FORMATTER_CLASS = "com.palantir.javaformat.java.Formatter" ;
35- static final String FORMATTER_METHOD = "formatSource" ;
36-
37- private static final String OPTIONS_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions" ;
38- private static final String OPTIONS_BUILDER_METHOD = "builder" ;
39- private static final String OPTIONS_BUILDER_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions$Builder" ;
40- private static final String OPTIONS_BUILDER_STYLE_METHOD = "style" ;
41- private static final String OPTIONS_BUILDER_BUILD_METHOD = "build" ;
42- private static final String OPTIONS_STYLE = "com.palantir.javaformat.java.JavaFormatterOptions$Style" ;
43-
44- private static final String REMOVE_UNUSED_CLASS = "com.palantir.javaformat.java.RemoveUnusedImports" ;
45- private static final String REMOVE_UNUSED_METHOD = "removeUnusedImports" ;
46-
47- private static final String IMPORT_ORDERER_CLASS = "com.palantir.javaformat.java.ImportOrderer" ;
48- private static final String IMPORT_ORDERER_METHOD = "reorderImports" ;
30+ private static final String NAME = "palantir-java-format" ;
31+ private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:" ;
32+ private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (8 , "1.1.0" ).add (11 , "2.10.0" );
4933
5034 /** Creates a step which formats everything - code, import order, and unused imports. */
5135 public static FormatterStep create (Provisioner provisioner ) {
@@ -54,99 +38,37 @@ public static FormatterStep create(Provisioner provisioner) {
5438
5539 /** Creates a step which formats everything - code, import order, and unused imports. */
5640 public static FormatterStep create (String version , Provisioner provisioner ) {
57- return create (version , DEFAULT_STYLE , provisioner );
58- }
59-
60- /** Creates a step which formats everything - code, import order, and unused imports. */
61- public static FormatterStep create (String version , String style , Provisioner provisioner ) {
62- return create (MAVEN_COORDINATE , version , style , provisioner );
63- }
64-
65- /** Creates a step which formats everything - groupArtifact, code, import order, and unused imports. */
66- public static FormatterStep create (String groupArtifact , String version , String style , Provisioner provisioner ) {
67- Objects .requireNonNull (groupArtifact , "groupArtifact" );
68- if (groupArtifact .chars ().filter (ch -> ch == ':' ).count () != 1 ) {
69- throw new IllegalArgumentException ("groupArtifact must be in the form 'groupId:artifactId'" );
70- }
7141 Objects .requireNonNull (version , "version" );
72- Objects .requireNonNull (style , "style" );
7342 Objects .requireNonNull (provisioner , "provisioner" );
43+
7444 return FormatterStep .createLazy (NAME ,
75- () -> new State (NAME , groupArtifact , version , style , provisioner ),
45+ () -> new State (JarState . from ( MAVEN_COORDINATE + version , provisioner ), version ),
7646 State ::createFormat );
7747 }
7848
79- static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (8 , "1.1.0" ).add (11 , "2.10.0" );
80-
81- public static String defaultGroupArtifact () {
82- return MAVEN_COORDINATE ;
83- }
84-
8549 /** Get default formatter version */
8650 public static String defaultVersion () {
8751 return JVM_SUPPORT .getRecommendedFormatterVersion ();
8852 }
8953
90- public static String defaultStyle () {
91- return DEFAULT_STYLE ;
92- }
93-
94- static final class State implements Serializable {
54+ private static final class State implements Serializable {
9555 private static final long serialVersionUID = 1L ;
9656
9757 /** The jar that contains the formatter. */
98- final JarState jarState ;
99- final String stepName ;
100- final String version ;
101- final String style ;
58+ private final JarState jarState ;
59+ /** Version of the formatter jar. */
60+ private final String formatterVersion ;
10261
103- State (String stepName , String groupArtifact , String version , String style , Provisioner provisioner ) throws Exception {
104- JVM_SUPPORT .assertFormatterSupported (version );
105- this .jarState = JarState .from (groupArtifact + ":" + version , provisioner );
106- this .stepName = stepName ;
107- this .version = version ;
108- this .style = style ;
62+ State (JarState jarState , String formatterVersion ) {
63+ this .jarState = jarState ;
64+ this .formatterVersion = formatterVersion ;
10965 }
11066
111- @ SuppressWarnings ({"unchecked" , "rawtypes" })
11267 FormatterFunc createFormat () throws Exception {
113- ClassLoader classLoader = jarState .getClassLoader ();
114-
115- // instantiate the formatter and get its format method
116- Class <?> optionsClass = classLoader .loadClass (OPTIONS_CLASS );
117- Class <?> optionsBuilderClass = classLoader .loadClass (OPTIONS_BUILDER_CLASS );
118- Method optionsBuilderMethod = optionsClass .getMethod (OPTIONS_BUILDER_METHOD );
119- Object optionsBuilder = optionsBuilderMethod .invoke (null );
120-
121- Class <?> optionsStyleClass = classLoader .loadClass (OPTIONS_STYLE );
122- Object styleConstant = Enum .valueOf ((Class <Enum >) optionsStyleClass , style );
123- Method optionsBuilderStyleMethod = optionsBuilderClass .getMethod (OPTIONS_BUILDER_STYLE_METHOD , optionsStyleClass );
124- optionsBuilderStyleMethod .invoke (optionsBuilder , styleConstant );
125-
126- Method optionsBuilderBuildMethod = optionsBuilderClass .getMethod (OPTIONS_BUILDER_BUILD_METHOD );
127- Object options = optionsBuilderBuildMethod .invoke (optionsBuilder );
128-
129- Class <?> formatterClazz = classLoader .loadClass (FORMATTER_CLASS );
130- Object formatter = formatterClazz .getMethod ("createFormatter" , optionsClass ).invoke (null , options );
131- Method formatterMethod = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
132-
133- Function <String , String > removeUnused = constructRemoveUnusedFunction (classLoader );
134-
135- Class <?> importOrdererClass = classLoader .loadClass (IMPORT_ORDERER_CLASS );
136- Method importOrdererMethod = importOrdererClass .getMethod (IMPORT_ORDERER_METHOD , String .class );
137-
138- return JVM_SUPPORT .suggestLaterVersionOnError (version , (input -> {
139- String formatted = (String ) formatterMethod .invoke (formatter , input );
140- String removedUnused = removeUnused .apply (formatted );
141- return (String ) importOrdererMethod .invoke (null , removedUnused );
142- }));
143- }
144-
145- private static Function <String , String > constructRemoveUnusedFunction (ClassLoader classLoader )
146- throws NoSuchMethodException , ClassNotFoundException {
147- Class <?> removeUnusedClass = classLoader .loadClass (REMOVE_UNUSED_CLASS );
148- Method removeUnusedMethod = removeUnusedClass .getMethod (REMOVE_UNUSED_METHOD , String .class );
149- return (x ) -> (String ) removeUnusedMethod .invoke (null , x );
68+ final ClassLoader classLoader = jarState .getClassLoader ();
69+ final Class <?> formatterFunc = classLoader .loadClass ("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc" );
70+ final Constructor <?> constructor = formatterFunc .getConstructor ();
71+ return JVM_SUPPORT .suggestLaterVersionOnError (formatterVersion , (FormatterFunc ) constructor .newInstance ());
15072 }
15173 }
15274}
0 commit comments