Skip to content

Commit ec3ecaf

Browse files
authored
Merge pull request #837 from CodexRaunak/Update-Recipe/Migrate-Acegi-Security-to-Spring-Security
Update Recipe Migrate Acegi Security To Spring Security
2 parents bb76958 + c906407 commit ec3ecaf

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/MigrateAcegiSecurityToSpringSecurity.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jenkins.tools.pluginmodernizer.core.recipes;
22

3+
import java.nio.file.Path;
34
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.List;
@@ -12,6 +13,8 @@
1213
import org.openrewrite.java.ChangePackage;
1314
import org.openrewrite.java.ChangeType;
1415
import org.openrewrite.java.JavaIsoVisitor;
16+
import org.openrewrite.java.JavaParser;
17+
import org.openrewrite.java.JavaTemplate;
1518
import org.openrewrite.java.MethodMatcher;
1619
import org.openrewrite.java.tree.Expression;
1720
import org.openrewrite.java.tree.J;
@@ -315,9 +318,44 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
315318
}
316319
}
317320

321+
// add super(null) call to the constructor of the class that extends AbstractAuthenticationToken
322+
if (enclosingClass != null
323+
&& enclosingClass.getExtends() != null
324+
&& enclosingClass.getExtends().getType() != null
325+
&& enclosingClass
326+
.getExtends()
327+
.getType()
328+
.toString()
329+
.equals("org.springframework.security.authentication.AbstractAuthenticationToken")) {
330+
if (method.isConstructor()) {
331+
if (method.getBody() != null) {
332+
J.Block body = method.getBody();
333+
334+
// Avoid duplicate insertions
335+
if (body.getStatements().isEmpty()
336+
|| !(body.getStatements().get(0) instanceof J.MethodInvocation
337+
&& body.getStatements()
338+
.get(0)
339+
.printTrimmed()
340+
.contains("super(new GrantedAuthority[] {})"))) {
341+
method = addSuperCallTemplate.apply(
342+
updateCursor(method),
343+
body.getCoordinates().firstStatement());
344+
}
345+
}
346+
}
347+
}
348+
318349
return super.visitMethodDeclaration(method, ctx);
319350
}
320351

352+
private final JavaTemplate addSuperCallTemplate = JavaTemplate.builder("super(null);")
353+
.contextSensitive()
354+
.javaParser(JavaParser.fromJavaVersion()
355+
.addClasspathEntry(
356+
Path.of("target/openrewrite-jars").resolve("jenkins-core-2.497.jar")))
357+
.build();
358+
321359
private J.CompilationUnit addImportIfNotExists(J.CompilationUnit cu, String className, String packageName) {
322360
boolean importExists = cu.getImports().stream().anyMatch(anImport -> (packageName + "." + className)
323361
.equals(anImport.getQualid().toString()));

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ void migrateAcegiToSpringSecurityWhenBarExtendsAbstractAuthenticationToken() {
155155
spec -> {
156156
var parser = JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true);
157157
collectRewriteTestDependencies().forEach(parser::addClasspathEntry);
158-
spec.recipe(new MigrateAcegiSecurityToSpringSecurity()).parser(parser);
158+
spec.recipe(new MigrateAcegiSecurityToSpringSecurity())
159+
.expectedCyclesThatMakeChanges(1)
160+
.cycles(1)
161+
.parser(parser);
159162
},
160163
java(
161164
"""
@@ -174,6 +177,9 @@ void migrateAcegiToSpringSecurityWhenBarExtendsAbstractAuthenticationToken() {
174177
import jenkins.security.SecurityListener;
175178
176179
public class Bar extends AbstractAuthenticationToken {
180+
Bar() {
181+
System.out.println("Bar");
182+
}
177183
@Override
178184
public GrantedAuthority[] getAuthorities() {
179185
return getName() != null? null : new GrantedAuthority[0];
@@ -213,6 +219,10 @@ public String getName() {
213219
import org.springframework.security.authentication.BadCredentialsException;
214220
215221
public class Bar extends AbstractAuthenticationToken {
222+
Bar() {
223+
super(null);
224+
System.out.println("Bar");
225+
}
216226
@Override
217227
public Collection<GrantedAuthority> getAuthorities() {
218228
return getName() != null? null : List.of();

0 commit comments

Comments
 (0)