Skip to content

Commit 95236f3

Browse files
authored
Merge pull request #1594 from s4chinjha/feature/commons-lang-to-jdk
Add recipe to migrate commons-lang usage to JDK APIs
2 parents 36e4d01 + 5946d07 commit 95236f3

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

plugin-modernizer-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@
117117
<groupId>org.openrewrite</groupId>
118118
<artifactId>rewrite-xml</artifactId>
119119
</dependency>
120+
<dependency>
121+
<groupId>org.openrewrite.recipe</groupId>
122+
<artifactId>rewrite-apache</artifactId>
123+
</dependency>
120124
<dependency>
121125
<groupId>org.openrewrite.recipe</groupId>
122126
<artifactId>rewrite-jenkins</artifactId>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This PR migrates commons-lang usage to standard JDK APIs.
2+
3+
It replaces commonly used Apache Commons Lang utility methods with equivalent methods from the Java standard library.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Migrate commons-lang usage to JDK APIs

plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ recipeList:
249249
- io.jenkins.tools.pluginmodernizer.core.recipes.MigrateToJenkinsBaseLineProperty
250250
---
251251
type: specs.openrewrite.org/v1beta/recipe
252+
name: io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi
253+
displayName: Migrate commons-lang usage to JDK APIs
254+
description: Replace commons-lang utility methods with modern JDK API equivalents using OpenRewrite.
255+
tags: ['dependencies', 'migration']
256+
recipeList:
257+
- org.openrewrite.apache.commons.lang.ApacheCommonsStringUtilsRecipes
258+
- org.openrewrite.apache.commons.lang.IsNotEmptyToJdk
259+
- org.openrewrite.java.RemoveUnusedImports
260+
---
261+
type: specs.openrewrite.org/v1beta/recipe
252262
name: io.jenkins.tools.pluginmodernizer.AddCodeOwner
253263
displayName: Add CODEOWNERS file
254264
description: Adds a CODEOWNERS file to a Jenkins plugin.

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,6 +3506,94 @@ public String getHtml() {
35063506
"""));
35073507
}
35083508

3509+
@Test
3510+
void migrateCommonsLangToJdkApi() {
3511+
rewriteRun(
3512+
spec -> {
3513+
var parser = JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true);
3514+
3515+
collectRewriteTestDependencies().forEach(parser::addClasspathEntry);
3516+
3517+
spec.recipeFromResource(
3518+
"/META-INF/rewrite/recipes.yml",
3519+
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi")
3520+
.parser(parser);
3521+
},
3522+
// Stub
3523+
java("""
3524+
package org.apache.commons.lang3;
3525+
public class StringUtils {
3526+
public static String defaultString(String str) {
3527+
return str == null ? "" : str;
3528+
}
3529+
public static String defaultString(String str, String defaultStr) {
3530+
return str == null ? defaultStr : str;
3531+
}
3532+
public static boolean isNotEmpty(String str) {
3533+
return str != null && !str.isEmpty();
3534+
}
3535+
}
3536+
"""),
3537+
// Input → Output
3538+
java("""
3539+
import org.apache.commons.lang3.StringUtils;
3540+
3541+
class MyComponent {
3542+
public String getDefault(String input) {
3543+
return StringUtils.defaultString(input);
3544+
}
3545+
public String getDefaultWithFallback(String input) {
3546+
return StringUtils.defaultString(input, "N/A");
3547+
}
3548+
}
3549+
""", """
3550+
import java.util.Objects;
3551+
3552+
class MyComponent {
3553+
public String getDefault(String input) {
3554+
return Objects.toString(input, "");
3555+
}
3556+
public String getDefaultWithFallback(String input) {
3557+
return Objects.toString(input, "N/A");
3558+
}
3559+
}
3560+
"""),
3561+
java("""
3562+
import org.apache.commons.lang3.StringUtils;
3563+
3564+
class Test {
3565+
boolean check(String str) {
3566+
return StringUtils.isNotEmpty(str);
3567+
}
3568+
}
3569+
""", """
3570+
class Test {
3571+
boolean check(String str) {
3572+
return str != null && !str.isEmpty();
3573+
}
3574+
}
3575+
"""));
3576+
}
3577+
3578+
@Test
3579+
void noChangeWhenNoCommonsLang() {
3580+
rewriteRun(
3581+
spec -> {
3582+
var parser = JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true);
3583+
collectRewriteTestDependencies().forEach(parser::addClasspathEntry);
3584+
3585+
spec.recipeFromResource(
3586+
"/META-INF/rewrite/recipes.yml",
3587+
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi")
3588+
.parser(parser);
3589+
},
3590+
java("""
3591+
class Test {
3592+
String s = "hello";
3593+
}
3594+
"""));
3595+
}
3596+
35093597
@Test
35103598
void migrateToJUnit5() {
35113599
rewriteRun(

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/TemplateUtilsTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.jenkins.tools.pluginmodernizer.core.utils;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56
import static org.mockito.Mockito.doReturn;
67
import static org.mockito.Mockito.mock;
@@ -855,6 +856,46 @@ public void testFriendlyPrBodyMigrateCommonsLang2ToLang3AndCommonText() {
855856
"Description");
856857
}
857858

859+
@Test
860+
public void testFriendlyPrTitleMigrateCommonsLangToJdkApi() {
861+
862+
// Mocks
863+
Plugin plugin = mock(Plugin.class);
864+
Recipe recipe = mock(Recipe.class);
865+
866+
doReturn("io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi")
867+
.when(recipe)
868+
.getName();
869+
870+
// Test
871+
String result = TemplateUtils.renderPullRequestTitle(plugin, recipe);
872+
873+
// Assert
874+
assertNotNull(result);
875+
assertTrue(result.contains("commons-lang"));
876+
assertEquals("Migrate commons-lang usage to JDK APIs", result);
877+
}
878+
879+
@Test
880+
public void testFriendlyPrBodyMigrateCommonsLangToJdkApi() {
881+
882+
// Mocks
883+
Plugin plugin = mock(Plugin.class);
884+
Recipe recipe = mock(Recipe.class);
885+
886+
doReturn("io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi")
887+
.when(recipe)
888+
.getName();
889+
890+
// Test
891+
String result = TemplateUtils.renderPullRequestBody(plugin, recipe);
892+
893+
// Assert
894+
assertNotNull(result);
895+
assertTrue(result.contains("commons-lang"));
896+
assertTrue(result.contains("standard JDK APIs"));
897+
}
898+
858899
@Test
859900
public void testFriendlyPrTitleMigrateJavaxAnnotationsToSpotbugs() {
860901

scripts/validate_metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"io.jenkins.tools.pluginmodernizer.EnsureIndexJelly",
9595
"io.jenkins.tools.pluginmodernizer.MigrateTomakehurstToWiremock",
9696
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLang2ToLang3AndCommonText",
97+
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi",
9798
"io.jenkins.tools.pluginmodernizer.RemoveOldJavaVersionForModernJenkins",
9899
"io.jenkins.tools.pluginmodernizer.SwitchToRenovate",
99100
"io.jenkins.tools.pluginmodernizer.JavaxAnnotationsToSpotbugs",

0 commit comments

Comments
 (0)