|
1 | 1 | package io.jenkins.tools.pluginmodernizer.core.recipes; |
2 | 2 |
|
| 3 | +import java.nio.file.Path; |
3 | 4 | import java.util.ArrayList; |
4 | 5 | import java.util.Collections; |
5 | 6 | import java.util.List; |
|
12 | 13 | import org.openrewrite.java.ChangePackage; |
13 | 14 | import org.openrewrite.java.ChangeType; |
14 | 15 | import org.openrewrite.java.JavaIsoVisitor; |
| 16 | +import org.openrewrite.java.JavaParser; |
| 17 | +import org.openrewrite.java.JavaTemplate; |
15 | 18 | import org.openrewrite.java.MethodMatcher; |
16 | 19 | import org.openrewrite.java.tree.Expression; |
17 | 20 | import org.openrewrite.java.tree.J; |
@@ -315,9 +318,44 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex |
315 | 318 | } |
316 | 319 | } |
317 | 320 |
|
| 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 | + |
318 | 349 | return super.visitMethodDeclaration(method, ctx); |
319 | 350 | } |
320 | 351 |
|
| 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 | + |
321 | 359 | private J.CompilationUnit addImportIfNotExists(J.CompilationUnit cu, String className, String packageName) { |
322 | 360 | boolean importExists = cu.getImports().stream().anyMatch(anImport -> (packageName + "." + className) |
323 | 361 | .equals(anImport.getQualid().toString())); |
|
0 commit comments