forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarativeContract.java
More file actions
202 lines (177 loc) · 8.26 KB
/
DeclarativeContract.java
File metadata and controls
202 lines (177 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Copyright 2012-2019 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Predicate;
import feign.Contract.BaseContract;
/**
* {@link Contract} base implementation that works by declaring witch annotations should be
* processed and how each annotation modifies {@link MethodMetadata}
*/
public abstract class DeclarativeContract extends BaseContract {
private List<GuardedAnnotationProcessor> classAnnotationProcessors = new ArrayList<>();
private List<GuardedAnnotationProcessor> methodAnnotationProcessors = new ArrayList<>();
Map<Class<Annotation>, DeclarativeContract.ParameterAnnotationProcessor<Annotation>> parameterAnnotationProcessors =
new HashMap<>();
@Override
public final List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
// any implementations must register processors
return super.parseAndValidatateMetadata(targetType);
}
/**
* Called by parseAndValidateMetadata twice, first on the declaring class, then on the target type
* (unless they are the same).
*
* @param data metadata collected so far relating to the current java method.
* @param clz the class to process
*/
@Override
protected final void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) {
Arrays.stream(targetType.getAnnotations())
.forEach(annotation -> classAnnotationProcessors.stream()
.filter(processor -> processor.test(annotation))
.forEach(processor -> processor.process(annotation, data)));
}
/**
* @param data metadata collected so far relating to the current java method.
* @param annotation annotations present on the current method annotation.
* @param method method currently being processed.
*/
@Override
protected final void processAnnotationOnMethod(MethodMetadata data,
Annotation annotation,
Method method) {
methodAnnotationProcessors.stream()
.filter(processor -> processor.test(annotation))
.forEach(processor -> processor.process(annotation, data));
}
/**
* @param data metadata collected so far relating to the current java method.
* @param annotations annotations present on the current parameter annotation.
* @param paramIndex if you find a name in {@code annotations}, call
* {@link #nameParam(MethodMetadata, String, int)} with this as the last parameter.
* @return true if you called {@link #nameParam(MethodMetadata, String, int)} after finding an
* http-relevant annotation.
*/
@Override
protected final boolean processAnnotationsOnParameter(MethodMetadata data,
Annotation[] annotations,
int paramIndex) {
Arrays.stream(annotations)
.filter(
annotation -> parameterAnnotationProcessors.containsKey(annotation.annotationType()))
.forEach(annotation -> parameterAnnotationProcessors
.getOrDefault(annotation.annotationType(), ParameterAnnotationProcessor.DO_NOTHING)
.process(annotation, data, paramIndex));
return false;
}
/**
* Called while class annotations are being processed
*
* @param annotationType to be processed
* @param processor function that defines the annotations modifies {@link MethodMetadata}
*/
protected <E extends Annotation> void registerClassAnnotation(Class<E> annotationType,
DeclarativeContract.AnnotationProcessor<E> processor) {
registerClassAnnotation(
annotation -> annotation.annotationType().equals(annotationType),
processor);
}
/**
* Called while class annotations are being processed
*
* @param predicate to check if the annotation should be processed or not
* @param processor function that defines the annotations modifies {@link MethodMetadata}
*/
protected <E extends Annotation> void registerClassAnnotation(Predicate<E> predicate,
DeclarativeContract.AnnotationProcessor<E> processor) {
this.classAnnotationProcessors.add(new GuardedAnnotationProcessor(predicate, processor));
}
/**
* Called while method annotations are being processed
*
* @param annotationType to be processed
* @param processor function that defines the annotations modifies {@link MethodMetadata}
*/
protected <E extends Annotation> void registerMethodAnnotation(Class<E> annotationType,
DeclarativeContract.AnnotationProcessor<E> processor) {
registerMethodAnnotation(
annotation -> annotation.annotationType().equals(annotationType),
processor);
}
/**
* Called while method annotations are being processed
*
* @param predicate to check if the annotation should be processed or not
* @param processor function that defines the annotations modifies {@link MethodMetadata}
*/
protected <E extends Annotation> void registerMethodAnnotation(Predicate<E> predicate,
DeclarativeContract.AnnotationProcessor<E> processor) {
this.methodAnnotationProcessors.add(new GuardedAnnotationProcessor(predicate, processor));
}
/**
* Called while method parameter annotations are being processed
*
* @param annotation to be processed
* @param processor function that defines the annotations modifies {@link MethodMetadata}
*/
protected <E extends Annotation> void registerParameterAnnotation(Class<E> annotation,
DeclarativeContract.ParameterAnnotationProcessor<E> processor) {
this.parameterAnnotationProcessors.put((Class) annotation,
(DeclarativeContract.ParameterAnnotationProcessor) processor);
}
@FunctionalInterface
public interface AnnotationProcessor<E extends Annotation> {
/**
* @param annotation present on the current element.
* @param metadata collected so far relating to the current java method.
*/
void process(E annotation, MethodMetadata metadata);
}
@FunctionalInterface
public interface ParameterAnnotationProcessor<E extends Annotation> {
DeclarativeContract.ParameterAnnotationProcessor<Annotation> DO_NOTHING = (ann, data, i) -> {
};
/**
* @param annotation present on the current parameter annotation.
* @param metadata metadata collected so far relating to the current java method.
* @param paramIndex if you find a name in {@code annotations}, call
* {@link #nameParam(MethodMetadata, String, int)} with this as the last parameter.
* @return true if you called {@link #nameParam(MethodMetadata, String, int)} after finding an
* http-relevant annotation.
*/
void process(E annotation, MethodMetadata metadata, int paramIndex);
}
private class GuardedAnnotationProcessor
implements Predicate<Annotation>, DeclarativeContract.AnnotationProcessor<Annotation> {
private Predicate<Annotation> predicate;
private DeclarativeContract.AnnotationProcessor<Annotation> processor;
@SuppressWarnings({"rawtypes", "unchecked"})
private GuardedAnnotationProcessor(Predicate predicate,
DeclarativeContract.AnnotationProcessor processor) {
this.predicate = predicate;
this.processor = processor;
}
@Override
public void process(Annotation annotation, MethodMetadata metadata) {
processor.process(annotation, metadata);
}
@Override
public boolean test(Annotation t) {
return predicate.test(t);
}
}
}