diff --git a/.github/workflows/checkstyle.yml b/.github/workflows/checkstyle.yml
deleted file mode 100644
index eb0ea0b887..0000000000
--- a/.github/workflows/checkstyle.yml
+++ /dev/null
@@ -1,24 +0,0 @@
-name: Checkstyle
-
-on: [push, pull_request]
-
-jobs:
- checkstyle:
- runs-on: ubuntu-latest
- strategy:
- matrix:
- java: [ 11 ]
- steps:
- - name: Checkout Java Client
- uses: actions/checkout@v3
-
- - name: Set up JDK ${{ matrix.java }}
- uses: actions/setup-java@v3
- with:
- java-version: ${{ matrix.java }}
- distribution: 'temurin'
- cache: 'gradle'
-
- - name: Check style and license headers
- run: |
- ./gradlew checkstyleMain checkstyleTest
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e804fbb6c..03a5a898d9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Dependencies
### Changed
+- Migrated from checkstyle to spotless ([#648](https://github.com/opensearch-project/opensearch-java/pull/648))
### Deprecated
diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md
index c90e138b40..6969aba856 100644
--- a/DEVELOPER_GUIDE.md
+++ b/DEVELOPER_GUIDE.md
@@ -97,11 +97,28 @@ Follow links in the [Java Tutorial](https://code.visualstudio.com/docs/java/java
## Java Language Formatting Guidelines
-Java files in the opensearch-java codebase are formatted with the [checkstyle plugin](https://docs.gradle.org/current/userguide/checkstyle_plugin.html). This plugin is configured using [checkstyle.xml](config/checkstyle/checkstyle.xml). To run the formatting checks:
+Java files in the OpenSearch codebase are formatted with the Eclipse JDT formatter, using the [Spotless Gradle](https://github.com/diffplug/spotless/tree/master/plugin-gradle) plugin. This plugin is configured on a project-by-project basis, via `build.gradle.kts`. So long as at least one project is configured, the formatting check can be run explicitly with:
-```
-./gradlew checkstyleMain checkstyleTest
-```
+ ./gradlew spotlessJavaCheck
+
+The code can be formatted with:
+
+ ./gradlew spotlessApply
+
+These tasks can also be run for specific subprojects, e.g.
+
+ ./gradlew :java-client:spotlessJavaCheck
+ ./gradlew :samples:spotlessJavaCheck
+
+Please follow these formatting guidelines:
+
+* Java indent is 4 spaces
+* Line width is 140 characters
+* Lines of code surrounded by `// tag::NAME` and `// end::NAME` comments are included in the documentation and should only be 76 characters wide not counting leading indentation. Such regions of code are not formatted automatically as it is not possible to change the line length rule of the formatter for part of a file. Please format such sections sympathetically with the rest of the code, while keeping lines to maximum length of 76 characters.
+* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail.
+* If *absolutely* necessary, you can disable formatting for regions of code with the `// tag::NAME` and `// end::NAME` directives, but note that these are intended for use in documentation, so please make it clear what you have done, and only do this where the benefit clearly outweighs the decrease in consistency.
+* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are.
+* There is an implicit rule that negative boolean expressions should use the form `foo == false` instead of `!foo` for better readability of the code. While this isn't strictly enforced, if might get called out in PR reviews as something to change.
## Submitting Changes
diff --git a/README.md b/README.md
index 06ec9c85cd..755e3d13cd 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,3 @@
-[![Checkstyle](https://github.com/opensearch-project/opensearch-java/actions/workflows/checkstyle.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/checkstyle.yml)
[![Build](https://github.com/opensearch-project/opensearch-java/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/build.yml)
[![Integration Tests](https://github.com/opensearch-project/opensearch-java/actions/workflows/test-integration.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/test-integration.yml)
![Maven Central](https://img.shields.io/maven-central/v/org.opensearch.client/opensearch-java)
@@ -14,6 +13,7 @@ OpenSearch Java Client
- [Project Resources](#project-resources)
- [Code of Conduct](#code-of-conduct)
- [User Guide](#user-guide)
+- [Snapshot Builds](#snapshot-builds)
- [Compatibility with OpenSearch](#compatibility-with-opensearch)
- [Security](#security)
- [License](#license)
diff --git a/build.gradle.kts b/build.gradle.kts
index df7f0dc13b..7779a42fd3 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -42,8 +42,6 @@ allprojects {
mavenCentral()
maven(url = "https://plugins.gradle.org/m2/")
}
-
- apply(plugin = "checkstyle")
}
// Find git information.
diff --git a/buildSrc/formatterConfig.xml b/buildSrc/formatterConfig.xml
new file mode 100644
index 0000000000..425f9acfea
--- /dev/null
+++ b/buildSrc/formatterConfig.xml
@@ -0,0 +1,362 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml
deleted file mode 100644
index 7739883f14..0000000000
--- a/config/checkstyle/checkstyle.xml
+++ /dev/null
@@ -1,156 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/config/checkstyle/checkstyle_suppressions.xml b/config/checkstyle/checkstyle_suppressions.xml
deleted file mode 100644
index edfa8870aa..0000000000
--- a/config/checkstyle/checkstyle_suppressions.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/config/checkstyle/header.java.txt b/config/checkstyle/header.java.txt
deleted file mode 100644
index b5b39ea4ed..0000000000
--- a/config/checkstyle/header.java.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-/*
- * SPDX-License-Identifier: Apache-2.0
- *
- * The OpenSearch Contributors require contributions made to
- * this file be licensed under the Apache-2.0 license or a
- * compatible open source license.
- */
diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts
index 7d2946497f..b357788591 100644
--- a/java-client/build.gradle.kts
+++ b/java-client/build.gradle.kts
@@ -49,10 +49,10 @@ buildscript {
plugins {
java
`java-library`
- checkstyle
`maven-publish`
id("com.github.jk1.dependency-license-report") version "2.5"
id("org.owasp.dependencycheck") version "8.4.0"
+ id("com.diffplug.spotless") version "6.22.0"
}
apply(plugin = "opensearch.repositories")
apply(plugin = "org.owasp.dependencycheck")
@@ -63,10 +63,6 @@ configurations {
}
}
-checkstyle {
- toolVersion = "10.12.3"
-}
-
java {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
@@ -116,6 +112,10 @@ tasks.withType {
}
}
+tasks.build {
+ dependsOn("spotlessJavaCheck")
+}
+
tasks.test {
systemProperty("tests.security.manager", "false")
@@ -269,6 +269,22 @@ tasks.withType {
}
}
+spotless {
+ java {
+
+ target("**/json/**/*.java", "**/transport/**/*.java", "**/util/**/*.java")
+
+ // Use the default importOrder configuration
+ importOrder()
+ removeUnusedImports()
+
+ eclipse().configFile("../buildSrc/formatterConfig.xml")
+
+ trimTrailingWhitespace()
+ endWithNewline()
+ }
+}
+
publishing {
repositories{
if (version.toString().endsWith("SNAPSHOT")) {
diff --git a/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java b/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java
index 92eb9d0b8f..baf834324a 100644
--- a/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java
+++ b/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java
@@ -72,7 +72,7 @@ public boolean ignoreUnknownFields() {
@SuppressWarnings("unchecked")
public T attribute(String name) {
if (this.name.equals(name)) {
- return (T)this.value;
+ return (T) this.value;
} else {
return mapper.attribute(name);
}
diff --git a/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java
index 677fb94098..aeb22671c2 100644
--- a/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java
+++ b/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java
@@ -33,7 +33,6 @@
package org.opensearch.client.json;
import jakarta.json.stream.JsonParser;
-
import java.util.function.Function;
/**
diff --git a/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java
index 84e96ef5dd..1cf864be7b 100644
--- a/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java
+++ b/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java
@@ -33,7 +33,6 @@
package org.opensearch.client.json;
import jakarta.json.stream.JsonParser;
-
import java.util.EnumSet;
public abstract class DelegatingDeserializer implements JsonpDeserializer {
@@ -68,7 +67,7 @@ public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event eve
*/
public static JsonpDeserializer> unwrap(JsonpDeserializer> deserializer) {
while (deserializer instanceof DelegatingDeserializer) {
- deserializer = ((DelegatingDeserializer,?>) deserializer).unwrap();
+ deserializer = ((DelegatingDeserializer, ?>) deserializer).unwrap();
}
return deserializer;
}
diff --git a/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java b/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java
index b34606e20a..c1b5b5c303 100644
--- a/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java
+++ b/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java
@@ -37,7 +37,6 @@
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonLocation;
import jakarta.json.stream.JsonParser;
-
import java.math.BigDecimal;
import java.util.Map;
import java.util.stream.Stream;
@@ -134,4 +133,4 @@ public void skipObject() {
public void close() {
parser.close();
}
-}
\ No newline at end of file
+}
diff --git a/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java b/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java
index 94627156e8..2878c7e127 100644
--- a/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java
+++ b/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java
@@ -32,13 +32,11 @@
package org.opensearch.client.json;
-import org.opensearch.client.util.TaggedUnion;
-
+import static jakarta.json.stream.JsonParser.Event;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParsingException;
-
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
@@ -46,10 +44,8 @@
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
-
import javax.annotation.Nullable;
-
-import static jakarta.json.stream.JsonParser.Event;
+import org.opensearch.client.util.TaggedUnion;
/**
* Utilities for union types whose discriminant is not directly part of the structure, either as an enclosing property name or as
@@ -71,15 +67,17 @@ public static class Deserializer, Member> {
@Nullable
private final BiFunction unKnownUnionCtor;
- public Deserializer(Map> deserializers,
- Function unionCtor) {
+ public Deserializer(Map> deserializers, Function unionCtor) {
this.deserializers = deserializers;
this.unionCtor = unionCtor;
this.unKnownUnionCtor = null;
}
-
- public Deserializer(Map> deserializers,
- Function unionCtor, BiFunction unKnownUnionCtor) {
+
+ public Deserializer(
+ Map> deserializers,
+ Function unionCtor,
+ BiFunction unKnownUnionCtor
+ ) {
this.deserializers = deserializers;
this.unionCtor = unionCtor;
this.unKnownUnionCtor = unKnownUnionCtor;
@@ -127,9 +125,9 @@ public void deserializeEntry(String key, JsonParser parser, JsonpMapper mapper,
int hashPos = key.indexOf('#');
if (hashPos == -1) {
throw new JsonParsingException(
- "Property name '" + key
- + "' is not in the 'type#name' format. Make sure the request has 'typed_keys' set.",
- parser.getLocation());
+ "Property name '" + key + "' is not in the 'type#name' format. Make sure the request has 'typed_keys' set.",
+ parser.getLocation()
+ );
}
String type = key.substring(0, hashPos);
@@ -138,43 +136,44 @@ public void deserializeEntry(String key, JsonParser parser, JsonpMapper mapper,
targetMap.put(name, deserializer.deserialize(type, parser, mapper, parser.next()));
}
}
-
+
public static > JsonpDeserializer