16
16
package com .diffplug .spotless .java ;
17
17
18
18
import java .io .Serializable ;
19
- import java .lang .reflect .Method ;
19
+ import java .lang .reflect .Constructor ;
20
20
import java .util .Objects ;
21
21
22
22
import com .diffplug .spotless .*;
23
- import com .diffplug .spotless .ThrowingEx .Function ;
24
23
25
24
/** Wraps up <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> fork of
26
25
* <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */
27
26
public class PalantirJavaFormatStep {
28
27
// prevent direct instantiation
29
28
private PalantirJavaFormatStep () {}
30
29
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" );
49
33
50
34
/** Creates a step which formats everything - code, import order, and unused imports. */
51
35
public static FormatterStep create (Provisioner provisioner ) {
@@ -54,99 +38,37 @@ public static FormatterStep create(Provisioner provisioner) {
54
38
55
39
/** Creates a step which formats everything - code, import order, and unused imports. */
56
40
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
- }
71
41
Objects .requireNonNull (version , "version" );
72
- Objects .requireNonNull (style , "style" );
73
42
Objects .requireNonNull (provisioner , "provisioner" );
43
+
74
44
return FormatterStep .createLazy (NAME ,
75
- () -> new State (NAME , groupArtifact , version , style , provisioner ),
45
+ () -> new State (JarState . from ( MAVEN_COORDINATE + version , provisioner ), version ),
76
46
State ::createFormat );
77
47
}
78
48
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
-
85
49
/** Get default formatter version */
86
50
public static String defaultVersion () {
87
51
return JVM_SUPPORT .getRecommendedFormatterVersion ();
88
52
}
89
53
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 {
95
55
private static final long serialVersionUID = 1L ;
96
56
97
57
/** 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 ;
102
61
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 ;
109
65
}
110
66
111
- @ SuppressWarnings ({"unchecked" , "rawtypes" })
112
67
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 ());
150
72
}
151
73
}
152
74
}
0 commit comments