|
| 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 | +} |
0 commit comments