Skip to content

Commit

Permalink
Migrate UsernamePasswordCredentials (#44)
Browse files Browse the repository at this point in the history
* Migrate UsernamePasswordCredentials

* Minor polish

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
SiBorea and timtebeek authored Nov 28, 2024
1 parent 330f54a commit 8affb54
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

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 String getDisplayName() {
return "Migrate `UsernamePasswordCredentials` to httpclient5";
}

@Override
public String getDescription() {
return "Change the password argument going into `UsernamePasswordCredentials` to be a `char[]`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher methodMatcher = new MethodMatcher(METHOD_PATTERN);
return Preconditions.check(new UsesMethod<>(methodMatcher), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass nc = super.visitNewClass(newClass, ctx);
if (methodMatcher.matches(nc)) {
Expression passwordArgument = nc.getArguments().get(1);
nc = JavaTemplate.apply("#{any(String)}.toCharArray()",
getCursor(), passwordArgument.getCoordinates().replace(), passwordArgument);
}
return nc;
}
});
}
}
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
Loading

0 comments on commit 8affb54

Please sign in to comment.