Skip to content

Commit 0d83dca

Browse files
committed
Simplify the PJF step using a compileOnly dependency
Support for non-default styles and alternative artifacts have been dropped in favor simplicity. If requested, we can add support for reflowing strings in the future.
1 parent e9352f6 commit 0d83dca

File tree

11 files changed

+74
-195
lines changed

11 files changed

+74
-195
lines changed

lib/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ apply from: rootProject.file('gradle/java-publish.gradle')
88

99
def NEEDS_GLUE = [
1010
'sortPom',
11+
'palantirJavaFormat',
1112
'ktlint',
1213
'flexmark'
1314
]
@@ -29,6 +30,8 @@ dependencies {
2930
// used for pom sorting
3031
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
3132

33+
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0'
34+
3235
String VER_KTLINT='0.43.2'
3336
ktlintCompileOnly "com.pinterest:ktlint:$VER_KTLINT"
3437
ktlintCompileOnly "com.pinterest.ktlint:ktlint-core:$VER_KTLINT"

lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,20 @@
1616
package com.diffplug.spotless.java;
1717

1818
import java.io.Serializable;
19-
import java.lang.reflect.Method;
19+
import java.lang.reflect.Constructor;
2020
import java.util.Objects;
2121

2222
import 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. */
2726
public 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
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.pjf;
17+
18+
import com.palantir.javaformat.java.Formatter;
19+
import com.palantir.javaformat.java.ImportOrderer;
20+
import com.palantir.javaformat.java.JavaFormatterOptions;
21+
import com.palantir.javaformat.java.RemoveUnusedImports;
22+
23+
import com.diffplug.spotless.FormatterFunc;
24+
25+
public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
26+
27+
private final Formatter formatter;
28+
29+
public PalantirJavaFormatFormatterFunc() {
30+
formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
31+
.style(JavaFormatterOptions.Style.PALANTIR)
32+
.build());
33+
}
34+
35+
@Override
36+
public String apply(String input) throws Exception {
37+
String source = input;
38+
source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR);
39+
source = RemoveUnusedImports.removeUnusedImports(source);
40+
return formatter.formatSource(source);
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}';
46+
}
47+
}

plugin-gradle/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ This is a workaround to a [pending issue](https://github.com/diffplug/spotless/i
210210
spotless {
211211
java {
212212
palantirJavaFormat()
213-
// optional: you can specify a specific version and/or switch to GOOGLE/AOSP style
214-
// and/or use custom group artifact (you probably don't need this)
215-
palantirJavaFormat('2.9.0').style("PALANTIR").groupArtifact('com.google.googlejavaformat:google-java-format')
213+
// optional: you can specify a specific version
214+
palantirJavaFormat('2.9.0')
216215
```
217216

218217
**⚠️ Note on using Palantir Java Format with Java 16+**

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -194,34 +194,14 @@ public PalantirJavaFormatConfig palantirJavaFormat(String version) {
194194

195195
public class PalantirJavaFormatConfig {
196196
final String version;
197-
String groupArtifact;
198-
String style;
199197

200198
PalantirJavaFormatConfig(String version) {
201199
this.version = Objects.requireNonNull(version);
202-
this.groupArtifact = PalantirJavaFormatStep.defaultGroupArtifact();
203-
this.style = PalantirJavaFormatStep.defaultStyle();
204200
addStep(createStep());
205201
}
206202

207-
public PalantirJavaFormatConfig groupArtifact(String groupArtifact) {
208-
this.groupArtifact = Objects.requireNonNull(groupArtifact);
209-
replaceStep(createStep());
210-
return this;
211-
}
212-
213-
public PalantirJavaFormatConfig style(String style) {
214-
this.style = Objects.requireNonNull(style);
215-
replaceStep(createStep());
216-
return this;
217-
}
218-
219203
private FormatterStep createStep() {
220-
return PalantirJavaFormatStep.create(
221-
groupArtifact,
222-
version,
223-
style,
224-
provisioner());
204+
return PalantirJavaFormatStep.create(version, provisioner());
225205
}
226206
}
227207

plugin-maven/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,6 @@ This is a workaround to a [pending issue](https://github.com/diffplug/spotless/i
230230
```xml
231231
<palantirJavaFormat>
232232
<version>2.10.0</version> <!-- optional -->
233-
<style>PALANTIR</style> <!-- or AOSP/GOOGLE (optional) -->
234-
<!-- optional: custom group artifact (you probably don't need this) -->
235-
<groupArtifact>com.palantir.javaformat:palantir-java-format</groupArtifact>
236233
</palantirJavaFormat>
237234
```
238235

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,13 @@
2323
import com.diffplug.spotless.maven.FormatterStepFactory;
2424

2525
public class PalantirJavaFormat implements FormatterStepFactory {
26-
@Parameter
27-
private String groupArtifact;
2826

2927
@Parameter
3028
private String version;
3129

32-
@Parameter
33-
private String style;
34-
3530
@Override
3631
public FormatterStep newFormatterStep(FormatterStepConfig config) {
37-
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : PalantirJavaFormatStep.defaultGroupArtifact();
3832
String version = this.version != null ? this.version : PalantirJavaFormatStep.defaultVersion();
39-
String style = this.style != null ? this.style : PalantirJavaFormatStep.defaultStyle();
40-
return PalantirJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner());
33+
return PalantirJavaFormatStep.create(version, config.getProvisioner());
4134
}
4235
}

testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test

Lines changed: 0 additions & 11 deletions
This file was deleted.

testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test

Lines changed: 0 additions & 16 deletions
This file was deleted.

testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test

Lines changed: 0 additions & 13 deletions
This file was deleted.

testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void jvm13Features() throws Exception {
3838
}
3939

4040
@Test
41-
@EnabledForJreRange(min = JAVA_11) // google-java-format requires JRE 11+
41+
@EnabledForJreRange(min = JAVA_11)
4242
void behavior2() throws Exception {
4343
FormatterStep step = PalantirJavaFormatStep.create("2.10.0", TestProvisioner.mavenCentral());
4444
StepHarness.forStep(step)
@@ -57,28 +57,9 @@ void behavior() throws Exception {
5757
}
5858

5959
@Test
60-
void behaviorWithAospStyle() throws Exception {
61-
FormatterStep step = PalantirJavaFormatStep.create("1.1.0", "AOSP", TestProvisioner.mavenCentral());
62-
StepHarness.forStep(step)
63-
.testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormattedAOSP.test")
64-
.testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test")
65-
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test");
66-
}
67-
68-
@Test
69-
void behaviorWithCustomGroupArtifact() throws Exception {
70-
FormatterStep step = PalantirJavaFormatStep.create(PalantirJavaFormatStep.defaultGroupArtifact(), "1.1.0", PalantirJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral());
71-
StepHarness.forStep(step)
72-
.testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test")
73-
.testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test")
74-
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test");
75-
}
76-
77-
@Test
78-
void equality() throws Exception {
60+
void equality() {
7961
new SerializableEqualityTester() {
8062
String version = "1.1.0";
81-
String style = "";
8263

8364
@Override
8465
protected void setupTest(API api) {
@@ -87,15 +68,12 @@ protected void setupTest(API api) {
8768
// change the version, and it's different
8869
version = "1.0.0";
8970
api.areDifferentThan();
90-
// change the style, and it's different
91-
style = "AOSP";
92-
api.areDifferentThan();
9371
}
9472

9573
@Override
9674
protected FormatterStep create() {
9775
String finalVersion = this.version;
98-
return PalantirJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral());
76+
return PalantirJavaFormatStep.create(finalVersion, TestProvisioner.mavenCentral());
9977
}
10078
}.testEquals();
10179
}

0 commit comments

Comments
 (0)