From 57d0c0bf3dcd008e8a34934130bcd8f6e30b1356 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Mon, 10 Jan 2022 17:36:58 -0500 Subject: [PATCH 01/13] fix #1076: Add support for palantir-java-format --- CHANGES.md | 1 + README.md | 2 + .../spotless/java/PalantirJavaFormatStep.java | 168 ++++++++++++++++++ plugin-gradle/README.md | 29 ++- .../gradle/spotless/JavaExtension.java | 52 +++++- .../PalantirJavaFormatIntegrationTest.java | 48 +++++ plugin-maven/README.md | 26 ++- .../diffplug/spotless/maven/java/Java.java | 6 +- .../maven/java/PalantirJavaFormat.java | 42 +++++ .../maven/java/PalantirJavaFormatTest.java | 64 +++++++ .../palantirjavaformat/JavaCodeFormatted.test | 11 ++ .../JavaCodeFormattedAOSP.test | 11 ++ .../JavaCodeUnformatted.test | 12 ++ .../JavaCodeWithLicenseFormatted.test | 16 ++ .../JavaCodeWithLicenseFormattedAOSP.test | 16 ++ .../JavaCodeWithLicenseUnformatted.test | 16 ++ .../JavaCodeWithPackageFormatted.test | 13 ++ .../JavaCodeWithPackageFormattedAOSP.test | 13 ++ .../JavaCodeWithPackageUnformatted.test | 13 ++ .../java/palantirjavaformat/TextBlock.clean | 14 ++ .../java/palantirjavaformat/TextBlock.dirty | 15 ++ .../java/PalantirJavaFormatStepTest.java | 102 +++++++++++ 22 files changed, 686 insertions(+), 4 deletions(-) create mode 100644 lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean create mode 100644 testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty create mode 100644 testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java diff --git a/CHANGES.md b/CHANGES.md index 04f139fd85..a606ce8834 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +TODO(ckozak) ## [2.21.2] - 2022-01-07 ### Fixed diff --git a/README.md b/README.md index b695671790..2823bb38ec 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ extra('cpp.EclipseFormatterStep') +'{{yes}} | {{yes}} extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', +lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', @@ -98,6 +99,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :+1: | :white_large_square: | +| [`java.PalantirJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :+1: | :white_large_square: | diff --git a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java new file mode 100644 index 0000000000..51f923eec3 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java @@ -0,0 +1,168 @@ +/* + * Copyright 2016-2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.java; + +import java.io.Serializable; +import java.lang.reflect.Method; +import java.util.Objects; + +import com.diffplug.spotless.*; +import com.diffplug.spotless.ThrowingEx.Function; + +/** Wraps up palantir-java-format fork of + * google-java-format as a FormatterStep. */ +public class PalantirJavaFormatStep { + // prevent direct instantiation + private PalantirJavaFormatStep() {} + + private static final String DEFAULT_STYLE = "PALANTIR"; + static final String NAME = "palantir-java-format"; + static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format"; + static final String FORMATTER_CLASS = "com.palantir.javaformat.java.Formatter"; + static final String FORMATTER_METHOD = "formatSource"; + + private static final String OPTIONS_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions"; + private static final String OPTIONS_BUILDER_METHOD = "builder"; + private static final String OPTIONS_BUILDER_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions$Builder"; + private static final String OPTIONS_BUILDER_STYLE_METHOD = "style"; + private static final String OPTIONS_BUILDER_BUILD_METHOD = "build"; + private static final String OPTIONS_Style = "com.palantir.javaformat.java.JavaFormatterOptions$Style"; + private static final String OPTIONS_MAX_LINE_LENGTH_METHOD = "maxLineLength"; + + private static final String REMOVE_UNUSED_CLASS = "com.palantir.javaformat.java.RemoveUnusedImports"; + private static final String REMOVE_UNUSED_METHOD = "removeUnusedImports"; + + private static final String IMPORT_ORDERER_CLASS = "com.palantir.javaformat.java.ImportOrderer"; + private static final String IMPORT_ORDERER_METHOD = "reorderImports"; + + /** Creates a step which formats everything - code, import order, and unused imports. */ + public static FormatterStep create(Provisioner provisioner) { + return create(defaultVersion(), provisioner); + } + + /** Creates a step which formats everything - code, import order, and unused imports. */ + public static FormatterStep create(String version, Provisioner provisioner) { + return create(version, DEFAULT_STYLE, provisioner); + } + + /** Creates a step which formats everything - code, import order, and unused imports. */ + public static FormatterStep create(String version, String style, Provisioner provisioner) { + return create(MAVEN_COORDINATE, version, style, provisioner); + } + + /** Creates a step which formats everything - groupArtifact, code, import order, and unused imports. */ + public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner) { + Objects.requireNonNull(groupArtifact, "groupArtifact"); + if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) { + throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'"); + } + Objects.requireNonNull(version, "version"); + Objects.requireNonNull(style, "style"); + Objects.requireNonNull(provisioner, "provisioner"); + return FormatterStep.createLazy(NAME, + () -> new State(NAME, groupArtifact, version, style, provisioner), + State::createFormat); + } + + static final Jvm.Support JVM_SUPPORT = Jvm. support(NAME).add(8, "1.1.0").add(11, "2.10.0"); + + public static String defaultGroupArtifact() { + return MAVEN_COORDINATE; + } + + /** Get default formatter version */ + public static String defaultVersion() { + return JVM_SUPPORT.getRecommendedFormatterVersion(); + } + + public static String defaultStyle() { + return DEFAULT_STYLE; + } + + static final class State implements Serializable { + private static final long serialVersionUID = 1L; + + /** The jar that contains the formatter. */ + final JarState jarState; + final String stepName; + final String version; + final String style; + + State(String stepName, String version, Provisioner provisioner) throws Exception { + this(stepName, version, DEFAULT_STYLE, provisioner); + } + + State(String stepName, String version, String style, Provisioner provisioner) throws Exception { + this(stepName, MAVEN_COORDINATE, version, style, provisioner); + } + + State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner) throws Exception { + JVM_SUPPORT.assertFormatterSupported(version); + this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); + this.stepName = stepName; + this.version = version; + this.style = style; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + FormatterFunc createFormat() throws Exception { + ClassLoader classLoader = jarState.getClassLoader(); + + // instantiate the formatter and get its format method + Class optionsClass = classLoader.loadClass(OPTIONS_CLASS); + Class optionsBuilderClass = classLoader.loadClass(OPTIONS_BUILDER_CLASS); + Method optionsBuilderMethod = optionsClass.getMethod(OPTIONS_BUILDER_METHOD); + Object optionsBuilder = optionsBuilderMethod.invoke(null); + + Class optionsStyleClass = classLoader.loadClass(OPTIONS_Style); + Object styleConstant = Enum.valueOf((Class) optionsStyleClass, style); + Method optionsBuilderStyleMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_STYLE_METHOD, optionsStyleClass); + optionsBuilderStyleMethod.invoke(optionsBuilder, styleConstant); + + Method optionsBuilderBuildMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_BUILD_METHOD); + Object options = optionsBuilderBuildMethod.invoke(optionsBuilder); + + Class formatterClazz = classLoader.loadClass(FORMATTER_CLASS); + Object formatter = formatterClazz.getMethod("createFormatter", optionsClass).invoke(null, options); + Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class); + + Function removeUnused = constructRemoveUnusedFunction(classLoader); + + Class importOrdererClass = classLoader.loadClass(IMPORT_ORDERER_CLASS); + Method importOrdererMethod = importOrdererClass.getMethod(IMPORT_ORDERER_METHOD, String.class); + + return JVM_SUPPORT.suggestLaterVersionOnError(version, (input -> { + String formatted = (String) formatterMethod.invoke(formatter, input); + String removedUnused = removeUnused.apply(formatted); + String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused); + return sortedImports; + })); + } + + FormatterFunc createRemoveUnusedImportsOnly() throws Exception { + ClassLoader classLoader = jarState.getClassLoader(); + Function removeUnused = constructRemoveUnusedFunction(classLoader); + return JVM_SUPPORT.suggestLaterVersionOnError(version, removeUnused::apply); + } + + private static Function constructRemoveUnusedFunction(ClassLoader classLoader) + throws NoSuchMethodException, ClassNotFoundException { + Class removeUnusedClass = classLoader.loadClass(REMOVE_UNUSED_CLASS); + Method removeUnusedMethod = removeUnusedClass.getMethod(REMOVE_UNUSED_METHOD, String.class); + return (x) -> (String) removeUnusedMethod.invoke(null, x); + } + } +} diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index f2ce18dd70..dcda33e912 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -58,7 +58,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [**Quickstart**](#quickstart) - [Requirements](#requirements) - **Languages** - - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier)) + - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier), [palantir-java-format](#palantir-java-format)) - [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy)) - [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier)) - [Scala](#scala) ([scalafmt](#scalafmt)) @@ -203,6 +203,33 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME ``` This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). +### palantir-java-format + +[homepage](https://github.com/palantir/palantir-java-format). [changelog](https://github.com/palantir/palantir-java-format/releases). +```gradle +spotless { + java { + palantirJavaFormat() + // optional: you can specify a specific version and/or switch to GOOGLE/AOSP style + // and/or use custom group artifact (you probably don't need this) + palantirJavaFormat('2.9.0').style("PALANTIR").groupArtifact('com.google.googlejavaformat:google-java-format') +``` + +**⚠️ Note on using Palantir Java Format with Java 16+** + +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. +These Flags can be provided using the `gradle.properties` file (See [documentation](https://docs.gradle.org/current/userguide/build_environment.html)). + +For example the following file under `gradle.properties` will run maven with the required flags: +``` +org.gradle.jvmargs=--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 +``` +This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). + ### eclipse jdt [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. diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 086c521737..c374faba9d 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.java.GoogleJavaFormatStep; import com.diffplug.spotless.java.ImportOrderStep; +import com.diffplug.spotless.java.PalantirJavaFormatStep; import com.diffplug.spotless.java.RemoveUnusedImportsStep; public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense { @@ -175,6 +176,55 @@ private FormatterStep createStep() { } } + /** Uses the palantir-java-format jar to format source code. */ + public PalantirJavaFormatConfig palantirJavaFormat() { + return palantirJavaFormat(PalantirJavaFormatStep.defaultVersion()); + } + + /** + * Uses the given version of palantir-java-format to format source code. + * + * Limited to published versions. See issue #33 + * for an workaround for using snapshot versions. + */ + public PalantirJavaFormatConfig palantirJavaFormat(String version) { + Objects.requireNonNull(version); + return new PalantirJavaFormatConfig(version); + } + + public class PalantirJavaFormatConfig { + final String version; + String groupArtifact; + String style; + + PalantirJavaFormatConfig(String version) { + this.version = Objects.requireNonNull(version); + this.groupArtifact = PalantirJavaFormatStep.defaultGroupArtifact(); + this.style = PalantirJavaFormatStep.defaultStyle(); + addStep(createStep()); + } + + public PalantirJavaFormatConfig groupArtifact(String groupArtifact) { + this.groupArtifact = Objects.requireNonNull(groupArtifact); + replaceStep(createStep()); + return this; + } + + public PalantirJavaFormatConfig style(String style) { + this.style = Objects.requireNonNull(style); + replaceStep(createStep()); + return this; + } + + private FormatterStep createStep() { + return PalantirJavaFormatStep.create( + groupArtifact, + version, + style, + provisioner()); + } + } + public EclipseConfig eclipse() { return new EclipseConfig(EclipseJdtFormatterStep.defaultVersion()); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java new file mode 100644 index 0000000000..6cf743e9f0 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.spotless; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +class PalantirJavaFormatIntegrationTest extends GradleIntegrationHarness { + @Test + void integration() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "", + "spotless {", + " java {", + " target file('test.java')", + " palantirJavaFormat('1.1.0')", + " }", + "}"); + + setFile("test.java").toResource("java/palantirjavaformat/JavaCodeUnformatted.test"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("test.java").sameAsResource("java/palantirjavaformat/JavaCodeFormatted.test"); + + checkRunsThenUpToDate(); + replace("build.gradle", + "palantirJavaFormat('1.1.0')", + "palantirJavaFormat('2.10.0')"); + checkRunsThenUpToDate(); + } +} diff --git a/plugin-maven/README.md b/plugin-maven/README.md index ea849273cd..a8e8df6a05 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -47,7 +47,7 @@ user@machine repo % mvn spotless:check - [Requirements](#requirements) - [Binding to maven phase](#binding-to-maven-phase) - **Languages** - - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier)) + - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format)) - [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy)) - [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier)) - [Scala](#scala) ([scalafmt](#scalafmt)) @@ -223,6 +223,30 @@ For example the following file under `.mvn/jvm.config` will run maven with the r ``` This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). +### palantir-java-format + +[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). + +```xml + + 2.10.0 + + + com.palantir.javaformat:palantir-java-format + +``` + +**⚠️ Note on using Palantir Java Format with Java 16+** + +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. +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)). + +For example the following file under `.mvn/jvm.config` will run maven with the required flags: +``` +--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 +``` +This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). + ### eclipse jdt [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. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index f48f2b742c..819179ebba 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,6 +54,10 @@ public void addImportOrder(ImportOrder importOrder) { addStepFactory(importOrder); } + public void addPalantirJavaFormat(PalantirJavaFormat palantirJavaFormat) { + addStepFactory(palantirJavaFormat); + } + public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) { addStepFactory(removeUnusedImports); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java new file mode 100644 index 0000000000..6eeb7d011f --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.java; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.java.PalantirJavaFormatStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class PalantirJavaFormat implements FormatterStepFactory { + @Parameter + private String groupArtifact; + + @Parameter + private String version; + + @Parameter + private String style; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + String groupArtifact = this.groupArtifact != null ? this.groupArtifact : PalantirJavaFormatStep.defaultGroupArtifact(); + String version = this.version != null ? this.version : PalantirJavaFormatStep.defaultVersion(); + String style = this.style != null ? this.style : PalantirJavaFormatStep.defaultStyle(); + return PalantirJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner()); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java new file mode 100644 index 0000000000..07adfe3beb --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.java; + +import static org.junit.jupiter.api.condition.JRE.JAVA_11; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; + +import com.diffplug.spotless.maven.MavenIntegrationHarness; + +class PalantirJavaFormatTest extends MavenIntegrationHarness { + @Test + void specificVersionDefaultStyle() throws Exception { + writePomWithJavaSteps( + "", + " 1.1.0", + ""); + + runTest("java/palantirjavaformat/JavaCodeFormatted.test"); + } + + @Test + void specificVersionSpecificStyle() throws Exception { + writePomWithJavaSteps( + "", + " 1.1.0", + " ", + ""); + + runTest("java/palantirjavaformat/JavaCodeFormattedAOSP.test"); + } + + @Test + @EnabledForJreRange(min = JAVA_11) + void specificJava11Version2() throws Exception { + writePomWithJavaSteps( + "", + " 2.10.0", + ""); + + runTest("java/palantirjavaformat/JavaCodeFormatted.test"); + } + + private void runTest(String targetResource) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toResource("java/palantirjavaformat/JavaCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource(targetResource); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test new file mode 100644 index 0000000000..25ccbc25f5 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test @@ -0,0 +1,11 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test new file mode 100644 index 0000000000..25ccbc25f5 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test @@ -0,0 +1,11 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test new file mode 100644 index 0000000000..18f2aca0e2 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test @@ -0,0 +1,12 @@ + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test new file mode 100644 index 0000000000..ab0bbbf033 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test @@ -0,0 +1,16 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test new file mode 100644 index 0000000000..ab0bbbf033 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test @@ -0,0 +1,16 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test new file mode 100644 index 0000000000..f43756b938 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test @@ -0,0 +1,16 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test new file mode 100644 index 0000000000..5c2c225e68 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test @@ -0,0 +1,13 @@ +package hello.world; + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test new file mode 100644 index 0000000000..5c2c225e68 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test @@ -0,0 +1,13 @@ +package hello.world; + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test new file mode 100644 index 0000000000..61b1b8fce4 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test @@ -0,0 +1,13 @@ +package hello.world; + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean new file mode 100644 index 0000000000..7f60a80cf3 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean @@ -0,0 +1,14 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty new file mode 100644 index 0000000000..0f5846d711 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty @@ -0,0 +1,15 @@ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java new file mode 100644 index 0000000000..b267c9b2c6 --- /dev/null +++ b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.java; + +import static org.junit.jupiter.api.condition.JRE.JAVA_11; +import static org.junit.jupiter.api.condition.JRE.JAVA_13; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.ResourceHarness; +import com.diffplug.spotless.SerializableEqualityTester; +import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.TestProvisioner; + +class PalantirJavaFormatStepTest extends ResourceHarness { + + @Test + @EnabledForJreRange(min = JAVA_13) + void jvm13Features() throws Exception { + try (StepHarness step = StepHarness.forStep(PalantirJavaFormatStep.create(TestProvisioner.mavenCentral()))) { + step.testResource("java/palantirjavaformat/TextBlock.dirty", "java/palantirjavaformat/TextBlock.clean"); + } + } + + @Test + @EnabledForJreRange(min = JAVA_11) // google-java-format requires JRE 11+ + void behavior2() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create("2.10.0", TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); + } + + @Test + void behavior() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create("1.1.0", TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); + } + + @Test + void behaviorWithAospStyle() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create("1.1.0", "AOSP", TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormattedAOSP.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test"); + } + + @Test + void behaviorWithCustomGroupArtifact() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create(PalantirJavaFormatStep.defaultGroupArtifact(), "1.1.0", PalantirJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); + } + + @Test + void equality() throws Exception { + new SerializableEqualityTester() { + String version = "1.1.0"; + String style = ""; + + @Override + protected void setupTest(API api) { + // same version == same + api.areDifferentThan(); + // change the version, and it's different + version = "1.0.0"; + api.areDifferentThan(); + // change the style, and it's different + style = "AOSP"; + api.areDifferentThan(); + } + + @Override + protected FormatterStep create() { + String finalVersion = this.version; + return PalantirJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral()); + } + }.testEquals(); + } +} From fb02b2dc64375f9847ed49855ad969397a48c5e9 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Mon, 10 Jan 2022 17:59:19 -0500 Subject: [PATCH 02/13] Release notes in CHANGES.md --- CHANGES.md | 3 ++- plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a606ce8834..6852314801 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] -TODO(ckozak) +### Added +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [2.21.2] - 2022-01-07 ### Fixed diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c83152a628..9ee82be22f 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [6.1.2] - 2022-01-07 ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 50c9ce98aa..5c328b5ad9 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [2.19.1] - 2022-01-07 ### Fixed From d7dd8cdf2088345c181dd7a4af3cdb517699f375 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Mon, 10 Jan 2022 18:23:49 -0500 Subject: [PATCH 03/13] Fix PalantirJavaFormatIntegrationTest gradle integration test Both versions tested are compatible with java 8 --- .../gradle/spotless/PalantirJavaFormatIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java index 6cf743e9f0..2283ce34bf 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java @@ -42,7 +42,7 @@ void integration() throws IOException { checkRunsThenUpToDate(); replace("build.gradle", "palantirJavaFormat('1.1.0')", - "palantirJavaFormat('2.10.0')"); + "palantirJavaFormat('1.0.1')"); checkRunsThenUpToDate(); } } From 6aff52d7f8ea25d05772ec8cd76688946a16f1e9 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Tue, 11 Jan 2022 11:43:59 -0500 Subject: [PATCH 04/13] Remove unused, fix `OPTIONS_STYLE` casing --- .../spotless/java/PalantirJavaFormatStep.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java index 51f923eec3..5bd065ebba 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java @@ -39,8 +39,7 @@ private PalantirJavaFormatStep() {} private static final String OPTIONS_BUILDER_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions$Builder"; private static final String OPTIONS_BUILDER_STYLE_METHOD = "style"; private static final String OPTIONS_BUILDER_BUILD_METHOD = "build"; - private static final String OPTIONS_Style = "com.palantir.javaformat.java.JavaFormatterOptions$Style"; - private static final String OPTIONS_MAX_LINE_LENGTH_METHOD = "maxLineLength"; + private static final String OPTIONS_STYLE = "com.palantir.javaformat.java.JavaFormatterOptions$Style"; private static final String REMOVE_UNUSED_CLASS = "com.palantir.javaformat.java.RemoveUnusedImports"; private static final String REMOVE_UNUSED_METHOD = "removeUnusedImports"; @@ -101,14 +100,6 @@ static final class State implements Serializable { final String version; final String style; - State(String stepName, String version, Provisioner provisioner) throws Exception { - this(stepName, version, DEFAULT_STYLE, provisioner); - } - - State(String stepName, String version, String style, Provisioner provisioner) throws Exception { - this(stepName, MAVEN_COORDINATE, version, style, provisioner); - } - State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner) throws Exception { JVM_SUPPORT.assertFormatterSupported(version); this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); @@ -127,7 +118,7 @@ FormatterFunc createFormat() throws Exception { Method optionsBuilderMethod = optionsClass.getMethod(OPTIONS_BUILDER_METHOD); Object optionsBuilder = optionsBuilderMethod.invoke(null); - Class optionsStyleClass = classLoader.loadClass(OPTIONS_Style); + Class optionsStyleClass = classLoader.loadClass(OPTIONS_STYLE); Object styleConstant = Enum.valueOf((Class) optionsStyleClass, style); Method optionsBuilderStyleMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_STYLE_METHOD, optionsStyleClass); optionsBuilderStyleMethod.invoke(optionsBuilder, styleConstant); @@ -147,17 +138,10 @@ FormatterFunc createFormat() throws Exception { return JVM_SUPPORT.suggestLaterVersionOnError(version, (input -> { String formatted = (String) formatterMethod.invoke(formatter, input); String removedUnused = removeUnused.apply(formatted); - String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused); - return sortedImports; + return (String) importOrdererMethod.invoke(null, removedUnused); })); } - FormatterFunc createRemoveUnusedImportsOnly() throws Exception { - ClassLoader classLoader = jarState.getClassLoader(); - Function removeUnused = constructRemoveUnusedFunction(classLoader); - return JVM_SUPPORT.suggestLaterVersionOnError(version, removeUnused::apply); - } - private static Function constructRemoveUnusedFunction(ClassLoader classLoader) throws NoSuchMethodException, ClassNotFoundException { Class removeUnusedClass = classLoader.loadClass(REMOVE_UNUSED_CLASS); From 368c75fbd3a0ddfc310cddfe0aaea5db3be7ce06 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 11 Jan 2022 10:59:11 -0800 Subject: [PATCH 05/13] Bump no_maven_15 to no_maven_17 since it is now the latest available JDK. --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 48ba552c3e..4855bb8f54 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -77,11 +77,11 @@ jobs: docker: - image: cimg/openjdk:11.0 <<: *test_nomaven - test_nomaven_15: - # latest JDK, replace with 16 when it comes out + test_nomaven_17: + # latest JDK <<: *env_gradle_large docker: - - image: cimg/openjdk:15.0 + - image: cimg/openjdk:17.0 <<: *test_nomaven test_justmaven_11: << : *env_gradle From b38c3b5126df33f6421ea7bddbeabee497e4a353 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 11 Jan 2022 11:08:34 -0800 Subject: [PATCH 06/13] Attempt to resolve `java.lang.OutOfMemoryError: Metaspace` on CI. --- gradle.properties | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gradle.properties b/gradle.properties index 2f4f7952a4..a4f33272ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,6 @@ +# To fix metaspace errors +org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + name=spotless description=Spotless - keep your code spotless with Gradle org=diffplug From e742e2e9f6b5caa7ec1048f77e9d839aaa2bf139 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 11 Jan 2022 11:13:41 -0800 Subject: [PATCH 07/13] Fix broken nomaven_15. --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4855bb8f54..8a6582207d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -275,7 +275,7 @@ workflows: - test_nomaven_11: requires: - assemble_testClasses - - test_nomaven_15: + - test_nomaven_17: requires: - assemble_testClasses - test_npm_8: From 9ab3a25dd63fc3cd289c99e86f9854a0eb351545 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 11 Jan 2022 11:44:50 -0800 Subject: [PATCH 08/13] Java 17 doesn't support footers in javadoc anymore. --- gradle/java-publish.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/gradle/java-publish.gradle b/gradle/java-publish.gradle index 62779d8b7d..78e73a351f 100644 --- a/gradle/java-publish.gradle +++ b/gradle/java-publish.gradle @@ -66,7 +66,6 @@ javadoc { options.addStringOption('source', '8') // setup the header options.header javadocInfo - options.footer javadocInfo // setup links options.linksOffline('https://docs.oracle.com/javase/8/docs/api/', "${dotdotGradle}/javadoc/java8") options.linksOffline('https://docs.gradle.org/2.14/javadoc/', "${dotdotGradle}/javadoc/gradle") From e9352f67e4f3ed08b3a12e081acfd4710a2208ff Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 11 Jan 2022 12:00:32 -0800 Subject: [PATCH 09/13] Go back to no_maven_15, I shouldn't have squashed that into this PR. --- .circleci/config.yml | 8 ++++---- gradle/java-publish.gradle | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8a6582207d..48ba552c3e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -77,11 +77,11 @@ jobs: docker: - image: cimg/openjdk:11.0 <<: *test_nomaven - test_nomaven_17: - # latest JDK + test_nomaven_15: + # latest JDK, replace with 16 when it comes out <<: *env_gradle_large docker: - - image: cimg/openjdk:17.0 + - image: cimg/openjdk:15.0 <<: *test_nomaven test_justmaven_11: << : *env_gradle @@ -275,7 +275,7 @@ workflows: - test_nomaven_11: requires: - assemble_testClasses - - test_nomaven_17: + - test_nomaven_15: requires: - assemble_testClasses - test_npm_8: diff --git a/gradle/java-publish.gradle b/gradle/java-publish.gradle index 78e73a351f..62779d8b7d 100644 --- a/gradle/java-publish.gradle +++ b/gradle/java-publish.gradle @@ -66,6 +66,7 @@ javadoc { options.addStringOption('source', '8') // setup the header options.header javadocInfo + options.footer javadocInfo // setup links options.linksOffline('https://docs.oracle.com/javase/8/docs/api/', "${dotdotGradle}/javadoc/java8") options.linksOffline('https://docs.gradle.org/2.14/javadoc/', "${dotdotGradle}/javadoc/gradle") From 0d83dca8cc087a2773d1202ccf84e58a54d65631 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Tue, 11 Jan 2022 16:23:59 -0500 Subject: [PATCH 10/13] 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. --- lib/build.gradle | 3 + .../spotless/java/PalantirJavaFormatStep.java | 112 +++--------------- .../pjf/PalantirJavaFormatFormatterFunc.java | 47 ++++++++ plugin-gradle/README.md | 5 +- .../gradle/spotless/JavaExtension.java | 22 +--- plugin-maven/README.md | 3 - .../maven/java/PalantirJavaFormat.java | 9 +- .../JavaCodeFormattedAOSP.test | 11 -- .../JavaCodeWithLicenseFormattedAOSP.test | 16 --- .../JavaCodeWithPackageFormattedAOSP.test | 13 -- .../java/PalantirJavaFormatStepTest.java | 28 +---- 11 files changed, 74 insertions(+), 195 deletions(-) create mode 100644 lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java delete mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test delete mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test delete mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test diff --git a/lib/build.gradle b/lib/build.gradle index e49d13f5d5..90d985ad69 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -8,6 +8,7 @@ apply from: rootProject.file('gradle/java-publish.gradle') def NEEDS_GLUE = [ 'sortPom', + 'palantirJavaFormat', 'ktlint', 'flexmark' ] @@ -29,6 +30,8 @@ dependencies { // used for pom sorting sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0' + palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' + String VER_KTLINT='0.43.2' ktlintCompileOnly "com.pinterest:ktlint:$VER_KTLINT" ktlintCompileOnly "com.pinterest.ktlint:ktlint-core:$VER_KTLINT" diff --git a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java index 5bd065ebba..a5474d549b 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java @@ -16,11 +16,10 @@ package com.diffplug.spotless.java; import java.io.Serializable; -import java.lang.reflect.Method; +import java.lang.reflect.Constructor; import java.util.Objects; import com.diffplug.spotless.*; -import com.diffplug.spotless.ThrowingEx.Function; /** Wraps up palantir-java-format fork of * google-java-format as a FormatterStep. */ @@ -28,24 +27,9 @@ public class PalantirJavaFormatStep { // prevent direct instantiation private PalantirJavaFormatStep() {} - private static final String DEFAULT_STYLE = "PALANTIR"; - static final String NAME = "palantir-java-format"; - static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format"; - static final String FORMATTER_CLASS = "com.palantir.javaformat.java.Formatter"; - static final String FORMATTER_METHOD = "formatSource"; - - private static final String OPTIONS_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions"; - private static final String OPTIONS_BUILDER_METHOD = "builder"; - private static final String OPTIONS_BUILDER_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions$Builder"; - private static final String OPTIONS_BUILDER_STYLE_METHOD = "style"; - private static final String OPTIONS_BUILDER_BUILD_METHOD = "build"; - private static final String OPTIONS_STYLE = "com.palantir.javaformat.java.JavaFormatterOptions$Style"; - - private static final String REMOVE_UNUSED_CLASS = "com.palantir.javaformat.java.RemoveUnusedImports"; - private static final String REMOVE_UNUSED_METHOD = "removeUnusedImports"; - - private static final String IMPORT_ORDERER_CLASS = "com.palantir.javaformat.java.ImportOrderer"; - private static final String IMPORT_ORDERER_METHOD = "reorderImports"; + private static final String NAME = "palantir-java-format"; + private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:"; + private static final Jvm.Support JVM_SUPPORT = Jvm. support(NAME).add(8, "1.1.0").add(11, "2.10.0"); /** Creates a step which formats everything - code, import order, and unused imports. */ public static FormatterStep create(Provisioner provisioner) { @@ -54,99 +38,37 @@ public static FormatterStep create(Provisioner provisioner) { /** Creates a step which formats everything - code, import order, and unused imports. */ public static FormatterStep create(String version, Provisioner provisioner) { - return create(version, DEFAULT_STYLE, provisioner); - } - - /** Creates a step which formats everything - code, import order, and unused imports. */ - public static FormatterStep create(String version, String style, Provisioner provisioner) { - return create(MAVEN_COORDINATE, version, style, provisioner); - } - - /** Creates a step which formats everything - groupArtifact, code, import order, and unused imports. */ - public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner) { - Objects.requireNonNull(groupArtifact, "groupArtifact"); - if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) { - throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'"); - } Objects.requireNonNull(version, "version"); - Objects.requireNonNull(style, "style"); Objects.requireNonNull(provisioner, "provisioner"); + return FormatterStep.createLazy(NAME, - () -> new State(NAME, groupArtifact, version, style, provisioner), + () -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version), State::createFormat); } - static final Jvm.Support JVM_SUPPORT = Jvm. support(NAME).add(8, "1.1.0").add(11, "2.10.0"); - - public static String defaultGroupArtifact() { - return MAVEN_COORDINATE; - } - /** Get default formatter version */ public static String defaultVersion() { return JVM_SUPPORT.getRecommendedFormatterVersion(); } - public static String defaultStyle() { - return DEFAULT_STYLE; - } - - static final class State implements Serializable { + private static final class State implements Serializable { private static final long serialVersionUID = 1L; /** The jar that contains the formatter. */ - final JarState jarState; - final String stepName; - final String version; - final String style; + private final JarState jarState; + /** Version of the formatter jar. */ + private final String formatterVersion; - State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner) throws Exception { - JVM_SUPPORT.assertFormatterSupported(version); - this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); - this.stepName = stepName; - this.version = version; - this.style = style; + State(JarState jarState, String formatterVersion) { + this.jarState = jarState; + this.formatterVersion = formatterVersion; } - @SuppressWarnings({"unchecked", "rawtypes"}) FormatterFunc createFormat() throws Exception { - ClassLoader classLoader = jarState.getClassLoader(); - - // instantiate the formatter and get its format method - Class optionsClass = classLoader.loadClass(OPTIONS_CLASS); - Class optionsBuilderClass = classLoader.loadClass(OPTIONS_BUILDER_CLASS); - Method optionsBuilderMethod = optionsClass.getMethod(OPTIONS_BUILDER_METHOD); - Object optionsBuilder = optionsBuilderMethod.invoke(null); - - Class optionsStyleClass = classLoader.loadClass(OPTIONS_STYLE); - Object styleConstant = Enum.valueOf((Class) optionsStyleClass, style); - Method optionsBuilderStyleMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_STYLE_METHOD, optionsStyleClass); - optionsBuilderStyleMethod.invoke(optionsBuilder, styleConstant); - - Method optionsBuilderBuildMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_BUILD_METHOD); - Object options = optionsBuilderBuildMethod.invoke(optionsBuilder); - - Class formatterClazz = classLoader.loadClass(FORMATTER_CLASS); - Object formatter = formatterClazz.getMethod("createFormatter", optionsClass).invoke(null, options); - Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class); - - Function removeUnused = constructRemoveUnusedFunction(classLoader); - - Class importOrdererClass = classLoader.loadClass(IMPORT_ORDERER_CLASS); - Method importOrdererMethod = importOrdererClass.getMethod(IMPORT_ORDERER_METHOD, String.class); - - return JVM_SUPPORT.suggestLaterVersionOnError(version, (input -> { - String formatted = (String) formatterMethod.invoke(formatter, input); - String removedUnused = removeUnused.apply(formatted); - return (String) importOrdererMethod.invoke(null, removedUnused); - })); - } - - private static Function constructRemoveUnusedFunction(ClassLoader classLoader) - throws NoSuchMethodException, ClassNotFoundException { - Class removeUnusedClass = classLoader.loadClass(REMOVE_UNUSED_CLASS); - Method removeUnusedMethod = removeUnusedClass.getMethod(REMOVE_UNUSED_METHOD, String.class); - return (x) -> (String) removeUnusedMethod.invoke(null, x); + final ClassLoader classLoader = jarState.getClassLoader(); + final Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc"); + final Constructor constructor = formatterFunc.getConstructor(); + return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance()); } } } diff --git a/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java new file mode 100644 index 0000000000..6824cdbc48 --- /dev/null +++ b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java @@ -0,0 +1,47 @@ +/* + * Copyright 2022 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.glue.pjf; + +import com.palantir.javaformat.java.Formatter; +import com.palantir.javaformat.java.ImportOrderer; +import com.palantir.javaformat.java.JavaFormatterOptions; +import com.palantir.javaformat.java.RemoveUnusedImports; + +import com.diffplug.spotless.FormatterFunc; + +public class PalantirJavaFormatFormatterFunc implements FormatterFunc { + + private final Formatter formatter; + + public PalantirJavaFormatFormatterFunc() { + formatter = Formatter.createFormatter(JavaFormatterOptions.builder() + .style(JavaFormatterOptions.Style.PALANTIR) + .build()); + } + + @Override + public String apply(String input) throws Exception { + String source = input; + source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR); + source = RemoveUnusedImports.removeUnusedImports(source); + return formatter.formatSource(source); + } + + @Override + public String toString() { + return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}'; + } +} diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index dcda33e912..9712e5f837 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -210,9 +210,8 @@ This is a workaround to a [pending issue](https://github.com/diffplug/spotless/i spotless { java { palantirJavaFormat() - // optional: you can specify a specific version and/or switch to GOOGLE/AOSP style - // and/or use custom group artifact (you probably don't need this) - palantirJavaFormat('2.9.0').style("PALANTIR").groupArtifact('com.google.googlejavaformat:google-java-format') + // optional: you can specify a specific version + palantirJavaFormat('2.9.0') ``` **⚠️ Note on using Palantir Java Format with Java 16+** diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index c374faba9d..479d2ffd12 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -194,34 +194,14 @@ public PalantirJavaFormatConfig palantirJavaFormat(String version) { public class PalantirJavaFormatConfig { final String version; - String groupArtifact; - String style; PalantirJavaFormatConfig(String version) { this.version = Objects.requireNonNull(version); - this.groupArtifact = PalantirJavaFormatStep.defaultGroupArtifact(); - this.style = PalantirJavaFormatStep.defaultStyle(); addStep(createStep()); } - public PalantirJavaFormatConfig groupArtifact(String groupArtifact) { - this.groupArtifact = Objects.requireNonNull(groupArtifact); - replaceStep(createStep()); - return this; - } - - public PalantirJavaFormatConfig style(String style) { - this.style = Objects.requireNonNull(style); - replaceStep(createStep()); - return this; - } - private FormatterStep createStep() { - return PalantirJavaFormatStep.create( - groupArtifact, - version, - style, - provisioner()); + return PalantirJavaFormatStep.create(version, provisioner()); } } diff --git a/plugin-maven/README.md b/plugin-maven/README.md index e996c66bee..887522e60e 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -230,9 +230,6 @@ This is a workaround to a [pending issue](https://github.com/diffplug/spotless/i ```xml 2.10.0 - - - com.palantir.javaformat:palantir-java-format ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java index 6eeb7d011f..df58764c1d 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java @@ -23,20 +23,13 @@ import com.diffplug.spotless.maven.FormatterStepFactory; public class PalantirJavaFormat implements FormatterStepFactory { - @Parameter - private String groupArtifact; @Parameter private String version; - @Parameter - private String style; - @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - String groupArtifact = this.groupArtifact != null ? this.groupArtifact : PalantirJavaFormatStep.defaultGroupArtifact(); String version = this.version != null ? this.version : PalantirJavaFormatStep.defaultVersion(); - String style = this.style != null ? this.style : PalantirJavaFormatStep.defaultStyle(); - return PalantirJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner()); + return PalantirJavaFormatStep.create(version, config.getProvisioner()); } } diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test deleted file mode 100644 index 25ccbc25f5..0000000000 --- a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedAOSP.test +++ /dev/null @@ -1,11 +0,0 @@ -import mylib.UsedA; -import mylib.UsedB; - -public class Java { - public static void main(String[] args) { - System.out.println( - "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); - UsedB.someMethod(); - UsedA.someMethod(); - } -} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test deleted file mode 100644 index ab0bbbf033..0000000000 --- a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Some license stuff. - * Very official. - */ - -import mylib.UsedA; -import mylib.UsedB; - -public class Java { - public static void main(String[] args) { - System.out.println( - "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); - UsedB.someMethod(); - UsedA.someMethod(); - } -} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test deleted file mode 100644 index 5c2c225e68..0000000000 --- a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test +++ /dev/null @@ -1,13 +0,0 @@ -package hello.world; - -import mylib.UsedA; -import mylib.UsedB; - -public class Java { - public static void main(String[] args) { - System.out.println( - "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); - UsedB.someMethod(); - UsedA.someMethod(); - } -} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java index b267c9b2c6..a589992567 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java @@ -38,7 +38,7 @@ void jvm13Features() throws Exception { } @Test - @EnabledForJreRange(min = JAVA_11) // google-java-format requires JRE 11+ + @EnabledForJreRange(min = JAVA_11) void behavior2() throws Exception { FormatterStep step = PalantirJavaFormatStep.create("2.10.0", TestProvisioner.mavenCentral()); StepHarness.forStep(step) @@ -57,28 +57,9 @@ void behavior() throws Exception { } @Test - void behaviorWithAospStyle() throws Exception { - FormatterStep step = PalantirJavaFormatStep.create("1.1.0", "AOSP", TestProvisioner.mavenCentral()); - StepHarness.forStep(step) - .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormattedAOSP.test") - .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormattedAOSP.test") - .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormattedAOSP.test"); - } - - @Test - void behaviorWithCustomGroupArtifact() throws Exception { - FormatterStep step = PalantirJavaFormatStep.create(PalantirJavaFormatStep.defaultGroupArtifact(), "1.1.0", PalantirJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral()); - StepHarness.forStep(step) - .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") - .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") - .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); - } - - @Test - void equality() throws Exception { + void equality() { new SerializableEqualityTester() { String version = "1.1.0"; - String style = ""; @Override protected void setupTest(API api) { @@ -87,15 +68,12 @@ protected void setupTest(API api) { // change the version, and it's different version = "1.0.0"; api.areDifferentThan(); - // change the style, and it's different - style = "AOSP"; - api.areDifferentThan(); } @Override protected FormatterStep create() { String finalVersion = this.version; - return PalantirJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral()); + return PalantirJavaFormatStep.create(finalVersion, TestProvisioner.mavenCentral()); } }.testEquals(); } From ac019ebe4a545ee6db51d05c9d150f678adf4b4e Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Tue, 11 Jan 2022 16:54:16 -0500 Subject: [PATCH 11/13] remove test with reference to the removed style field --- .../spotless/maven/java/PalantirJavaFormatTest.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java index 07adfe3beb..33ec8ea84f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java @@ -33,17 +33,6 @@ void specificVersionDefaultStyle() throws Exception { runTest("java/palantirjavaformat/JavaCodeFormatted.test"); } - @Test - void specificVersionSpecificStyle() throws Exception { - writePomWithJavaSteps( - "", - " 1.1.0", - " ", - ""); - - runTest("java/palantirjavaformat/JavaCodeFormattedAOSP.test"); - } - @Test @EnabledForJreRange(min = JAVA_11) void specificJava11Version2() throws Exception { From a5bf1fe7065304da3ae9c78f5f361879b687f989 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Jan 2022 11:26:16 -0800 Subject: [PATCH 12/13] Remove the CodeQL workflow because it is using jcenter which is now dead. --- .github/workflows/codeql-analysis.yml | 71 --------------------------- 1 file changed, 71 deletions(-) delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 8af7ad4b44..0000000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,71 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -name: "CodeQL" - -on: - push: - branches: [main] - pull_request: - # The branches below must be a subset of the branches above - branches: [main] - schedule: - - cron: '0 16 * * 3' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - # Override automatic language detection by changing the below list - # Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python'] - language: ['java', 'javascript'] - # Learn more... - # https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - # We must fetch at least the immediate parents so that if this is - # a pull request then we can checkout the head. - fetch-depth: 0 - - # If this run was triggered by a pull request event, then checkout - # the head of the pull request instead of the merge commit. - - run: git checkout HEAD^2 - if: ${{ github.event_name == 'pull_request' }} - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # ℹ️ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 From f4db9e01fc505a9d16edca9128aa450585b30e6a Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Jan 2022 11:33:53 -0800 Subject: [PATCH 13/13] Revert "Remove the CodeQL workflow because it is using jcenter which is now dead." This reverts commit a5bf1fe7065304da3ae9c78f5f361879b687f989. --- .github/workflows/codeql-analysis.yml | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000000..8af7ad4b44 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,71 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +name: "CodeQL" + +on: + push: + branches: [main] + pull_request: + # The branches below must be a subset of the branches above + branches: [main] + schedule: + - cron: '0 16 * * 3' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + # Override automatic language detection by changing the below list + # Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python'] + language: ['java', 'javascript'] + # Learn more... + # https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + # We must fetch at least the immediate parents so that if this is + # a pull request then we can checkout the head. + fetch-depth: 0 + + # If this run was triggered by a pull request event, then checkout + # the head of the pull request instead of the merge commit. + - run: git checkout HEAD^2 + if: ${{ github.event_name == 'pull_request' }} + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1