Skip to content

Commit a851e9f

Browse files
author
Vincent Potucek
committed
PalantirJavaFormatFormatterFormatSourceAndFixImportsAndDeclarationsFunc
1 parent d5cc199 commit a851e9f

File tree

3 files changed

+106
-35
lines changed

3 files changed

+106
-35
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
package com.diffplug.spotless.java;
1717

1818
import com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFormatSourceAndFixImportsAndDeclarationsFunc;
1920
import com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc;
2021

2122
/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
2223
public interface RemoveUnusedDeclarationsStep {
2324
String NAME = "removeUnusedImports";
2425

2526
static FormatterStep create() {
26-
return new PalantirJavaFormatFormatterFunc("PALANTIR", true);
27+
return new PalantirJavaFormatFormatterFormatSourceAndFixImportsAndDeclarationsFunc("PALANTIR", true);
2728
}
2829
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2022-2025 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 java.io.File;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
21+
import java.util.List;
22+
23+
import javax.annotation.Nullable;
24+
25+
import com.palantir.javaformat.java.Formatter;
26+
import com.palantir.javaformat.java.ImportOrderer;
27+
import com.palantir.javaformat.java.JavaFormatterOptions;
28+
import com.palantir.javaformat.java.RemoveUnusedImports;
29+
30+
import com.diffplug.spotless.FormatterFunc;
31+
import com.diffplug.spotless.FormatterStep;
32+
import com.diffplug.spotless.Lint;
33+
34+
public class PalantirJavaFormatFormatterFormatSourceAndFixImportsAndDeclarationsFunc implements FormatterFunc, FormatterStep {
35+
36+
private final Formatter formatter;
37+
38+
private final JavaFormatterOptions.Style formatterStyle;
39+
40+
/**
41+
* Creates a new formatter func that formats code via Palantir.
42+
* @param style The style to use for formatting.
43+
* @param formatJavadoc Whether to format Java docs. Requires at least Palantir 2.36.0 or later, otherwise the
44+
* constructor will throw.
45+
*/
46+
public PalantirJavaFormatFormatterFormatSourceAndFixImportsAndDeclarationsFunc(String style, boolean formatJavadoc) {
47+
this.formatterStyle = JavaFormatterOptions.Style.valueOf(style);
48+
JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder();
49+
builder.style(formatterStyle);
50+
if (formatJavadoc) {
51+
applyFormatJavadoc(builder);
52+
}
53+
formatter = Formatter.createFormatter(builder.build());
54+
}
55+
56+
@Override
57+
public String apply(String input) throws Exception {
58+
return formatter.formatSourceAndFixImportsAndDeclarations(input);
59+
}
60+
61+
@Override
62+
public String apply(String unix, File file) throws Exception {
63+
return FormatterFunc.super.apply(unix, file);
64+
}
65+
66+
@Override
67+
public List<Lint> lint(String content, File file) throws Exception {
68+
return FormatterFunc.super.lint(content, file);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}';
74+
}
75+
76+
private static void applyFormatJavadoc(JavaFormatterOptions.Builder builder) {
77+
// The formatJavadoc option is available since Palantir 2.36.0
78+
// To support older versions for now, attempt to invoke the builder method via reflection.
79+
try {
80+
Method formatJavadoc = JavaFormatterOptions.Builder.class.getMethod("formatJavadoc", boolean.class);
81+
formatJavadoc.invoke(builder, true);
82+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
83+
throw new IllegalStateException("Cannot enable formatJavadoc option, make sure you are using Palantir with version 2.36.0 or later", e);
84+
}
85+
}
86+
87+
@Override
88+
public String getName() {
89+
return toString();
90+
}
91+
92+
@Nullable @Override
93+
public String format(String rawUnix, File file) throws Exception {
94+
return apply(rawUnix);
95+
}
96+
97+
@Override
98+
public void close() throws Exception {
99+
100+
}
101+
}

lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2025 DiffPlug
2+
* Copyright 2022-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.
@@ -15,23 +15,17 @@
1515
*/
1616
package com.diffplug.spotless.glue.pjf;
1717

18-
import java.io.File;
1918
import java.lang.reflect.InvocationTargetException;
2019
import java.lang.reflect.Method;
21-
import java.util.List;
22-
23-
import javax.annotation.Nullable;
2420

2521
import com.palantir.javaformat.java.Formatter;
2622
import com.palantir.javaformat.java.ImportOrderer;
2723
import com.palantir.javaformat.java.JavaFormatterOptions;
2824
import com.palantir.javaformat.java.RemoveUnusedImports;
2925

3026
import com.diffplug.spotless.FormatterFunc;
31-
import com.diffplug.spotless.FormatterStep;
32-
import com.diffplug.spotless.Lint;
3327

34-
public class PalantirJavaFormatFormatterFunc implements FormatterFunc, FormatterStep {
28+
public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
3529

3630
private final Formatter formatter;
3731

@@ -58,17 +52,7 @@ public String apply(String input) throws Exception {
5852
String source = input;
5953
source = ImportOrderer.reorderImports(source, formatterStyle);
6054
source = RemoveUnusedImports.removeUnusedImports(source);
61-
return formatter.formatSourceAndFixImportsAndDeclarations(source);
62-
}
63-
64-
@Override
65-
public String apply(String unix, File file) throws Exception {
66-
return FormatterFunc.super.apply(unix, file);
67-
}
68-
69-
@Override
70-
public List<Lint> lint(String content, File file) throws Exception {
71-
return FormatterFunc.super.lint(content, file);
55+
return formatter.formatSource(source);
7256
}
7357

7458
@Override
@@ -86,19 +70,4 @@ private static void applyFormatJavadoc(JavaFormatterOptions.Builder builder) {
8670
throw new IllegalStateException("Cannot enable formatJavadoc option, make sure you are using Palantir with version 2.36.0 or later", e);
8771
}
8872
}
89-
90-
@Override
91-
public String getName() {
92-
return toString();
93-
}
94-
95-
@Nullable @Override
96-
public String format(String rawUnix, File file) throws Exception {
97-
return apply(rawUnix);
98-
}
99-
100-
@Override
101-
public void close() throws Exception {
102-
103-
}
10473
}

0 commit comments

Comments
 (0)