Skip to content

Commit 57dc5cf

Browse files
authored
Migrating from checkstyle to spotless for formatting (opensearch-project#648)
* Adding spotless for checking and fixing formatting and removing checkstyle Signed-off-by: Vacha Shah <[email protected]> * Making build task depend on spotlessJavaCheck Signed-off-by: Vacha Shah <[email protected]> * Removing resources and documentation related to checkstyle Signed-off-by: Vacha Shah <[email protected]> * Running spotless for json, transport and util folders Signed-off-by: Vacha Shah <[email protected]> * Running spotless for samples Signed-off-by: Vacha Shah <[email protected]> * Removing commented code Signed-off-by: Vacha Shah <[email protected]> --------- Signed-off-by: Vacha Shah <[email protected]>
1 parent aea607a commit 57dc5cf

File tree

92 files changed

+1523
-1596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1523
-1596
lines changed

.github/workflows/checkstyle.yml

-24
This file was deleted.

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4242
### Dependencies
4343

4444
### Changed
45+
- Migrated from checkstyle to spotless ([#648](https://github.com/opensearch-project/opensearch-java/pull/648))
4546

4647
### Deprecated
4748

DEVELOPER_GUIDE.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,28 @@ Follow links in the [Java Tutorial](https://code.visualstudio.com/docs/java/java
9797

9898
## Java Language Formatting Guidelines
9999

100-
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:
100+
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:
101101

102-
```
103-
./gradlew checkstyleMain checkstyleTest
104-
```
102+
./gradlew spotlessJavaCheck
103+
104+
The code can be formatted with:
105+
106+
./gradlew spotlessApply
107+
108+
These tasks can also be run for specific subprojects, e.g.
109+
110+
./gradlew :java-client:spotlessJavaCheck
111+
./gradlew :samples:spotlessJavaCheck
112+
113+
Please follow these formatting guidelines:
114+
115+
* Java indent is 4 spaces
116+
* Line width is 140 characters
117+
* 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.
118+
* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail.
119+
* 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.
120+
* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are.
121+
* 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.
105122

106123
## Submitting Changes
107124

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
[![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)
21
[![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)
32
[![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)
43
![Maven Central](https://img.shields.io/maven-central/v/org.opensearch.client/opensearch-java)
@@ -14,6 +13,7 @@ OpenSearch Java Client
1413
- [Project Resources](#project-resources)
1514
- [Code of Conduct](#code-of-conduct)
1615
- [User Guide](#user-guide)
16+
- [Snapshot Builds](#snapshot-builds)
1717
- [Compatibility with OpenSearch](#compatibility-with-opensearch)
1818
- [Security](#security)
1919
- [License](#license)

build.gradle.kts

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ allprojects {
4242
mavenCentral()
4343
maven(url = "https://plugins.gradle.org/m2/")
4444
}
45-
46-
apply(plugin = "checkstyle")
4745
}
4846

4947
// Find git information.

buildSrc/formatterConfig.xml

+362
Large diffs are not rendered by default.

config/checkstyle/checkstyle.xml

-156
This file was deleted.

config/checkstyle/checkstyle_suppressions.xml

-44
This file was deleted.

config/checkstyle/header.java.txt

-7
This file was deleted.

java-client/build.gradle.kts

+21-5
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ buildscript {
4949
plugins {
5050
java
5151
`java-library`
52-
checkstyle
5352
`maven-publish`
5453
id("com.github.jk1.dependency-license-report") version "2.5"
5554
id("org.owasp.dependencycheck") version "8.4.0"
55+
id("com.diffplug.spotless") version "6.22.0"
5656
}
5757
apply(plugin = "opensearch.repositories")
5858
apply(plugin = "org.owasp.dependencycheck")
@@ -63,10 +63,6 @@ configurations {
6363
}
6464
}
6565

66-
checkstyle {
67-
toolVersion = "10.12.3"
68-
}
69-
7066
java {
7167
targetCompatibility = JavaVersion.VERSION_11
7268
sourceCompatibility = JavaVersion.VERSION_11
@@ -116,6 +112,10 @@ tasks.withType<Jar> {
116112
}
117113
}
118114

115+
tasks.build {
116+
dependsOn("spotlessJavaCheck")
117+
}
118+
119119
tasks.test {
120120
systemProperty("tests.security.manager", "false")
121121

@@ -269,6 +269,22 @@ tasks.withType<Jar> {
269269
}
270270
}
271271

272+
spotless {
273+
java {
274+
275+
target("**/json/**/*.java", "**/transport/**/*.java", "**/util/**/*.java")
276+
277+
// Use the default importOrder configuration
278+
importOrder()
279+
removeUnusedImports()
280+
281+
eclipse().configFile("../buildSrc/formatterConfig.xml")
282+
283+
trimTrailingWhitespace()
284+
endWithNewline()
285+
}
286+
}
287+
272288
publishing {
273289
repositories{
274290
if (version.toString().endsWith("SNAPSHOT")) {

java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public boolean ignoreUnknownFields() {
7272
@SuppressWarnings("unchecked")
7373
public <T> T attribute(String name) {
7474
if (this.name.equals(name)) {
75-
return (T)this.value;
75+
return (T) this.value;
7676
} else {
7777
return mapper.attribute(name);
7878
}

java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
package org.opensearch.client.json;
3434

3535
import jakarta.json.stream.JsonParser;
36-
3736
import java.util.function.Function;
3837

3938
/**

java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
package org.opensearch.client.json;
3434

3535
import jakarta.json.stream.JsonParser;
36-
3736
import java.util.EnumSet;
3837

3938
public abstract class DelegatingDeserializer<T, U> implements JsonpDeserializer<T> {
@@ -68,7 +67,7 @@ public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event eve
6867
*/
6968
public static JsonpDeserializer<?> unwrap(JsonpDeserializer<?> deserializer) {
7069
while (deserializer instanceof DelegatingDeserializer) {
71-
deserializer = ((DelegatingDeserializer<?,?>) deserializer).unwrap();
70+
deserializer = ((DelegatingDeserializer<?, ?>) deserializer).unwrap();
7271
}
7372
return deserializer;
7473
}

0 commit comments

Comments
 (0)