Skip to content

Commit

Permalink
Merge pull request #837 from CodexRaunak/Update-Recipe/Migrate-Acegi-…
Browse files Browse the repository at this point in the history
…Security-to-Spring-Security

Update Recipe Migrate Acegi Security To Spring Security
  • Loading branch information
jonesbusy authored Mar 2, 2025
2 parents bb76958 + c906407 commit ec3ecaf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -12,6 +13,8 @@
import org.openrewrite.java.ChangePackage;
import org.openrewrite.java.ChangeType;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -315,9 +318,44 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
}
}

// add super(null) call to the constructor of the class that extends AbstractAuthenticationToken
if (enclosingClass != null
&& enclosingClass.getExtends() != null
&& enclosingClass.getExtends().getType() != null
&& enclosingClass
.getExtends()
.getType()
.toString()
.equals("org.springframework.security.authentication.AbstractAuthenticationToken")) {
if (method.isConstructor()) {
if (method.getBody() != null) {
J.Block body = method.getBody();

// Avoid duplicate insertions
if (body.getStatements().isEmpty()
|| !(body.getStatements().get(0) instanceof J.MethodInvocation
&& body.getStatements()
.get(0)
.printTrimmed()
.contains("super(new GrantedAuthority[] {})"))) {
method = addSuperCallTemplate.apply(
updateCursor(method),
body.getCoordinates().firstStatement());
}
}
}
}

return super.visitMethodDeclaration(method, ctx);
}

private final JavaTemplate addSuperCallTemplate = JavaTemplate.builder("super(null);")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion()
.addClasspathEntry(
Path.of("target/openrewrite-jars").resolve("jenkins-core-2.497.jar")))
.build();

private J.CompilationUnit addImportIfNotExists(J.CompilationUnit cu, String className, String packageName) {
boolean importExists = cu.getImports().stream().anyMatch(anImport -> (packageName + "." + className)
.equals(anImport.getQualid().toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ void migrateAcegiToSpringSecurityWhenBarExtendsAbstractAuthenticationToken() {
spec -> {
var parser = JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true);
collectRewriteTestDependencies().forEach(parser::addClasspathEntry);
spec.recipe(new MigrateAcegiSecurityToSpringSecurity()).parser(parser);
spec.recipe(new MigrateAcegiSecurityToSpringSecurity())
.expectedCyclesThatMakeChanges(1)
.cycles(1)
.parser(parser);
},
java(
"""
Expand All @@ -174,6 +177,9 @@ void migrateAcegiToSpringSecurityWhenBarExtendsAbstractAuthenticationToken() {
import jenkins.security.SecurityListener;
public class Bar extends AbstractAuthenticationToken {
Bar() {
System.out.println("Bar");
}
@Override
public GrantedAuthority[] getAuthorities() {
return getName() != null? null : new GrantedAuthority[0];
Expand Down Expand Up @@ -213,6 +219,10 @@ public String getName() {
import org.springframework.security.authentication.BadCredentialsException;
public class Bar extends AbstractAuthenticationToken {
Bar() {
super(null);
System.out.println("Bar");
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
return getName() != null? null : List.of();
Expand Down

0 comments on commit ec3ecaf

Please sign in to comment.