1 /*
2 * Copyright 2014 - 2020 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.build.HashCodeAndEqualsPlugin;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 /**
24 * Describes a declaration source for annotations.
25 */
26 public interface AnnotationSource {
27
28 /**
29 * Returns a list of annotations that are declared by this instance.
30 *
31 * @return A list of declared annotations.
32 */
33 AnnotationList getDeclaredAnnotations();
34
35 /**
36 * An annotation source that does not declare any annotations.
37 */
38 enum Empty implements AnnotationSource {
39
40 /**
41 * The singleton instance.
42 */
43 INSTANCE;
44
45 /**
46 * {@inheritDoc}
47 */
48 public AnnotationList getDeclaredAnnotations() {
49 return new AnnotationList.Empty();
50 }
51 }
52
53 /**
54 * An annotation source that declares a given list of annotations.
55 */
56 @HashCodeAndEqualsPlugin.Enhance
57 class Explicit implements AnnotationSource {
58
59 /**
60 * The represented annotations.
61 */
62 private final List<? extends AnnotationDescription> annotations;
63
64 /**
65 * Creates a new explicit annotation source.
66 *
67 * @param annotation The represented annotations.
68 */
69 public Explicit(AnnotationDescription... annotation) {
70 this(Arrays.asList(annotation));
71 }
72
73 /**
74 * Creates a new explicit annotation source.
75 *
76 * @param annotations The represented annotations.
77 */
78 public Explicit(List<? extends AnnotationDescription> annotations) {
79 this.annotations = annotations;
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public AnnotationList getDeclaredAnnotations() {
86 return new AnnotationList.Explicit(annotations);
87 }
88 }
89 }
90