11package io .smallrye .openapi .runtime .util ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
45import java .util .Collection ;
6+ import java .util .Collections ;
57import java .util .EnumMap ;
68import java .util .HashSet ;
79import java .util .List ;
@@ -74,8 +76,17 @@ private static Collection<AnnotationInstance> getDeclaredAnnotations(AnnotationT
7476 private static List <AnnotationInstance > declaredFieldAnnotations (FieldInfo field ) {
7577 List <AnnotationInstance > fieldAnnotations = field .declaredAnnotations ();
7678 List <AnnotationInstance > propertyAnnotations = KotlinUtil .getPropertyAnnotations (field );
77- return Stream .concat (fieldAnnotations .stream (), propertyAnnotations .stream ())
78- .collect (Collectors .toList ());
79+
80+ if (propertyAnnotations .isEmpty ()) {
81+ return fieldAnnotations ;
82+ } else if (fieldAnnotations .isEmpty ()) {
83+ return propertyAnnotations ;
84+ } else {
85+ List <AnnotationInstance > results = new ArrayList <>(fieldAnnotations .size () + propertyAnnotations .size ());
86+ results .addAll (fieldAnnotations );
87+ results .addAll (propertyAnnotations );
88+ return results ;
89+ }
7990 }
8091
8192 private boolean composable (DotName annotation ) {
@@ -90,47 +101,56 @@ private boolean composable(DotName annotation) {
90101 return true ;
91102 }
92103
93- private Stream <AnnotationInstance > getComposedAnnotation (Collection <AnnotationInstance > declaredAnnotations ,
104+ private List <AnnotationInstance > getComposedAnnotation (Collection <AnnotationInstance > declaredAnnotations ,
94105 DotName name ,
95106 Set <DotName > scanned ) {
96107
97- return declaredAnnotations
98- .stream ()
99- .filter (AnnotationInstance ::runtimeVisible )
100- .map (AnnotationInstance ::name )
101- .filter (this ::composable )
102- .map (context .getAugmentedIndex ()::getClassByName )
103- .filter (Objects ::nonNull )
104- .flatMap (annotationClass -> {
105- if (scanned .contains (annotationClass .name ())) {
106- return null ;
108+ List <AnnotationInstance > results = new ArrayList <>();
109+
110+ for (AnnotationInstance annotation : declaredAnnotations ) {
111+ if (annotation .runtimeVisible ()) {
112+ DotName annotationName = annotation .name ();
113+ if (composable (annotationName )) {
114+ ClassInfo annotationClass = context .getAugmentedIndex ().getClassByName (annotationName );
115+ if (annotationClass == null || scanned .contains (annotationClass .name ())) {
116+ continue ;
107117 }
108118
109119 scanned .add (annotationClass .name ());
110120 UtilLogging .logger .composedAnnotationSearch (name , annotationClass .name ());
111- return getDeclaredAnnotation (annotationClass , name , scanned );
112- })
113- .filter (Objects ::nonNull );
121+ results .addAll (getDeclaredAnnotation (annotationClass , name , scanned ));
122+ }
123+ }
124+ }
125+
126+ return results ;
114127 }
115128
116- private Stream <AnnotationInstance > getDeclaredAnnotation (AnnotationTarget target , DotName name , Set <DotName > scanned ) {
129+ private List <AnnotationInstance > getDeclaredAnnotation (AnnotationTarget target , DotName name , Set <DotName > scanned ) {
117130 if (target == null ) {
118- return Stream . empty ();
131+ return Collections . emptyList ();
119132 }
120133
121134 Collection <AnnotationInstance > declaredAnnotations = getDeclaredAnnotations (target );
122135
123136 if (declaredAnnotations .isEmpty ()) {
124- return Stream . empty ();
137+ return Collections . emptyList ();
125138 }
126139
127- Stream <AnnotationInstance > direct = declaredAnnotations .stream ().filter (a -> name .equals (a .name ()));
128- Stream <AnnotationInstance > composed = getComposedAnnotation (declaredAnnotations , name , scanned );
140+ AnnotationInstance direct = declaredAnnotations .stream ().filter (a -> name .equals (a .name ())).findFirst ().orElse (null );
141+ Collection <AnnotationInstance > composed = getComposedAnnotation (declaredAnnotations , name , scanned );
142+
143+ List <AnnotationInstance > results = new ArrayList <>((direct != null ? 1 : 0 ) + composed .size ());
144+
145+ if (direct != null ) {
146+ results .add (direct );
147+ }
148+ results .addAll (composed );
129149
130- return Stream . concat ( direct , composed ) ;
150+ return results ;
131151 }
132152
133- private Stream <AnnotationInstance > getDeclaredAnnotation (AnnotationTarget target , DotName name ) {
153+ private List <AnnotationInstance > getDeclaredAnnotation (AnnotationTarget target , DotName name ) {
134154 return getDeclaredAnnotation (target , name , new HashSet <>());
135155 }
136156
@@ -285,14 +305,15 @@ public List<AnnotationInstance> getRepeatableAnnotation(AnnotationTarget target,
285305 DotName singleAnnotationName ,
286306 DotName repeatableAnnotationName ) {
287307
288- Stream <AnnotationInstance > single = getDeclaredAnnotation (target , singleAnnotationName );
308+ List <AnnotationInstance > single = getDeclaredAnnotation (target , singleAnnotationName );
289309 Stream <AnnotationInstance > wrapped = getDeclaredAnnotation (target , repeatableAnnotationName )
310+ .stream ()
290311 .map (a -> this .<AnnotationInstance []> value (a , VALUE ))
291312 .filter (Objects ::nonNull )
292313 .flatMap (Arrays ::stream )
293314 .map (a -> AnnotationInstance .create (a .name (), target , a .values ()));
294315
295- return Stream .concat (single , wrapped ).collect (Collectors .toList ());
316+ return Stream .concat (single . stream () , wrapped ).collect (Collectors .toList ());
296317 }
297318
298319 /**
@@ -306,9 +327,10 @@ public List<AnnotationInstance> getRepeatableAnnotation(AnnotationTarget target,
306327 */
307328 public AnnotationInstance getMethodParameterAnnotation (MethodInfo method , int parameterIndex ,
308329 DotName annotationName ) {
309- return getDeclaredAnnotation (MethodParameterInfo .create (method , (short ) parameterIndex ), annotationName )
310- .findFirst ()
311- .orElse (null );
330+
331+ AnnotationTarget target = MethodParameterInfo .create (method , (short ) parameterIndex );
332+ List <AnnotationInstance > found = getDeclaredAnnotation (target , annotationName );
333+ return found .isEmpty () ? null : found .get (0 );
312334 }
313335
314336 /**
@@ -364,10 +386,14 @@ public AnnotationInstance getAnnotation(AnnotationTarget annotationTarget, DotNa
364386 }
365387
366388 public AnnotationInstance getAnnotation (AnnotationTarget annotationTarget , Collection <DotName > annotationNames ) {
367- return annotationNames .stream ()
368- .flatMap (annotationName -> getDeclaredAnnotation (annotationTarget , annotationName ))
369- .findFirst ()
370- .orElse (null );
389+ for (DotName annotationName : annotationNames ) {
390+ List <AnnotationInstance > found = getDeclaredAnnotation (annotationTarget , annotationName );
391+ if (!found .isEmpty ()) {
392+ return found .get (0 );
393+ }
394+ }
395+
396+ return null ;
371397 }
372398
373399 /**
0 commit comments