Flatten repeated CovariantReturnType annotations

Bug: 269740550
Test: m libcore-openjdk-analyzer && libcore-openjdk-analyzer list-new-apis -t 17
Change-Id: I9e812ed0024b7c9e3a7c56af0ad896f7ef5008ef
diff --git a/tools/openjdk-analyzer/src/libcore/tools/analyzer/openjdk/Main.java b/tools/openjdk-analyzer/src/libcore/tools/analyzer/openjdk/Main.java
index 9afe7e4..528d6c2 100644
--- a/tools/openjdk-analyzer/src/libcore/tools/analyzer/openjdk/Main.java
+++ b/tools/openjdk-analyzer/src/libcore/tools/analyzer/openjdk/Main.java
@@ -29,6 +29,7 @@
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.Opcodes;
 import org.objectweb.asm.Type;
+import org.objectweb.asm.tree.AnnotationNode;
 import org.objectweb.asm.tree.ClassNode;
 import org.objectweb.asm.tree.FieldNode;
 import org.objectweb.asm.tree.MethodNode;
@@ -221,11 +222,31 @@
          * to generate synthetic methods with a different return type.
          */
         private static Stream<MethodNode> getImpliedMethods(MethodNode node) {
-            if (node.invisibleAnnotations == null) {
+            if (node.invisibleAnnotations == null
+                    // Synthetic methods generated by javac can be annotated with
+                    // @CovariantReturnType, but can be safely ignored to avoid double counting.
+                    || (node.access & Opcodes.ACC_SYNTHETIC) != 0) {
                 return Stream.of(node);
             }
 
             Stream<MethodNode> syntheticMethods = node.invisibleAnnotations.stream()
+                    .flatMap(a -> { // flatten CovariantReturnTypes.value
+                        if (!"Ldalvik/annotation/codegen/CovariantReturnType$CovariantReturnTypes;"
+                                .equals(a.desc)) {
+                            return Stream.of(a);
+                        }
+                        for (int i = 0; i < a.values.size() - 1; i++) {
+                            if ("value".equals(a.values.get(i))) {
+                                Object type = a.values.get(i + 1);
+                                if (type instanceof List nodes) {
+                                    return Stream.concat(Stream.of(a),
+                                            (Stream<AnnotationNode>) nodes.stream());
+                                }
+                            }
+                        }
+                        return Stream.of(a);
+
+                    })
                     .filter(a -> "Ldalvik/annotation/codegen/CovariantReturnType;".equals(a.desc))
                     .map(a -> {
                         for (int i = 0; i < a.values.size() - 1; i++) {