Skip to content

Commit bc07306

Browse files
author
Vincent Potucek
committed
fix removeUnusedPrivateMethods
Signed-off-by: Vincent Potucek <vpotucek@me.com>
1 parent 94c5f21 commit bc07306

File tree

35 files changed

+161
-431
lines changed

35 files changed

+161
-431
lines changed

DEVELOPER_GUIDE.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ Fork [opensearch-project/OpenSearch](https://github.com/opensearch-project/OpenS
7676

7777
#### JDK
7878

79-
OpenSearch recommends building with the [Temurin/Adoptium](https://adoptium.net/temurin/releases/) distribution. JDK 11 is the minimum supported, and JDK-24 is the newest supported. You must have a supported JDK installed with the environment variable `JAVA_HOME` referencing the path to Java home for your JDK installation, e.g. `JAVA_HOME=/usr/lib/jvm/jdk-21`.
79+
OpenSearch recommends building with the [Temurin/Adoptium](https://adoptium.net/temurin/releases/) distribution. JDK 11 is the minimum supported, and JDK-24 is the newest supported. You must have a supported JDK installed with the environment variable `JAVA_HOME` referencing the path to Java home for your JDK installation, e.g. `JAVA_HOME=/usr/lib/jvm/jdk-21`.
8080

81-
Download Java 11 from [here](https://adoptium.net/releases.html?variant=openjdk11).
81+
Download Java 11 from [here](https://adoptium.net/releases.html?variant=openjdk11).
8282

8383

8484
In addition, certain backward compatibility tests check out and compile the previous major version of OpenSearch, and therefore require installing [JDK 11](https://adoptium.net/temurin/releases/?version=11) and [JDK 17](https://adoptium.net/temurin/releases/?version=17) and setting the `JAVA11_HOME` and `JAVA17_HOME` environment variables. More to that, since 8.10 release, Gradle has deprecated the usage of the any JDKs below JDK-16. For smooth development experience, the recommendation is to install at least [JDK 17](https://adoptium.net/temurin/releases/?version=17) or [JDK 21](https://adoptium.net/temurin/releases/?version=21). If you still want to build with JDK-11 only, please add `-Dorg.gradle.warning.mode=none` when invoking any Gradle build task from command line, for example:
@@ -319,17 +319,14 @@ For example, we have an hdfs test that uses mini-hdfs to test our repository-hdf
319319

320320
## Java Language Formatting Guidelines
321321

322-
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` in the root of the repository. So long as at least one project is configured, the formatting check can be run explicitly with:
322+
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` in the root of the repository.
323323

324-
./gradlew spotlessJavaCheck
325-
326-
The code can be formatted with:
327-
328-
./gradlew spotlessApply
329-
330-
These tasks can also be run for specific subprojects, e.g.
331-
332-
./gradlew server:spotlessJavaCheck
324+
- As long at least one project is configured, the formatting check can be run explicitly with:
325+
- `./gradlew spotlessJavaCheck`
326+
- The code can be formatted with:
327+
- `./gradlew spotlessApply`
328+
- These tasks can also be run for specific subprojects, e.g.
329+
- `./gradlew server:spotlessJavaCheck`
333330

334331
Please follow these formatting guidelines:
335332

@@ -341,6 +338,43 @@ Please follow these formatting guidelines:
341338
* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are.
342339
* 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, it might get called out in PR reviews as something to change.
343340

341+
## 🚀 Code Transformation & Automated Rewriting
342+
*(Advanced Refactoring, Modernization, and Bulk Code Changes)*
343+
344+
### 🔧 Automated Rewriting of Source Code
345+
The OpenSearch build system supports **automated, large-scale code transformations** using [Moderne](https://moderne.io/) and OpenRewrite. These tools enable:
346+
347+
- **Safe Refactoring** (e.g., renaming methods, migrating deprecated APIs)
348+
- **Modernization** (e.g., adopting Java 17 features like `var`, `records`, or `switch` expressions)
349+
- **Anti-Pattern Fixes** (e.g., replacing `Vector` with `ArrayList`, removing redundant `StringBuilder`)
350+
- **Convention Enforcement** (e.g., consistent JUnit test naming, `final` keyword usage)
351+
- **Dependency Upgrades** (e.g., automatic migration when updating library versions)
352+
353+
### 📜 Configuration
354+
Rewrite rules are defined in:
355+
- **[code-convention.gradle](gradle/code-convention.gradle)** – Configures the OpenRewrite plugin and active recipes.
356+
- **[code-convention.yml](gradle/code-convention.yml)** – Custom rule definitions (e.g., project-specific patterns).
357+
358+
### ⚙️ Usage
359+
- **Dry-run (check changes without applying):**
360+
- Full project: `./gradlew rewriteDryRun`
361+
- Subproject (e.g., `server`): `./gradlew server:rewriteDryRun`
362+
- **Apply transformations:**
363+
- Full project: `./gradlew rewriteRun`
364+
- Subproject: `./gradlew server:rewriteRun`
365+
366+
### 🛠️ Example Transformations
367+
| **Before** | **After** | **Rule** |
368+
|--------------------------------|------------------------------------|-----------------------------------|
369+
| `new ArrayList<String>()` | `new ArrayList<>()` | Diamond Operator (Java 7+) |
370+
| `if (x == false)` | `if (!x)` | Boolean Simplification |
371+
| `@Test public void testFoo()` | `@Test void foo()` | JUnit 5 Convention |
372+
373+
### ⚠️ Notes
374+
- **Custom Rules**: Add project-specific rewrites in `code-convention.yml`.
375+
- **Exclusions**: Use `// rewrite:off` and `// rewrite:on` to opt out of transformations for specific code blocks.
376+
- **PR Reviews**: Automated rewrites are flagged in CI. Verify changes match intent before merging.
377+
344378
## Adding Dependencies
345379

346380
When adding a new dependency or removing an existing dependency via any `build.gradle` (that are not in the test scope), update the dependency LICENSE and library SHAs.

build.gradle

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,22 @@
2828
* under the License.
2929
*/
3030

31-
import java.nio.charset.StandardCharsets;
32-
import java.io.ByteArrayOutputStream;
3331

3432
import com.avast.gradle.dockercompose.tasks.ComposePull
3533
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
3634
import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin
3735
import org.apache.tools.ant.taskdefs.condition.Os
36+
import org.gradle.plugins.ide.eclipse.model.AccessRule
37+
import org.gradle.plugins.ide.eclipse.model.SourceFolder
3838
import org.opensearch.gradle.BuildPlugin
39+
import org.opensearch.gradle.CheckCompatibilityTask
3940
import org.opensearch.gradle.Version
4041
import org.opensearch.gradle.VersionProperties
4142
import org.opensearch.gradle.info.BuildParams
4243
import org.opensearch.gradle.plugin.PluginBuildPlugin
4344
import org.opensearch.gradle.tar.SymbolicLinkPreservingTar
44-
import org.gradle.plugins.ide.eclipse.model.AccessRule
45-
import org.gradle.plugins.ide.eclipse.model.EclipseJdt
46-
import org.gradle.plugins.ide.eclipse.model.SourceFolder
47-
import org.gradle.api.Project;
48-
import org.gradle.process.ExecResult;
49-
import org.opensearch.gradle.CheckCompatibilityTask
45+
46+
import java.nio.charset.StandardCharsets
5047

5148
import static org.opensearch.gradle.util.GradleUtils.maybeConfigure
5249

@@ -56,6 +53,7 @@ plugins {
5653
id 'opensearch.global-build-info'
5754
id "com.diffplug.spotless" version "6.25.0" apply false
5855
id "org.gradle.test-retry" version "1.6.2" apply false
56+
id "org.openrewrite.rewrite" version "7.11.0" apply false
5957
id "test-report-aggregation"
6058
id 'jacoco-report-aggregation'
6159
}
@@ -69,14 +67,39 @@ apply from: 'gradle/local-distribution.gradle'
6967
apply from: 'gradle/run.gradle'
7068
apply from: 'gradle/missing-javadoc.gradle'
7169
apply from: 'gradle/code-coverage.gradle'
70+
apply from: 'gradle/code-convention.gradle'
7271

7372
// common maven publishing configuration
7473
allprojects {
7574
group = 'org.opensearch'
7675
version = VersionProperties.getOpenSearch()
7776
description = "OpenSearch subproject ${project.path}"
7877
}
79-
78+
allprojects {
79+
configurations.configureEach {
80+
resolutionStrategy {
81+
force(
82+
'org.jetbrains:annotations:26.0.2',
83+
'org.apache.commons:commons-text:1.13.1',
84+
'org.ow2.asm:asm:9.8',
85+
'org.ow2.asm:asm-util:9.8',
86+
)
87+
eachDependency { details ->
88+
if (details.requested.group.startsWith('com.fasterxml.jackson')) {
89+
details.useVersion '2.17.3'
90+
}
91+
if (details.requested.group == 'org.openrewrite.recipe' &&
92+
details.requested.name == 'rewrite-java-dependencies') {
93+
details.useVersion '1.37.0'
94+
}
95+
if (details.requested.group == 'com.google.errorprone' &&
96+
details.requested.name == 'error_prone_annotations') {
97+
details.useVersion '2.36.0'
98+
}
99+
}
100+
}
101+
}
102+
}
80103
configure(allprojects - project(':distribution:archives:integ-test-zip')) {
81104
project.pluginManager.withPlugin('nebula.maven-base-publish') {
82105
if (project.pluginManager.hasPlugin('opensearch.build') == false) {

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
org.gradle.caching=true
1414
org.gradle.warning.mode=none
1515
org.gradle.parallel=true
16+
# https://github.com/openrewrite/rewrite-gradle-plugin/issues/212
17+
org.gradle.workers.max=8
1618
org.gradle.jvmargs=-Xmx3g -XX:+HeapDumpOnOutOfMemoryError -Xss2m \
1719
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
1820
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \

gradle/code-convention.gradle

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
/*
10+
* Licensed to Elasticsearch under one or more contributor
11+
* license agreements. See the NOTICE file distributed with
12+
* this work for additional information regarding copyright
13+
* ownership. Elasticsearch licenses this file to you under
14+
* the Apache License, Version 2.0 (the "License"); you may
15+
* not use this file except in compliance with the License.
16+
* You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing,
21+
* software distributed under the License is distributed on an
22+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23+
* KIND, either express or implied. See the License for the
24+
* specific language governing permissions and limitations
25+
* under the License.
26+
*/
27+
28+
import org.opensearch.gradle.BuildPlugin
29+
30+
import static java.lang.System.getenv
31+
import static java.util.Objects.isNull
32+
33+
allprojects {
34+
plugins.withType(BuildPlugin).whenPluginAdded {
35+
project.apply plugin: "org.openrewrite.rewrite"
36+
rewrite {
37+
activeRecipe("org.opensearch.openrewrite.recipe.CodeCleanup")
38+
configFile = file("$rootDir/gradle/code-convention.yml")
39+
exclusions.add("**/simple-bulk11.json")
40+
exclusions.add("**/simple-msearch5.json")
41+
exclusions.add("**AbstractBenchmark.java")
42+
exclusions.add("**OpenSearchTestCaseTests.java")
43+
exclusions.add("**ScriptClassPathResolutionContext.java")
44+
exclusions.add("**SearchAfterIT.java")
45+
exclusions.add("**StarTreeMapper.java")
46+
failOnDryRunResults = true
47+
}
48+
repositories {
49+
mavenCentral()
50+
}
51+
dependencies {
52+
rewrite("org.openrewrite.recipe:rewrite-rewrite:0.9.0")
53+
rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.12.0")
54+
}
55+
tasks {
56+
if (!getenv("codeCleanupSkip")) { // skip on release
57+
check.dependsOn(rewriteDryRun)
58+
check.dependsOn(spotlessJavaCheck)
59+
}
60+
if (isNull(getenv("JENKINS_URL")) && getenv("codeCleanup")) { // optional for dev
61+
assemble.dependsOn(rewriteRun)
62+
assemble.dependsOn(spotlessApply)
63+
}
64+
}
65+
}
66+
}

gradle/code-convention.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type: specs.openrewrite.org/v1beta/recipe
2+
name: org.opensearch.openrewrite.recipe.CodeCleanup
3+
displayName: CodeCleanup
4+
description: Automatically cleanup code, e.g. remove unnecessary parentheses, simplify expressions.
5+
recipeList:
6+
# - org.openrewrite.java.RemoveUnusedImports
7+
# - org.openrewrite.staticanalysis.RemoveUnusedLocalVariables
8+
# - org.openrewrite.staticanalysis.RemoveUnusedPrivateFields
9+
- org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods
10+
# - org.openrewrite.java.OrderImports
11+
# - org.openrewrite.java.format.RemoveTrailingWhitespace
12+
# - org.openrewrite.staticanalysis.EmptyBlock
13+
# - org.openrewrite.staticanalysis.RemoveCallsToSystemGc
14+
# - org.openrewrite.staticanalysis.UnnecessaryThrows
15+
# - org.openrewrite.text.EndOfLineAtEndOfFile

libs/task-commons/src/main/java/org/opensearch/task/commons/clients/TaskListRequest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ public TaskListRequest taskType(TaskStatus... taskStatus) {
7171
return this;
7272
}
7373

74-
/**
75-
* Update worker node to filter with in the request
76-
* @param workerNode WorkerNode
77-
* @return ListTaskRequest
78-
*/
79-
private TaskListRequest workerNode(WorkerNode workerNode) {
80-
this.workerNodes = workerNode;
81-
return this;
82-
}
83-
8474
/**
8575
* Update page number to start with when fetching the list of tasks
8676
* @param startPageNumber startPageNumber

modules/cache-common/src/test/java/org/opensearch/cache/common/tier/TieredSpilloverCacheTests.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,17 +2600,6 @@ private TieredSpilloverCache<String, String> getTieredSpilloverCache(
26002600
return builder.build();
26012601
}
26022602

2603-
private TieredSpilloverCache<String, String> initializeTieredSpilloverCache(
2604-
int keyValueSize,
2605-
int diskCacheSize,
2606-
RemovalListener<ICacheKey<String>, String> removalListener,
2607-
Settings settings,
2608-
long diskDeliberateDelay
2609-
2610-
) {
2611-
return initializeTieredSpilloverCache(keyValueSize, diskCacheSize, removalListener, settings, diskDeliberateDelay, null, 256);
2612-
}
2613-
26142603
private TieredSpilloverCache<String, String> initializeTieredSpilloverCache(
26152604
int keyValueSize,
26162605
int diskCacheSize,

plugins/arrow-flight-rpc/src/main/java/org/opensearch/arrow/flight/bootstrap/FlightClientManager.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.Map;
3636
import java.util.Objects;
3737
import java.util.Optional;
38-
import java.util.Set;
3938
import java.util.concurrent.CompletableFuture;
4039
import java.util.concurrent.ConcurrentHashMap;
4140
import java.util.concurrent.ExecutorService;
@@ -247,10 +246,6 @@ private static boolean isValidNode(DiscoveryNode node) {
247246
return node != null && !node.getVersion().before(MIN_SUPPORTED_VERSION) && FeatureFlags.isEnabled(ARROW_STREAMS_SETTING);
248247
}
249248

250-
private Set<String> getCurrentClusterNodes() {
251-
return Objects.requireNonNull(clientConfig.clusterService).state().nodes().getNodes().keySet();
252-
}
253-
254249
@VisibleForTesting
255250
Map<String, ClientHolder> getFlightClients() {
256251
return flightClients;

plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,35 +1264,6 @@ PersistentCacheManager buildCacheManager() {
12641264
}
12651265
}
12661266

1267-
private EhcacheDiskCache.Builder<String, String> createDummyBuilder(String storagePath) throws IOException {
1268-
Settings settings = Settings.builder().build();
1269-
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
1270-
ToLongBiFunction<ICacheKey<String>, String> weigher = getWeigher();
1271-
try (NodeEnvironment env = newNodeEnvironment(settings)) {
1272-
if (storagePath == null || storagePath.isBlank()) {
1273-
storagePath = env.nodePaths()[0].path.toString() + "/request_cache";
1274-
}
1275-
return (EhcacheDiskCache.Builder<String, String>) new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias(
1276-
"ehcacheTest"
1277-
)
1278-
.setIsEventListenerModeSync(true)
1279-
.setStoragePath(storagePath)
1280-
.setKeyType(String.class)
1281-
.setValueType(String.class)
1282-
.setKeySerializer(new StringSerializer())
1283-
.setDiskCacheAlias("test1")
1284-
.setValueSerializer(new StringSerializer())
1285-
.setDimensionNames(List.of(dimensionName))
1286-
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
1287-
.setSettings(settings)
1288-
.setExpireAfterAccess(TimeValue.MAX_VALUE)
1289-
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
1290-
.setRemovalListener(removalListener)
1291-
.setWeigher(weigher)
1292-
.setStatsTrackingEnabled(false);
1293-
}
1294-
}
1295-
12961267
private List<String> getRandomDimensions(List<String> dimensionNames) {
12971268
Random rand = Randomness.get();
12981269
int bound = 3;

server/src/internalClusterTest/java/org/opensearch/cluster/routing/allocation/decider/DiskThresholdDeciderIT.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,6 @@ public void testDiskMonitorResetLastRuntimeMilliSecOnlyInFirstCall() throws Exce
503503
}, 30L, TimeUnit.SECONDS);
504504
}
505505

506-
private String populateNode(final String dataNodeName) throws Exception {
507-
final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
508-
createAndPopulateIndex(indexName, dataNodeName);
509-
return indexName;
510-
}
511-
512506
private void createIndex(String indexName, String nodeName, boolean isWarmIndex) throws Exception {
513507
final Settings.Builder indexSettingBuilder = Settings.builder()
514508
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)

0 commit comments

Comments
 (0)