Skip to content

Commit c66f83e

Browse files
authored
Add support for palantir-java-format (#1083 implements #1076)
2 parents bb27596 + f4db9e0 commit c66f83e

File tree

24 files changed

+545
-4
lines changed

24 files changed

+545
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)).
1315

1416
## [2.21.2] - 2022-01-07
1517
### Fixed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extra('cpp.EclipseFormatterStep') +'{{yes}} | {{yes}}
5858
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
5959
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
6060
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
61+
lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6162
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
6263
extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
6364
lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
@@ -98,6 +99,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
9899
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
99100
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
100101
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
102+
| [`java.PalantirJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
101103
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
102104
| [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
103105
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :+1: | :white_large_square: |

gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# To fix metaspace errors
2+
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
3+
14
name=spotless
25
description=Spotless - keep your code spotless with Gradle
36
org=diffplug

lib/build.gradle

+3
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2016-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.java;
17+
18+
import java.io.Serializable;
19+
import java.lang.reflect.Constructor;
20+
import java.util.Objects;
21+
22+
import com.diffplug.spotless.*;
23+
24+
/** Wraps up <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> fork of
25+
* <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */
26+
public class PalantirJavaFormatStep {
27+
// prevent direct instantiation
28+
private PalantirJavaFormatStep() {}
29+
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");
33+
34+
/** Creates a step which formats everything - code, import order, and unused imports. */
35+
public static FormatterStep create(Provisioner provisioner) {
36+
return create(defaultVersion(), provisioner);
37+
}
38+
39+
/** Creates a step which formats everything - code, import order, and unused imports. */
40+
public static FormatterStep create(String version, Provisioner provisioner) {
41+
Objects.requireNonNull(version, "version");
42+
Objects.requireNonNull(provisioner, "provisioner");
43+
44+
return FormatterStep.createLazy(NAME,
45+
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version),
46+
State::createFormat);
47+
}
48+
49+
/** Get default formatter version */
50+
public static String defaultVersion() {
51+
return JVM_SUPPORT.getRecommendedFormatterVersion();
52+
}
53+
54+
private static final class State implements Serializable {
55+
private static final long serialVersionUID = 1L;
56+
57+
/** The jar that contains the formatter. */
58+
private final JarState jarState;
59+
/** Version of the formatter jar. */
60+
private final String formatterVersion;
61+
62+
State(JarState jarState, String formatterVersion) {
63+
this.jarState = jarState;
64+
this.formatterVersion = formatterVersion;
65+
}
66+
67+
FormatterFunc createFormat() throws Exception {
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());
72+
}
73+
}
74+
}
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/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* 🎉🎉🎉 [**IntelliJ plugin**](https://plugins.jetbrains.com/plugin/18321-spotless-gradle) thanks to [@ragurney](https://github.com/ragurney) 🎉🎉🎉
8+
* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)).
89

910
## [6.1.2] - 2022-01-07
1011
### Fixed

plugin-gradle/README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
6060
- [**Quickstart**](#quickstart)
6161
- [Requirements](#requirements)
6262
- **Languages**
63-
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier))
63+
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier), [palantir-java-format](#palantir-java-format))
6464
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
6565
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
6666
- [Scala](#scala) ([scalafmt](#scalafmt))
@@ -205,6 +205,32 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
205205
```
206206
This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834).
207207

208+
### palantir-java-format
209+
210+
[homepage](https://github.com/palantir/palantir-java-format). [changelog](https://github.com/palantir/palantir-java-format/releases).
211+
```gradle
212+
spotless {
213+
java {
214+
palantirJavaFormat()
215+
// optional: you can specify a specific version
216+
palantirJavaFormat('2.9.0')
217+
```
218+
219+
**⚠️ Note on using Palantir Java Format with Java 16+**
220+
221+
Using Java 16+ with Palantir Java Format [requires additional flags](https://github.com/google/google-java-format/releases/tag/v1.10.0) on the running JDK.
222+
These Flags can be provided using the `gradle.properties` file (See [documentation](https://docs.gradle.org/current/userguide/build_environment.html)).
223+
224+
For example the following file under `gradle.properties` will run maven with the required flags:
225+
```
226+
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
227+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
228+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
229+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
230+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
231+
```
232+
This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834).
233+
208234
### eclipse jdt
209235

210236
[homepage](https://www.eclipse.org/downloads/packages/). [compatible versions](https://github.com/diffplug/spotless/tree/main/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_jdt_formatter). See [here](../ECLIPSE_SCREENSHOTS.md) for screenshots that demonstrate how to get and install the config file mentioned below.

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 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.
@@ -34,6 +34,7 @@
3434
import com.diffplug.spotless.generic.LicenseHeaderStep;
3535
import com.diffplug.spotless.java.GoogleJavaFormatStep;
3636
import com.diffplug.spotless.java.ImportOrderStep;
37+
import com.diffplug.spotless.java.PalantirJavaFormatStep;
3738
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
3839

3940
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
@@ -175,6 +176,35 @@ private FormatterStep createStep() {
175176
}
176177
}
177178

179+
/** Uses the <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> jar to format source code. */
180+
public PalantirJavaFormatConfig palantirJavaFormat() {
181+
return palantirJavaFormat(PalantirJavaFormatStep.defaultVersion());
182+
}
183+
184+
/**
185+
* Uses the given version of <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> to format source code.
186+
*
187+
* Limited to published versions. See <a href="https://github.com/diffplug/spotless/issues/33#issuecomment-252315095">issue #33</a>
188+
* for an workaround for using snapshot versions.
189+
*/
190+
public PalantirJavaFormatConfig palantirJavaFormat(String version) {
191+
Objects.requireNonNull(version);
192+
return new PalantirJavaFormatConfig(version);
193+
}
194+
195+
public class PalantirJavaFormatConfig {
196+
final String version;
197+
198+
PalantirJavaFormatConfig(String version) {
199+
this.version = Objects.requireNonNull(version);
200+
addStep(createStep());
201+
}
202+
203+
private FormatterStep createStep() {
204+
return PalantirJavaFormatStep.create(version, provisioner());
205+
}
206+
}
207+
178208
public EclipseConfig eclipse() {
179209
return new EclipseConfig(EclipseJdtFormatterStep.defaultVersion());
180210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class PalantirJavaFormatIntegrationTest extends GradleIntegrationHarness {
23+
@Test
24+
void integration() throws IOException {
25+
setFile("build.gradle").toLines(
26+
"plugins {",
27+
" id 'com.diffplug.spotless'",
28+
"}",
29+
"repositories { mavenCentral() }",
30+
"",
31+
"spotless {",
32+
" java {",
33+
" target file('test.java')",
34+
" palantirJavaFormat('1.1.0')",
35+
" }",
36+
"}");
37+
38+
setFile("test.java").toResource("java/palantirjavaformat/JavaCodeUnformatted.test");
39+
gradleRunner().withArguments("spotlessApply").build();
40+
assertFile("test.java").sameAsResource("java/palantirjavaformat/JavaCodeFormatted.test");
41+
42+
checkRunsThenUpToDate();
43+
replace("build.gradle",
44+
"palantirJavaFormat('1.1.0')",
45+
"palantirJavaFormat('1.0.1')");
46+
checkRunsThenUpToDate();
47+
}
48+
}

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)).
68

79
## [2.19.2] - 2022-01-10
810
### Fixed

plugin-maven/README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ user@machine repo % mvn spotless:check
4747
- [Requirements](#requirements)
4848
- [Binding to maven phase](#binding-to-maven-phase)
4949
- **Languages**
50-
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier))
50+
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format))
5151
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
5252
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
5353
- [Scala](#scala) ([scalafmt](#scalafmt))
@@ -223,6 +223,27 @@ For example the following file under `.mvn/jvm.config` will run maven with the r
223223
```
224224
This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834).
225225

226+
### palantir-java-format
227+
228+
[homepage](https://github.com/palantir/palantir-java-format). [changelog](https://github.com/palantir/palantir-java-format/releases). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java).
229+
230+
```xml
231+
<palantirJavaFormat>
232+
<version>2.10.0</version> <!-- optional -->
233+
</palantirJavaFormat>
234+
```
235+
236+
**⚠️ Note on using Palantir Java Format with Java 16+**
237+
238+
Using Java 16+ with Palantir Java Format [requires additional flags](https://github.com/google/google-java-format/releases/tag/v1.10.0) on the running JDK.
239+
These Flags can be provided using `MAVEN_OPTS` environment variable or using the `./mvn/jvm.config` file (See [documentation](https://maven.apache.org/configure.html#mvn-jvm-config-file)).
240+
241+
For example the following file under `.mvn/jvm.config` will run maven with the required flags:
242+
```
243+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
244+
```
245+
This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834).
246+
226247
### eclipse jdt
227248

228249
[homepage](https://www.eclipse.org/downloads/packages/). [compatible versions](https://github.com/diffplug/spotless/tree/main/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_jdt_formatter). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java). See [here](../ECLIPSE_SCREENSHOTS.md) for screenshots that demonstrate how to get and install the config file mentioned below.

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 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.
@@ -54,6 +54,10 @@ public void addImportOrder(ImportOrder importOrder) {
5454
addStepFactory(importOrder);
5555
}
5656

57+
public void addPalantirJavaFormat(PalantirJavaFormat palantirJavaFormat) {
58+
addStepFactory(palantirJavaFormat);
59+
}
60+
5761
public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
5862
addStepFactory(removeUnusedImports);
5963
}

0 commit comments

Comments
 (0)