1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau;
18
19 import static org.apache.juneau.commons.utils.AssertionUtils.*;
20 import static org.apache.juneau.commons.utils.Utils.*;
21
22 import java.lang.annotation.*;
23 import java.lang.reflect.*;
24 import java.util.*;
25 import java.util.stream.*;
26
27 import org.apache.juneau.annotation.*;
28 import org.apache.juneau.commons.reflect.*;
29 import org.apache.juneau.svl.*;
30
31
32
33
34
35
36 public class AnnotationWorkList extends ArrayList<AnnotationWork> {
37 private static final long serialVersionUID = 1L;
38
39
40
41
42
43
44 public static AnnotationWorkList create() {
45 return new AnnotationWorkList(VarResolver.DEFAULT.createSession());
46 }
47
48
49
50
51
52
53
54 public static AnnotationWorkList create(VarResolverSession vrs) {
55 return new AnnotationWorkList(vrs);
56 }
57
58
59
60
61
62
63
64 public static AnnotationWorkList of(Stream<AnnotationInfo<? extends Annotation>> annotations) {
65 return create().add(annotations);
66 }
67
68
69
70
71
72
73
74
75 public static AnnotationWorkList of(VarResolverSession vrs, Stream<AnnotationInfo<? extends Annotation>> annotations) {
76 return create(vrs).add(annotations);
77 }
78
79 private final VarResolverSession vrs;
80
81 private AnnotationWorkList(VarResolverSession vrs) {
82 this.vrs = assertArgNotNull("vrs", vrs);
83 }
84
85
86
87
88
89
90
91
92 public AnnotationWorkList add(AnnotationInfo<?> ai, AnnotationApplier<Annotation,Object> aa) {
93 add(new AnnotationWork(ai, aa));
94 return this;
95 }
96
97
98
99
100
101
102
103 public AnnotationWorkList add(Stream<AnnotationInfo<? extends Annotation>> annotations) {
104 annotations.sorted(Comparator.comparingInt(AnnotationInfo::getRank)).forEach(this::applyAnnotation);
105 return this;
106 }
107
108
109
110
111
112
113 @SuppressWarnings("unchecked")
114 private void applyAnnotation(AnnotationInfo<?> ai) {
115 var a = ai.inner();
116 var cpa = assertNotNull(a.annotationType().getAnnotation(ContextApply.class), "Annotation found without @ContextApply: %s", cn(ai.annotationType()));
117 Arrays.stream(cpa.value())
118 .map(x -> safe(() -> (Constructor<? extends AnnotationApplier<?,?>>)x.getConstructor(VarResolverSession.class)))
119 .forEach(applyConstructor -> {
120 var applier = safe(() -> (AnnotationApplier<Annotation,Object>)applyConstructor.newInstance(vrs));
121 add(ai, applier);
122 });
123 }
124 }