From 9bc16b460b7ae9e58d3783ffd42ff3b32d4afd02 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 21 Jan 2024 13:47:10 +0100 Subject: [PATCH] [MPLUGIN-506] Translation between V3 and V4 Mojo API There are currently two differences on Mojo annotations that cause build failure. This PR fixes those and prepares for potentially more differences. --- https://issues.apache.org/jira/browse/MPLUGIN-506 --- .../DefaultMojoAnnotationsScanner.java | 22 +++++++++++++++++-- .../visitors/MojoAnnotationVisitor.java | 12 ++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java index a91aea24e..c886064a9 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java @@ -81,7 +81,17 @@ public class DefaultMojoAnnotationsScanner extends AbstractLogEnabled implements private static final Pattern SCANNABLE_CLASS = Pattern.compile("[^-]+\\.class"); private static final String EMPTY = ""; - private Reflector reflector = new Reflector(); + private final Reflector reflector = new Reflector(); + + private final HashMap mojo3To4Translations = new HashMap<>(); + private final HashMap execute3To4Translations = new HashMap<>(); + private final HashMap parameter3To4Translations = new HashMap<>(); + + public DefaultMojoAnnotationsScanner() { + // Mojo V4 vs Mojo V3 + mojo3To4Translations.put("projectRequired", "requiresProject"); + mojo3To4Translations.put("onlineRequired", "requiresOnline"); + } @Override public Map scan(MojoAnnotationsScannerRequest request) throws ExtractionException { @@ -266,7 +276,15 @@ protected void populateAnnotationContent(Object content, MojoAnnotationVisitor m throws ReflectorException { for (Map.Entry entry : mojoAnnotationVisitor.getAnnotationValues().entrySet()) { - reflector.invoke(content, entry.getKey(), new Object[] {entry.getValue()}); + String entryKey = entry.getKey(); + if (MOJO_V4.equals(mojoAnnotationVisitor.getAnnotationClassName())) { + entryKey = mojo3To4Translations.getOrDefault(entryKey, entryKey); + } else if (EXECUTE_V4.equals(mojoAnnotationVisitor.getAnnotationClassName())) { + entryKey = execute3To4Translations.getOrDefault(entryKey, entryKey); + } else if (PARAMETER_V4.equals(mojoAnnotationVisitor.getAnnotationClassName())) { + entryKey = parameter3To4Translations.getOrDefault(entryKey, entryKey); + } + reflector.invoke(content, entryKey, new Object[] {entry.getValue()}); } } diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java index 9eb420da9..6c7ccc67f 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java @@ -31,15 +31,19 @@ * @since 3.0 */ public class MojoAnnotationVisitor extends AnnotationVisitor { - private String annotationClassName; + private final String annotationClassName; - private Map annotationValues = new HashMap<>(); + private final Map annotationValues = new HashMap<>(); MojoAnnotationVisitor(String annotationClassName) { super(Opcodes.ASM9); this.annotationClassName = annotationClassName; } + public String getAnnotationClassName() { + return annotationClassName; + } + public Map getAnnotationValues() { return annotationValues; } @@ -55,12 +59,12 @@ public void visitEnum(String name, String desc, String value) { } @Override - public AnnotationVisitor visitAnnotation(String name, String desc) { + public MojoAnnotationVisitor visitAnnotation(String name, String desc) { return new MojoAnnotationVisitor(this.annotationClassName); } @Override - public AnnotationVisitor visitArray(String s) { + public MojoAnnotationVisitor visitArray(String s) { return new MojoAnnotationVisitor(this.annotationClassName); } }