Skip to content

Commit 8da79f6

Browse files
committed
Add commons-lang to JDK migration via OpenRewrite and fix metadata validation
1 parent e00c3e4 commit 8da79f6

4 files changed

Lines changed: 88 additions & 1 deletion

File tree

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>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ recipeList:
239239
newFullyQualifiedTypeName: org.apache.commons.text.WordUtils
240240
---
241241
type: specs.openrewrite.org/v1beta/recipe
242+
name: io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi
243+
displayName: Migrate commons-lang usage to JDK API
244+
description: Replace commons-lang utility methods with modern JDK API equivalents using OpenRewrite.
245+
tags: ['dependencies', 'migration']
246+
recipeList:
247+
# Replace StringUtils methods with JDK equivalents
248+
- org.openrewrite.apache.commons.lang.ApacheCommonsStringUtilsRecipes
249+
# Replace isEmpty/isNotEmpty with JDK equivalents
250+
- org.openrewrite.apache.commons.lang.IsNotEmptyToJdk
251+
# Clean up unused imports after migration
252+
- org.openrewrite.java.RemoveUnusedImports
253+
---
254+
type: specs.openrewrite.org/v1beta/recipe
242255
name: io.jenkins.tools.pluginmodernizer.MigrateToJenkinsBaseLineProperty
243256
displayName: Migrate pom to using jenkins.baseline property if bom is present
244257
description: Migrate pom to using jenkins.baseline property if bom is present.

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,6 +3504,75 @@ public String getHtml() {
35043504
"""));
35053505
}
35063506

3507+
@Test
3508+
void migrateCommonsLangToJdkApi() {
3509+
rewriteRun(
3510+
spec -> {
3511+
var parser = JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true);
3512+
3513+
collectRewriteTestDependencies().forEach(parser::addClasspathEntry);
3514+
3515+
spec.recipeFromResource(
3516+
"/META-INF/rewrite/recipes.yml",
3517+
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLangToJdkApi")
3518+
.parser(parser);
3519+
},
3520+
// language=java
3521+
java("""
3522+
package org.apache.commons.lang3;
3523+
public class StringUtils {
3524+
public static String defaultString(String str) {
3525+
return str == null ? "" : str;
3526+
}
3527+
public static String defaultString(String str, String defaultStr) {
3528+
return str == null ? defaultStr : str;
3529+
}
3530+
public static boolean isEmpty(CharSequence cs) {
3531+
return cs == null || cs.length() == 0;
3532+
}
3533+
public static boolean isNotEmpty(CharSequence cs) {
3534+
return cs != null && cs.length() > 0;
3535+
}
3536+
public static String trim(String str) {
3537+
return str == null ? null : str.trim();
3538+
}
3539+
public static String strip(String str) {
3540+
return str == null ? null : str.strip();
3541+
}
3542+
public static String upperCase(String str) {
3543+
return str == null ? null : str.toUpperCase();
3544+
}
3545+
public static String lowerCase(String str) {
3546+
return str == null ? null : str.toLowerCase();
3547+
}
3548+
}
3549+
"""),
3550+
// language=java
3551+
java("""
3552+
import org.apache.commons.lang3.StringUtils;
3553+
3554+
class MyComponent {
3555+
public String getDefault(String input) {
3556+
return StringUtils.defaultString(input);
3557+
}
3558+
public String getDefaultWithFallback(String input) {
3559+
return StringUtils.defaultString(input, "N/A");
3560+
}
3561+
}
3562+
""", """
3563+
import java.util.Objects;
3564+
3565+
class MyComponent {
3566+
public String getDefault(String input) {
3567+
return Objects.toString(input, "");
3568+
}
3569+
public String getDefaultWithFallback(String input) {
3570+
return Objects.toString(input, "N/A");
3571+
}
3572+
}
3573+
"""));
3574+
}
3575+
35073576
@Test
35083577
void migrateToJUnit5() {
35093578
rewriteRun(

scripts/validate_metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
# migration IDs
5050
valid_migration_ids = [
51-
"io.jenkins.tools.pluginmodernizer.FetchMetadata,"
51+
"io.jenkins.tools.pluginmodernizer.FetchMetadata",
5252
"io.jenkins.tools.pluginmodernizer.MergeGitIgnoreRecipe",
5353
"io.jenkins.tools.pluginmodernizer.UpdateScmUrl",
5454
"io.jenkins.tools.pluginmodernizer.SetupJenkinsfile",
@@ -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)