• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #868

17 Mar 2026 07:53PM UTC coverage: 83.945% (+0.005%) from 83.94%
#868

push

raphw
Fix test.

29966 of 35697 relevant lines covered (83.95%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.5
/byte-buddy-dep/src/main/java/net/bytebuddy/description/annotation/AnnotationList.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.description.annotation;
17

18
import net.bytebuddy.description.type.TypeDescription;
19
import net.bytebuddy.description.type.TypeList;
20
import net.bytebuddy.matcher.ElementMatcher;
21
import net.bytebuddy.matcher.FilterableList;
22
import net.bytebuddy.utility.AnnotationComparator;
23
import net.bytebuddy.utility.GraalImageCode;
24
import net.bytebuddy.utility.nullability.AlwaysNull;
25
import net.bytebuddy.utility.nullability.MaybeNull;
26

27
import java.lang.annotation.Annotation;
28
import java.lang.annotation.RetentionPolicy;
29
import java.util.ArrayList;
30
import java.util.Arrays;
31
import java.util.Collections;
32
import java.util.List;
33
import java.util.Set;
34

35
/**
36
 * Defines a list of annotation instances.
37
 */
38
public interface AnnotationList extends FilterableList<AnnotationDescription, AnnotationList> {
39

40
    /**
41
     * Checks if this list contains an annotation of the given type.
42
     *
43
     * @param annotationType The type to find in the list.
44
     * @return {@code true} if the list contains the annotation type.
45
     */
46
    boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
47

48
    /**
49
     * Checks if this list contains an annotation of the given type.
50
     *
51
     * @param annotationType The type to find in the list.
52
     * @return {@code true} if the list contains the annotation type.
53
     */
54
    boolean isAnnotationPresent(TypeDescription annotationType);
55

56
    /**
57
     * Finds the first annotation of the given type and returns it.
58
     *
59
     * @param annotationType The type to be found in the list.
60
     * @param <T>            The annotation type.
61
     * @return The annotation description or {@code null} if no such annotation was found.
62
     */
63
    @MaybeNull
64
    <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T> annotationType);
65

66
    /**
67
     * Finds the first annotation of the given type and returns it.
68
     *
69
     * @param annotationType The type to be found in the list.
70
     * @return The annotation description or {@code null} if no such annotation was found.
71
     */
72
    AnnotationDescription ofType(TypeDescription annotationType);
73

74
    /**
75
     * Returns only annotations that are marked as {@link java.lang.annotation.Inherited} as long as they are not
76
     * contained by the set of ignored annotation types.
77
     *
78
     * @param ignoredTypes A list of annotation types to be ignored from the lookup.
79
     * @return A list of all inherited annotations besides of the given ignored types.
80
     */
81
    AnnotationList inherited(Set<? extends TypeDescription> ignoredTypes);
82

83
    /**
84
     * Only retains annotations with the given retention policy.
85
     *
86
     * @param matcher A matcher for the required retention policy.
87
     * @return A of annotations only with elements
88
     */
89
    AnnotationList visibility(ElementMatcher<? super RetentionPolicy> matcher);
90

91
    /**
92
     * Returns a list of the annotation types of this list.
93
     *
94
     * @return A list of the annotation types of this list.
95
     */
96
    TypeList asTypeList();
97

98
    /**
99
     * Returns a list of the names of the annotation types. This list might contain the names of
100
     * annotations that are not otherwise resolvable.
101
     *
102
     * @return A list of binary names of the represented annotations.
103
     */
104
    List<String> asTypeNames();
105

106
    /**
107
     * An abstract base implementation of an annotation list.
108
     */
109
    abstract class AbstractBase extends FilterableList.AbstractBase<AnnotationDescription, AnnotationList> implements AnnotationList {
1✔
110

111
        /**
112
         * {@inheritDoc}
113
         */
114
        public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
115
            for (AnnotationDescription annotation : this) {
1✔
116
                if (annotation.getAnnotationType().represents(annotationType)) {
1✔
117
                    return true;
1✔
118
                }
119
            }
1✔
120
            return false;
1✔
121
        }
122

123
        /**
124
         * {@inheritDoc}
125
         */
126
        public boolean isAnnotationPresent(TypeDescription annotationType) {
127
            for (AnnotationDescription annotation : this) {
1✔
128
                if (annotation.getAnnotationType().equals(annotationType)) {
1✔
129
                    return true;
1✔
130
                }
131
            }
1✔
132
            return false;
1✔
133
        }
134

135
        /**
136
         * {@inheritDoc}
137
         */
138
        @SuppressWarnings("unchecked")
139
        @MaybeNull
140
        public <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T> annotationType) {
141
            for (AnnotationDescription annotation : this) {
1✔
142
                if (annotation.getAnnotationType().represents(annotationType)) {
1✔
143
                    return annotation.prepare(annotationType);
1✔
144
                }
145
            }
1✔
146
            return (AnnotationDescription.Loadable<T>) AnnotationDescription.UNDEFINED;
1✔
147
        }
148

149
        /**
150
         * {@inheritDoc}
151
         */
152
        @MaybeNull
153
        public AnnotationDescription ofType(TypeDescription annotationType) {
154
            for (AnnotationDescription annotation : this) {
1✔
155
                if (annotation.getAnnotationType().equals(annotationType)) {
1✔
156
                    return annotation;
1✔
157
                }
158
            }
1✔
159
            return AnnotationDescription.UNDEFINED;
1✔
160
        }
161

162
        /**
163
         * {@inheritDoc}
164
         */
165
        public AnnotationList inherited(Set<? extends TypeDescription> ignoredTypes) {
166
            List<AnnotationDescription> inherited = new ArrayList<AnnotationDescription>(size());
1✔
167
            for (AnnotationDescription annotation : this) {
1✔
168
                if (!ignoredTypes.contains(annotation.getAnnotationType()) && annotation.isInherited()) {
1✔
169
                    inherited.add(annotation);
1✔
170
                }
171
            }
1✔
172
            return wrap(inherited);
1✔
173
        }
174

175
        /**
176
         * {@inheritDoc}
177
         */
178
        public AnnotationList visibility(ElementMatcher<? super RetentionPolicy> matcher) {
179
            List<AnnotationDescription> annotationDescriptions = new ArrayList<AnnotationDescription>(size());
1✔
180
            for (AnnotationDescription annotation : this) {
1✔
181
                if (matcher.matches(annotation.getRetention())) {
1✔
182
                    annotationDescriptions.add(annotation);
1✔
183
                }
184
            }
1✔
185
            return wrap(annotationDescriptions);
1✔
186
        }
187

188
        /**
189
         * {@inheritDoc}
190
         */
191
        public TypeList asTypeList() {
192
            List<TypeDescription> annotationTypes = new ArrayList<TypeDescription>(size());
1✔
193
            for (AnnotationDescription annotation : this) {
1✔
194
                annotationTypes.add(annotation.getAnnotationType());
1✔
195
            }
1✔
196
            return new TypeList.Explicit(annotationTypes);
1✔
197
        }
198

199
        /**
200
         * {@inheritDoc}
201
         */
202
        public List<String> asTypeNames() {
203
            List<String> typeNames = new ArrayList<String>(size());
1✔
204
            for (AnnotationDescription annotation : this) {
1✔
205
                typeNames.add(annotation.getAnnotationType().getName());
1✔
206
            }
1✔
207
            return typeNames;
1✔
208
        }
209

210
        @Override
211
        protected AnnotationList wrap(List<AnnotationDescription> values) {
212
            return new Explicit(values);
1✔
213
        }
214
    }
215

216
    /**
217
     * Describes an array of loaded {@link java.lang.annotation.Annotation}s as an annotation list.
218
     */
219
    class ForLoadedAnnotations extends AbstractBase {
220

221
        /**
222
         * The represented annotations.
223
         */
224
        private final List<? extends Annotation> annotations;
225

226
        /**
227
         * Creates a new list of loaded annotations.
228
         *
229
         * @param annotation The represented annotations.
230
         */
231
        public ForLoadedAnnotations(Annotation... annotation) {
232
            this(Arrays.asList(GraalImageCode.getCurrent().sorted(annotation, AnnotationComparator.INSTANCE)));
1✔
233
        }
1✔
234

235
        /**
236
         * Creates a new list of loaded annotations.
237
         *
238
         * @param annotations The represented annotations.
239
         */
240
        public ForLoadedAnnotations(List<? extends Annotation> annotations) {
1✔
241
            this.annotations = annotations;
1✔
242
        }
1✔
243

244
        /**
245
         * Creates a list of annotation lists representing the given loaded annotations.
246
         *
247
         * @param annotations The annotations to represent where each dimension is converted into a list.
248
         * @return A list of annotation lists representing the given annotations.
249
         */
250
        public static List<AnnotationList> asList(Annotation[][] annotations) {
251
            List<AnnotationList> result = new ArrayList<AnnotationList>(annotations.length);
×
252
            for (Annotation[] annotation : annotations) {
×
253
                result.add(new ForLoadedAnnotations(annotation));
×
254
            }
255
            return result;
×
256
        }
257

258
        /**
259
         * {@inheritDoc}
260
         */
261
        public AnnotationDescription get(int index) {
262
            return AnnotationDescription.ForLoadedAnnotation.of(annotations.get(index));
1✔
263
        }
264

265
        /**
266
         * {@inheritDoc}
267
         */
268
        public int size() {
269
            return annotations.size();
1✔
270
        }
271
    }
272

273
    /**
274
     * Represents a list of explicitly provided annotation descriptions.
275
     */
276
    class Explicit extends AbstractBase {
277

278
        /**
279
         * The list of represented annotation descriptions.
280
         */
281
        private final List<? extends AnnotationDescription> annotationDescriptions;
282

283
        /**
284
         * Creates a new list of annotation descriptions.
285
         *
286
         * @param annotationDescription The list of represented annotation descriptions.
287
         */
288
        public Explicit(AnnotationDescription... annotationDescription) {
289
            this(Arrays.asList(annotationDescription));
1✔
290
        }
1✔
291

292
        /**
293
         * Creates a new list of annotation descriptions.
294
         *
295
         * @param annotationDescriptions The list of represented annotation descriptions.
296
         */
297
        public Explicit(List<? extends AnnotationDescription> annotationDescriptions) {
1✔
298
            this.annotationDescriptions = annotationDescriptions;
1✔
299
        }
1✔
300

301
        /**
302
         * Creates a list of annotation lists for a given multidimensional list of annotation descriptions.
303
         *
304
         * @param annotations The list of annotations to represent as a list of annotation lists.
305
         * @return The list of annotation lists.
306
         */
307
        public static List<AnnotationList> asList(List<? extends List<? extends AnnotationDescription>> annotations) {
308
            List<AnnotationList> result = new ArrayList<AnnotationList>(annotations.size());
×
309
            for (List<? extends AnnotationDescription> annotation : annotations) {
×
310
                result.add(new Explicit(annotation));
×
311
            }
×
312
            return result;
×
313
        }
314

315
        /**
316
         * {@inheritDoc}
317
         */
318
        public AnnotationDescription get(int index) {
319
            return annotationDescriptions.get(index);
1✔
320
        }
321

322
        /**
323
         * {@inheritDoc}
324
         */
325
        public int size() {
326
            return annotationDescriptions.size();
1✔
327
        }
328
    }
329

330
    /**
331
     * Represents an empty annotation list.
332
     */
333
    class Empty extends FilterableList.Empty<AnnotationDescription, AnnotationList> implements AnnotationList {
1✔
334

335
        /**
336
         * Creates a list of empty annotation lists of the given dimension.
337
         *
338
         * @param length The length of the list.
339
         * @return A list of empty annotation lists of the given length.
340
         */
341
        public static List<AnnotationList> asList(int length) {
342
            List<AnnotationList> result = new ArrayList<AnnotationList>(length);
×
343
            for (int i = 0; i < length; i++) {
×
344
                result.add(new AnnotationList.Empty());
×
345
            }
346
            return result;
×
347
        }
348

349
        /**
350
         * {@inheritDoc}
351
         */
352
        public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
353
            return false;
1✔
354
        }
355

356
        /**
357
         * {@inheritDoc}
358
         */
359
        public boolean isAnnotationPresent(TypeDescription annotationType) {
360
            return false;
1✔
361
        }
362

363
        /**
364
         * {@inheritDoc}
365
         */
366
        @SuppressWarnings("unchecked")
367
        @AlwaysNull
368
        public <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T> annotationType) {
369
            return (AnnotationDescription.Loadable<T>) AnnotationDescription.UNDEFINED;
1✔
370
        }
371

372
        /**
373
         * {@inheritDoc}
374
         */
375
        @AlwaysNull
376
        public AnnotationDescription ofType(TypeDescription annotationType) {
377
            return AnnotationDescription.UNDEFINED;
×
378
        }
379

380
        /**
381
         * {@inheritDoc}
382
         */
383
        public AnnotationList inherited(Set<? extends TypeDescription> ignoredTypes) {
384
            return this;
1✔
385
        }
386

387
        /**
388
         * {@inheritDoc}
389
         */
390
        public AnnotationList visibility(ElementMatcher<? super RetentionPolicy> matcher) {
391
            return this;
1✔
392
        }
393

394
        /**
395
         * {@inheritDoc}
396
         */
397
        public TypeList asTypeList() {
398
            return new TypeList.Empty();
1✔
399
        }
400

401
        /**
402
         * {@inheritDoc}
403
         */
404
        public List<String> asTypeNames() {
405
            return Collections.emptyList();
1✔
406
        }
407
    }
408
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc