Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate UsernamePasswordCredentials #44

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.apache.httpclient5;
SiBorea marked this conversation as resolved.
Show resolved Hide resolved

import org.openrewrite.*;
SiBorea marked this conversation as resolved.
Show resolved Hide resolved
SiBorea marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
SiBorea marked this conversation as resolved.
Show resolved Hide resolved

public class UsernamePasswordCredentials extends Recipe {
private static final String FQN = "org.apache.http.auth.UsernamePasswordCredentials";
private static final String METHOD_PATTERN = FQN + " <constructor>(String, String)";

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Migrate UsernamePasswordCredentials to httpclient5";
SiBorea marked this conversation as resolved.
Show resolved Hide resolved
}
SiBorea marked this conversation as resolved.
Show resolved Hide resolved
SiBorea marked this conversation as resolved.
Show resolved Hide resolved

@Override
public @NlsRewrite.Description String getDescription() {
return "Migrate UsernamePasswordCredentials to httpclient5.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(METHOD_PATTERN),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
newClass = super.visitNewClass(newClass, ctx);
if (TypeUtils.isOfType(newClass.getType(), JavaType.buildType(FQN))) {
newClass = JavaTemplate.builder(newClass.getArguments().get(1).printTrimmed() + ".toCharArray()")
.build()
.apply(getCursor(), newClass.getArguments().get(1).getCoordinates().replace());
}
return newClass;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ recipeList:
newGroupId: org.apache.httpcomponents.core5
newArtifactId: httpcore5
newVersion: 5.3.x
- org.openrewrite.apache.httpclient5.UsernamePasswordCredentials
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,29 @@ void method() throws IOException {
""")
);
}

@Test
void changeUsernamePasswordCredentials() {
rewriteRun(
//language=java
java(
"""
import org.apache.http.auth.UsernamePasswordCredentials;

class Example {
void method() {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
}
}
""", """
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;

class Example {
void method() {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password".toCharArray());
}
}
""")
);
}
}