Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ group = "org.openrewrite.recipe"
description = "Apache Migration"

recipeDependencies {
parserClasspath("org.apache.httpcomponents.core5:httpcore5:5.1.+")
parserClasspath("org.apache.httpcomponents.client5:httpclient5:5.1.+")
parserClasspath("commons-io:commons-io:2.+")
parserClasspath("org.apache.commons:commons-collections4:4.4")
parserClasspath("org.apache.poi:poi:3.16")
parserClasspath("org.apache.commons:commons-lang3:3.+")

parserClasspath("org.apache.httpcomponents.client5:httpclient5:5.1.+")
parserClasspath("org.apache.httpcomponents.core5:httpcore5:5.1.+")
parserClasspath("org.apache.httpcomponents:httpclient:4.5.14")
parserClasspath("org.apache.httpcomponents:httpmime:4.5.14")
parserClasspath("org.apache.httpcomponents:httpcore-nio:4.4.16")
parserClasspath("org.apache.httpcomponents:httpcore:4.4.16")
parserClasspath("org.apache.httpcomponents:httpmime:4.5.14")

parserClasspath("org.apache.maven.shared:maven-shared-utils:3.+")

parserClasspath("org.apache.poi:poi:3.16")

parserClasspath("org.codehaus.plexus:plexus-utils:3.+")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
Expand All @@ -32,12 +40,13 @@ dependencies {
exclude("io.github.eisop","dataflow-errorprone")
}

implementation("commons-io:commons-io:2.+")
implementation("org.apache.commons:commons-lang3:3.+")
implementation("org.apache.maven.shared:maven-shared-utils:3.+")
implementation("org.codehaus.plexus:plexus-utils:3.+")
compileOnly("commons-io:commons-io:2.+")
compileOnly("org.apache.commons:commons-lang3:3.+")
compileOnly("org.apache.maven.shared:maven-shared-utils:3.+")
compileOnly("org.codehaus.plexus:plexus-utils:3.+")

implementation("org.jspecify:jspecify:1.0.0")

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-test")
testImplementation("org.openrewrite:rewrite-maven")
Expand All @@ -58,3 +67,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.13.3")

}

tasks.withType<JavaCompile> {
options.compilerArgs.add("-Arewrite.javaParserClasspathFrom=resources")
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,33 @@ public Validated validate() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.apache.commons.io.IOUtils", false), new JavaIsoVisitor<ExecutionContext>() {
private final JavaParser.Builder<?, ?> javaParser = JavaParser.fromJavaVersion().classpath("commons-io");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (STRING_GET_BYTES.matches(mi)) {
mi = mi.withSelect(method.getArguments().get(0));
//noinspection ConstantConditions
mi = mi.withMethodType(mi.getMethodType().withName("getBytes"));
mi = JavaTemplate.builder("#{any(String)}.getBytes(StandardCharsets.#{})")
.javaParser(javaParser)
maybeAddImport("java.nio.charset.StandardCharsets");
return JavaTemplate.builder("#{any(String)}.getBytes(StandardCharsets.#{})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "commons-io"))
.imports("java.nio.charset.StandardCharsets")
.build()
.apply(updateCursor(mi),
mi.getCoordinates().replaceMethod(),
mi.getArguments().get(0), encoding == null ? "UTF_8" : encoding);
} else {
for (Map.Entry<MethodMatcher, String> entry : MATCHER_TEMPLATES.entrySet()) {
if (entry.getKey().matches(mi)) {
List<Object> args = new ArrayList<>(mi.getArguments());
args.add(encoding == null ? "UTF_8" : encoding);
mi = JavaTemplate.builder(entry.getValue())
.contextSensitive()
.javaParser(javaParser)
.imports("java.nio.charset.StandardCharsets")
.build()
.apply(updateCursor(mi),
mi.getCoordinates().replaceMethod(), args.toArray());
}
}

for (Map.Entry<MethodMatcher, String> entry : MATCHER_TEMPLATES.entrySet()) {
if (entry.getKey().matches(mi)) {
List<Object> args = new ArrayList<>(mi.getArguments());
args.add(encoding == null ? "UTF_8" : encoding);
mi = JavaTemplate.builder(entry.getValue())
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "commons-io"))
.imports("java.nio.charset.StandardCharsets")
.build()
.apply(updateCursor(mi), mi.getCoordinates().replaceMethod(), args.toArray());
}
}
if (method != mi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.apache.commons.lang;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
Expand Down Expand Up @@ -71,8 +72,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private final MethodMatcher isNotEmptyMatcher = new MethodMatcher("*..StringUtils isNotEmpty(..)");
private final MethodMatcher trimMatcher = new MethodMatcher("java.lang.String trim()");

private final JavaTemplate isEmptyReplacement = Semantics.expression(this, "IsEmpty", (String s) -> (s == null || s.isEmpty())).build();
private final JavaTemplate isNotEmptyReplacement = Semantics.expression(this, "IsNotEmpty", (String s) -> (s != null && !s.isEmpty())).build();
// No need to use context here, as there are no classpath dependencies needed for these replacements
private final JavaTemplate isEmptyReplacement = Semantics.expression(this, "IsEmpty", (@Nullable String s) -> (s == null || s.isEmpty())).build();
private final JavaTemplate isNotEmptyReplacement = Semantics.expression(this, "IsNotEmpty", (@Nullable String s) -> (s != null && !s.isEmpty())).build();
private final JavaTemplate isEmptyTrimmed = Semantics.expression(this, "IsEmptyTrimmed", (String s) -> s.trim().isEmpty()).build();
private final JavaTemplate isNotEmptyTrimmed = Semantics.expression(this, "IsNotEmptyTrimmed", (String s) -> !s.trim().isEmpty()).build();

Expand Down
Binary file modified src/main/resources/META-INF/rewrite/classpath.tsv.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void toJavaBase64() {
java(
"""
import org.apache.commons.codec.binary.Base64;

class Test {
static byte[] decodeBytes(byte[] encodedBytes) {
return Base64.decodeBase64(encodedBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -26,7 +27,7 @@
class ApacheCommonsFileUtilsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("commons-io"))
spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),"commons-io"))
.recipe(new ApacheCommonsFileUtilsRecipes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -28,7 +29,7 @@ class ApacheFileUtilsToJavaFilesTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec
.recipe(new ApacheFileUtilsToJavaFiles())
.parser(JavaParser.fromJavaVersion().classpath("commons-io"));
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-io"));
}

@DocumentExample
Expand All @@ -42,7 +43,7 @@ void convertTest() {
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import java.util.List;

class A {
byte[] readFileBytes(File file) {
return FileUtils.readFileToByteArray(file);
Expand All @@ -62,9 +63,9 @@ List<String> readLinesWithCharsetId(File file) {
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;

import java.util.List;

class A {
byte[] readFileBytes(File file) {
return Files.readAllBytes(file.toPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.apache.commons.io;

import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -29,7 +30,7 @@ class ApacheIOUtilsUseExplicitCharsetTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec
.recipe(new ApacheIOUtilsUseExplicitCharset(null))
.parser(JavaParser.fromJavaVersion().classpath("commons-io"));
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-io"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -28,7 +29,7 @@ class UseJavaStandardCharsetsTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec
.recipeFromResources("org.openrewrite.apache.commons.io.UseStandardCharsets")
.parser(JavaParser.fromJavaVersion().classpath("commons-io"));
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-io"));
}

@DocumentExample
Expand All @@ -41,7 +42,7 @@ void toStandardCharsets() {
"""
import java.nio.charset.Charset;
import org.apache.commons.io.Charsets;

class A {
Charset iso88591 = Charsets.ISO_8859_1;
Charset usAscii = Charsets.US_ASCII;
Expand All @@ -54,7 +55,7 @@ class A {
"""
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

class A {
Charset iso88591 = StandardCharsets.ISO_8859_1;
Charset usAscii = StandardCharsets.US_ASCII;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.config.Environment;
import org.openrewrite.java.JavaParser;
Expand All @@ -34,7 +35,7 @@ public void defaults(RecipeSpec spec) {
.scanRuntimeClasspath("org.openrewrite.apache.commons.io")
.build()
.activateRecipes("org.openrewrite.apache.commons.io.UseSystemLineSeparator"))
.parser(JavaParser.fromJavaVersion().classpath("commons-io"));
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-io"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -30,7 +31,7 @@ class ApacheCommonsStringUtilsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("commons-lang3"))
spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-lang3"))
.recipe(new ApacheCommonsStringUtilsRecipes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -30,7 +31,7 @@ class IsNotEmptyToJdkTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("commons-lang3", "plexus-utils", "maven-shared-utils"))
spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "commons-lang3", "plexus-utils", "maven-shared-utils"))
.recipe(new IsNotEmptyToJdk());
}

Expand Down Expand Up @@ -71,7 +72,7 @@ void trim(String import_) {
java(
"""
import %s;

class A {
boolean test(String first) {
boolean a = StringUtils.isEmpty(first.trim());
Expand All @@ -87,7 +88,7 @@ String foo() {
""".formatted(import_),
"""
import %s;

class A {
boolean test(String first) {
boolean a = first.trim().isEmpty();
Expand Down Expand Up @@ -190,7 +191,7 @@ String getField() {
java(
"""
import org.apache.commons.lang3.StringUtils;

class A {
boolean test(B b) {
return StringUtils.isEmpty(b.getField());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -28,7 +29,7 @@ class MavenSharedStringUtilsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("maven-shared-utils"))
spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "maven-shared-utils"))
.recipe(new MavenSharedStringUtilsRecipes());
}

Expand Down Expand Up @@ -81,19 +82,19 @@ void bar(String in, CharSequence cs) {
String[] array;
boolean bool;
String string;

// Test all methods in alphabetical order to only execute the slow recipes once
string = in.length() <= 10 ? in : in.substring(0, 10 - 3) + "...";
string = in == null || in.isEmpty() ? in : Character.toTitleCase(in.charAt(0)) + in.substring(1);
string = Objects.toString(in, "");
string = Objects.toString(in, "nil");
string = in.replaceAll("\\\\s+", "");

bool = in == null ? false : in.equalsIgnoreCase("other");
bool = Objects.equals(in, "other");
//bool = StringUtils.equals(cs, "other");
bool = StringUtils.isEmpty(in);

string = in == null ? null : in.toLowerCase();
string = in == null || in.isEmpty() ? in : in.replace("search", "replacement");
string = in == null ? null : new StringBuffer(in).reverse().toString();
Expand Down
Loading
Loading