Skip to content

Commit

Permalink
Merge pull request #15063 from raboof/keep-annotation-order-fixing-14743
Browse files Browse the repository at this point in the history
Keep annotation order
  • Loading branch information
smarter authored Apr 28, 2022
2 parents f29eea7 + 414091f commit 35b1084
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Constants._
import Types._
import Decorators._

import scala.collection.mutable

class RepeatableAnnotations extends MiniPhase:

override def phaseName: String = RepeatableAnnotations.name
Expand All @@ -28,7 +30,7 @@ class RepeatableAnnotations extends MiniPhase:
tree

private def aggregateAnnotations(annotations: Seq[Annotation])(using Context): List[Annotation] =
val annsByType = annotations.groupBy(_.symbol)
val annsByType = stableGroupBy(annotations, _.symbol)
annsByType.flatMap {
case (_, a :: Nil) => a :: Nil
case (sym, anns) if sym.derivesFrom(defn.ClassfileAnnotationClass) =>
Expand All @@ -50,6 +52,14 @@ class RepeatableAnnotations extends MiniPhase:
case (_, anns) => anns
}.toList

private def stableGroupBy[A, K](ins: Seq[A], f: A => K): scala.collection.MapView[K, List[A]] =
val out = new mutable.LinkedHashMap[K, mutable.ListBuffer[A]]()
for (in <- ins) {
val buffer = out.getOrElseUpdate(f(in), new mutable.ListBuffer)
buffer += in
}
out.view.mapValues(_.toList)

object RepeatableAnnotations:
val name: String = "repeatableAnnotations"
val description: String = "aggregate repeatable annotations"
8 changes: 8 additions & 0 deletions tests/pos/Annotations.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo.bar

import jdk.jfr.Enabled

@Enabled
@Deprecated
final class Annotations {
}
2 changes: 2 additions & 0 deletions tests/pos/annotations1/a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Annot1(s: String) extends scala.annotation.StaticAnnotation
class Annot2(s: Class[_]) extends scala.annotation.StaticAnnotation
3 changes: 3 additions & 0 deletions tests/pos/annotations1/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@Annot1("foo")
@Annot2(classOf[AnyRef])
class Test
5 changes: 5 additions & 0 deletions tests/pos/annotationsJava/Annot1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot1 { String value() default ""; }
5 changes: 5 additions & 0 deletions tests/pos/annotationsJava/Annot2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot2 { Class value(); }
1 change: 1 addition & 0 deletions tests/pos/annotationsJava/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Annot1("foo") @Annot2(classOf[AnyRef]) class Test
13 changes: 13 additions & 0 deletions tests/pos/annotationsJavaRepeatable/Annot1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.lang.annotation.*;

@Repeatable(Annot1.Container.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Annot1 { String value() default "";

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface Container {
Annot1[] value();
}
}
6 changes: 6 additions & 0 deletions tests/pos/annotationsJavaRepeatable/Annot2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot2 { Class value(); }
1 change: 1 addition & 0 deletions tests/pos/annotationsJavaRepeatable/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Annot1("foo") @Annot2(classOf[String]) @Annot1("bar") class Test

0 comments on commit 35b1084

Please sign in to comment.