-
Notifications
You must be signed in to change notification settings - Fork 4
/
PojoCodecProvider.java
286 lines (248 loc) · 12 KB
/
PojoCodecProvider.java
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package de.bild.codec;
import de.bild.codec.annotations.DecodeUndefinedHandlingStrategy;
import de.bild.codec.annotations.DecodingFieldFailureStrategy;
import de.bild.codec.annotations.DecodingPojoFailureStrategy;
import de.bild.codec.annotations.EncodeNullHandlingStrategy;
import org.bson.BsonReader;
import org.bson.BsonValue;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.function.Predicate;
/**
* Provides a codec for Pojos
* Use the internal builder to register classes and packages that can be handled by the codec
*/
public class PojoCodecProvider implements CodecProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(PojoCodecProvider.class);
private final TypesModel typesModel;
private final PojoContext pojoContext;
PojoCodecProvider(final Set<Class<?>> classes,
final Set<String> packages,
final Set<Class<? extends Annotation>> ignoreAnnotations,
Set<Predicate<String>> ignoreTypesMatchingClassNamePredicates,
Set<Class<?>> ignoreClasses, List<TypeCodecProvider> typeCodecProviders,
final List<CodecResolver> codecResolvers,
CodecConfiguration codecConfiguration, ClassResolver classResolver) {
this.typesModel = new TypesModel(classes, packages, ignoreAnnotations, ignoreTypesMatchingClassNamePredicates, ignoreClasses, classResolver);
this.pojoContext = new PojoContext(typesModel, codecResolvers, typeCodecProviders, codecConfiguration);
}
public static Builder builder() {
return new Builder();
}
@Override
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
// if clazz has type parameters, we warn the user that generic class definitions are problematic
Codec<T> codec = pojoContext.get(clazz, registry);
if (codec instanceof TypeCodec) {
if (clazz != null && clazz.getTypeParameters().length > 0) {
LOGGER.warn("Generic classes will only be encoded/decoded with their upper bounds! " +
"We could prohibit handling of the pojo codec for those generic classes, " +
"but then user would loose flexibility when subclassing such classes. Class: {}", clazz.toGenericString());
}
TypeCodec typeCodec = (TypeCodec) codec;
// generate dynamic proxy to add CollectibleCodec functionality
if (typeCodec.isCollectible()) {
LOGGER.debug("Enhancing {} to be collectible codec.", typeCodec);
// For easy of use, adding all implemented interfaces to the proxy, so the proxy can be used for most use cases
// Unfortunately the functionalities of the underlying concrete codec class will be missing.
ArrayList<Class<?>> proxyInterfaceList = new ArrayList<>(Arrays.asList(typeCodec.getClass().getInterfaces()));
proxyInterfaceList.add(CollectibleCodec.class);
proxyInterfaceList.add(DelegatingCodec.class); // so users can retrieve the delegating codec form the proxy.
return (CollectibleCodec) Proxy.newProxyInstance(
PojoCodecProvider.class.getClassLoader(),
proxyInterfaceList.toArray(new Class<?>[1]),
new CollectibleCodecDelegator(typeCodec));
}
}
return codec;
}
public Bson getTypeFilter(Class<?> clazz, CodecRegistry registry) {
Codec<?> codec = get(clazz, registry);
if (codec instanceof TypeCodec) {
return ((TypeCodec) codec).getTypeFilter();
}
return null;
}
/**
* delegator for CollectibleCodec
*/
private static class CollectibleCodecDelegator<T> implements InvocationHandler, CollectibleCodec<T>, DelegatingCodec<T> {
private final TypeCodec<T> delegatingCodec;
public CollectibleCodecDelegator(TypeCodec<T> delegatingCodec) {
this.delegatingCodec = delegatingCodec;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (method.getDeclaringClass() == CollectibleCodec.class || method.getDeclaringClass() == DelegatingCodec.class) {
return method.invoke(this, args);
} else {
return method.invoke(delegatingCodec, args);
}
} catch (IllegalAccessException | InvocationTargetException e) {
LOGGER.warn("An exception was caught while invoking the delegate {} with args {}", method, args);
LOGGER.debug("Original exception when invoking target.", e);
// rethrowing cause instead of invocationexception
throw e.getCause();
}
}
@Override
public T generateIdIfAbsentFromDocument(T document) {
return delegatingCodec.generateIdIfAbsentFromDocument(document);
}
@Override
public boolean documentHasId(T document) {
return delegatingCodec.documentHasId(document);
}
@Override
public BsonValue getDocumentId(T document) {
return delegatingCodec.getDocumentId(document);
}
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
return delegatingCodec.decode(reader, decoderContext);
}
@Override
public void encode(BsonWriter writer, T value, EncoderContext encoderContext) {
delegatingCodec.encode(writer, value, encoderContext);
}
@Override
public Class<T> getEncoderClass() {
return delegatingCodec.getEncoderClass();
}
@Override
public TypeCodec<T> getDelegate() {
return delegatingCodec;
}
}
/**
*
*/
public static class Builder {
private Set<String> packages = new HashSet<>();
private Set<Class<?>> classes = new HashSet<>();
private List<CodecResolver> codecResolvers = new ArrayList<>();
private Set<Class<? extends Annotation>> ignoreAnnotations = new HashSet<>();
private Set<Predicate<String>> ignoreTypesMatchingClassNamePredicates = new HashSet<>();
private Set<Class<?>> ignoreClasses = new HashSet<>();
private ClassResolver classResolver;
private List<TypeCodecProvider> typeCodecProviders = new ArrayList<>();
private EncodeNullHandlingStrategy.Strategy encodeNullHandlingStrategy = EncodeNullHandlingStrategy.Strategy.CODEC;
private DecodeUndefinedHandlingStrategy.Strategy decodeUndefinedHandlingStrategy = DecodeUndefinedHandlingStrategy.Strategy.KEEP_POJO_DEFAULT;
private DecodingFieldFailureStrategy.Strategy decodingFieldFailureStrategy = DecodingFieldFailureStrategy.Strategy.RETHROW_EXCEPTION;
private DecodingPojoFailureStrategy.Strategy decodingPojoFailureStrategy = DecodingPojoFailureStrategy.Strategy.RETHROW_EXCEPTION;
private boolean encodeNulls = false;
public Builder setPackages(Set<String> packages) {
this.packages = packages;
return this;
}
public Builder register(String... packages) {
this.packages.addAll(Arrays.asList(packages));
return this;
}
public Builder register(Class<?>... classes) {
this.classes.addAll(Arrays.asList(classes));
return this;
}
/**
* If you need to provide a mechanism to scan packages for model classes, register a {@link ClassResolver}
*
* @param classResolver the resolver for classes within packages
* @return this Builder
*/
public Builder registerClassResolver(ClassResolver classResolver) {
this.classResolver = classResolver;
return this;
}
public Builder ignoreTypesAnnotatedWith(Class<? extends Annotation>... annotations) {
this.ignoreAnnotations.addAll(Arrays.asList(annotations));
return this;
}
/**
* If you need to exclude private inner classes form the domain model, use a Predicate
*
* @param ignoreTypesMatchingClassNamePredicates
* @return the Builder
*/
public Builder ignoreTypesMatchingClassNamePredicate(Predicate<String>... ignoreTypesMatchingClassNamePredicates) {
this.ignoreTypesMatchingClassNamePredicates.addAll(Arrays.asList(ignoreTypesMatchingClassNamePredicates));
return this;
}
/**
* If ypu can point to the classes to be ignored, you can do this here
*
* @return the Builder
*/
public Builder ignoreClasses(Class<?>... ignoreClasses) {
this.ignoreClasses.addAll(Arrays.asList(ignoreClasses));
return this;
}
/**
* In case you need to register
*
* @param typeCodecProviders
* @return
*/
public Builder register(TypeCodecProvider... typeCodecProviders) {
this.typeCodecProviders.addAll(Arrays.asList(typeCodecProviders));
return this;
}
public Builder decodingPojoFailureStrategy(DecodingPojoFailureStrategy.Strategy decodingPojoFailureStrategy) {
if (decodingPojoFailureStrategy != null) {
this.decodingPojoFailureStrategy = decodingPojoFailureStrategy;
}
return this;
}
public Builder decodingFieldFailureStrategy(DecodingFieldFailureStrategy.Strategy decodingFieldFailureStrategy) {
if (decodingFieldFailureStrategy != null) {
this.decodingFieldFailureStrategy = decodingFieldFailureStrategy;
}
return this;
}
public Builder encodeNullHandlingStrategy(EncodeNullHandlingStrategy.Strategy encodeNullHandlingStrategy) {
if (encodeNullHandlingStrategy != null) {
this.encodeNullHandlingStrategy = encodeNullHandlingStrategy;
}
return this;
}
public Builder decodeUndefinedHandlingStrategy(DecodeUndefinedHandlingStrategy.Strategy decodeUndefinedHandlingStrategy) {
if (decodeUndefinedHandlingStrategy != null) {
this.decodeUndefinedHandlingStrategy = decodeUndefinedHandlingStrategy;
}
return this;
}
public Builder encodeNulls(boolean encodeNulls) {
this.encodeNulls = encodeNulls;
return this;
}
/**
* A CodecResolver is supposed to provide specialized codecs in case the default implementation
* {@link BasicReflectionCodec} is not sufficient
*
* @param codecResolvers a list of CodecResolvers to be registered
* @return the builder
*/
public Builder registerCodecResolver(CodecResolver... codecResolvers) {
this.codecResolvers.addAll(Arrays.asList(codecResolvers));
return this;
}
public PojoCodecProvider build() {
CodecConfiguration codecConfiguration = new CodecConfiguration(encodeNulls, encodeNullHandlingStrategy, decodeUndefinedHandlingStrategy, decodingFieldFailureStrategy, decodingPojoFailureStrategy);
return new PojoCodecProvider(classes, packages, ignoreAnnotations, ignoreTypesMatchingClassNamePredicates, ignoreClasses, typeCodecProviders, codecResolvers, codecConfiguration, classResolver);
}
}
}