|
16 | 16 |
|
17 | 17 | package org.springframework.data.couchbase.core.convert;
|
18 | 18 |
|
| 19 | +import java.lang.annotation.Annotation; |
19 | 20 | import java.lang.reflect.Constructor;
|
20 | 21 | import java.lang.reflect.InvocationTargetException;
|
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.util.Collections; |
21 | 24 | import java.util.HashMap;
|
22 | 25 | import java.util.Map;
|
| 26 | +import java.util.Optional; |
| 27 | +import java.util.concurrent.ConcurrentHashMap; |
23 | 28 |
|
| 29 | +import org.springframework.beans.BeanUtils; |
24 | 30 | import org.springframework.data.convert.PropertyValueConverter;
|
25 | 31 | import org.springframework.data.convert.PropertyValueConverterFactory;
|
26 | 32 | import org.springframework.data.convert.ValueConversionContext;
|
27 | 33 | import org.springframework.data.mapping.PersistentProperty;
|
28 | 34 |
|
29 | 35 | import com.couchbase.client.core.encryption.CryptoManager;
|
30 | 36 | import com.couchbase.client.java.encryption.annotation.Encrypted;
|
| 37 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 38 | +import com.fasterxml.jackson.annotation.JsonValue; |
31 | 39 |
|
32 | 40 | /**
|
33 |
| - * Accept the Couchbase @Encrypted annotation in addition to @ValueConverter |
| 41 | + * Accept the Couchbase @Encrypted and @JsonValue annotations in addition to @ValueConverter annotation.<br> |
| 42 | + * There can only be one propertyValueConverter for a property. Although there maybe be multiple annotations, |
| 43 | + * getConverter(property) only returns one converter (a ChainedPropertyValueConverter might be useful). Note that |
| 44 | + * valueConversions.afterPropertiesSet() encapsulates this in a CachingPropertyValueConverterFactory which caches by |
| 45 | + * 'property'. Although CachingPropertyValueConverterFactory does have the functionality to cache by a type, it only |
| 46 | + * caches by the type specified on an @ValueConverter annotation.To avoid having identical converter instances for each |
| 47 | + * instance of a class containing an @JsonValue annotation, converterCacheForType is used. |
34 | 48 | *
|
35 | 49 | * @author Michael Reiche
|
36 | 50 | */
|
37 | 51 | public class CouchbasePropertyValueConverterFactory implements PropertyValueConverterFactory {
|
38 | 52 |
|
39 |
| - CryptoManager cryptoManager; |
40 |
| - Map<Class<? extends PropertyValueConverter<?, ?, ?>>, PropertyValueConverter<?, ?, ?>> converterCache = new HashMap<>(); |
41 |
| - |
| 53 | + final CryptoManager cryptoManager; |
| 54 | + static protected final Map<Class<?>, Optional<PropertyValueConverter<?, ?, ?>>> converterCacheForType = new ConcurrentHashMap<>(); |
42 | 55 | public CouchbasePropertyValueConverterFactory(CryptoManager cryptoManager) {
|
43 | 56 | this.cryptoManager = cryptoManager;
|
44 | 57 | }
|
45 | 58 |
|
| 59 | + /** |
| 60 | + * @param property must not be {@literal null}. |
| 61 | + * @return |
| 62 | + * @param <DV> destination value |
| 63 | + * @param <SV> source value |
| 64 | + * @param <P> context |
| 65 | + */ |
46 | 66 | @Override
|
47 | 67 | public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
|
48 | 68 | PersistentProperty<?> property) {
|
49 | 69 | PropertyValueConverter<DV, SV, P> valueConverter = PropertyValueConverterFactory.super.getConverter(property);
|
50 | 70 | if (valueConverter != null) {
|
| 71 | + /* if using @ValueConverter, need process maybeTypePropertyConverter(property) for enum stuff |
| 72 | + Optional<PropertyValueConverter<?, ?, ?>> cachedConverterForType = converterCacheForType |
| 73 | + .get(property.getType()); |
| 74 | + if (cachedConverterForType != null) { |
| 75 | + return valueConverter; |
| 76 | + } |
| 77 | + PropertyValueConverter<DV, SV, P> converterForType = maybeTypePropertyConverter(property); |
| 78 | + converterCacheForType.put(property, Optional.ofNullable(converterForType)); |
| 79 | + */ |
51 | 80 | return valueConverter;
|
52 | 81 | }
|
53 |
| - Encrypted encryptedAnn = property.findAnnotation(Encrypted.class); |
54 |
| - if (encryptedAnn != null) { |
55 |
| - Class cryptoConverterClass = CryptoConverter.class; |
56 |
| - return getConverter((Class<PropertyValueConverter<DV, SV, P>>) cryptoConverterClass); |
57 |
| - } else { |
58 |
| - return null; |
| 82 | + |
| 83 | + // this will return the converter for the first annotation that requires a PropertyValueConverter |
| 84 | + for (Annotation ann : property.getField().getAnnotations()) { |
| 85 | + Class<?> converterClass = converterFromFieldAnnotation(ann); |
| 86 | + if (converterClass != null) { |
| 87 | + return getConverter((Class<PropertyValueConverter<DV, SV, P>>) converterClass, property); |
| 88 | + } |
| 89 | + } |
| 90 | + return (PropertyValueConverter<DV, SV, P> )converterCacheForType |
| 91 | + .computeIfAbsent(property.getType(), p -> Optional.ofNullable((maybeTypePropertyConverter(property)))).orElse(null); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * lookup the converter class from the annotation. Analogous to getting the converter class from the value() attribute |
| 96 | + * of the @ValueProperty annotation |
| 97 | + * |
| 98 | + * @param ann the annotation |
| 99 | + * @return the class of the converter |
| 100 | + */ |
| 101 | + private Class<?> converterFromFieldAnnotation(Annotation ann) { |
| 102 | + if (ann instanceof Encrypted) { |
| 103 | + return CryptoConverter.class; |
59 | 104 | }
|
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> maybeTypePropertyConverter( |
| 109 | + PersistentProperty<?> property) { |
| 110 | + |
| 111 | + Class<?> type = property.getType(); |
| 112 | + |
| 113 | + // since we need to find the annotated method to determine if a converter is required, |
| 114 | + // we may as well cache it. And also create a value -> enum-object map for Enums. |
| 115 | + Method jsonValueMethod = null; |
| 116 | + for (Method m : type.getDeclaredMethods()) { |
| 117 | + JsonValue jsonValueAnn = m.getAnnotation(JsonValue.class); |
| 118 | + if (jsonValueAnn != null && jsonValueAnn.value()) { |
| 119 | + jsonValueMethod = m; |
| 120 | + jsonValueMethod.setAccessible(true); |
| 121 | + JsonValueConverter.valueMethodCache.put(type, jsonValueMethod); |
| 122 | + // for Enums, we can compute the conversion on read() |
| 123 | + // otherwise, we rely on a constructor with an argument that is the output of the JsonValue method (inverse) |
| 124 | + if (type.isEnum()) { |
| 125 | + Map<Object, Enum<?>> enumConstants = new HashMap<>(); |
| 126 | + Class<Enum<?>> enumType = (Class<Enum<?>>) type; |
| 127 | + for (Enum<?> e : enumType.getEnumConstants()) { // create a value -> enum-object map for Enums |
| 128 | + try { |
| 129 | + enumConstants.put(Optional.ofNullable(jsonValueMethod.invoke(e)), e); |
| 130 | + } catch (IllegalAccessException | InvocationTargetException ex) { |
| 131 | + throw new RuntimeException(ex); |
| 132 | + } |
| 133 | + } |
| 134 | + enumConstants = Collections.unmodifiableMap(enumConstants); |
| 135 | + JsonValueConverter.enumConstantsCache.put(type, enumConstants); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + Constructor<?> jsonCreatorMethod = null; |
| 141 | + for (Constructor<?> m : type.getConstructors()) { |
| 142 | + JsonCreator jsonCreatorAnn = m.getAnnotation(JsonCreator.class); |
| 143 | + if (jsonCreatorAnn != null && !jsonCreatorAnn.mode().equals(JsonCreator.Mode.DISABLED)) { |
| 144 | + jsonCreatorMethod = m; |
| 145 | + jsonCreatorMethod.setAccessible(true); |
| 146 | + JsonValueConverter.creatorMethodCache.put(type, jsonCreatorMethod); |
| 147 | + // hopefully the jsonValueConverter.read() will know what to do with this. |
| 148 | + } |
| 149 | + } |
| 150 | + if (jsonValueMethod != null || jsonCreatorMethod != null) { |
| 151 | + Class jsonValueConverterClass = JsonValueConverter.class; |
| 152 | + return getConverter((Class<PropertyValueConverter<DV, SV, P>>) jsonValueConverterClass, property); |
| 153 | + } |
| 154 | + return null; // we didn't find a property value converter to use |
60 | 155 | }
|
61 | 156 |
|
62 | 157 | @Override
|
63 | 158 | public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
|
64 | 159 | Class<? extends PropertyValueConverter<DV, SV, P>> converterType) {
|
| 160 | + return getConverter(converterType, null); |
| 161 | + } |
65 | 162 |
|
66 |
| - PropertyValueConverter<?, ?, ?> converter = converterCache.get(converterType); |
67 |
| - if (converter != null) { |
68 |
| - return (PropertyValueConverter<DV, SV, P>) converter; |
69 |
| - } |
| 163 | + /** |
| 164 | + * @param converterType |
| 165 | + * @param property |
| 166 | + * @return |
| 167 | + * @param <DV> |
| 168 | + * @param <SV> |
| 169 | + * @param <P> |
| 170 | + */ |
| 171 | + public <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter( |
| 172 | + Class<? extends PropertyValueConverter<DV, SV, P>> converterType, PersistentProperty<?> property) { |
70 | 173 |
|
| 174 | + // CryptoConverter takes a cryptoManager argument |
71 | 175 | if (CryptoConverter.class.isAssignableFrom(converterType)) {
|
72 |
| - converter = new CryptoConverter(cryptoManager); |
73 |
| - } else { |
| 176 | + return (PropertyValueConverter<DV, SV, P>) new CryptoConverter(cryptoManager); |
| 177 | + } else if (property != null) { // try constructor that takes PersistentProperty, fall-back to no-args constructor |
74 | 178 | try {
|
75 |
| - Constructor constructor = converterType.getConstructor(); |
76 |
| - converter = (PropertyValueConverter<?, ?, ?>) constructor.newInstance(); |
77 |
| - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { |
78 |
| - throw new RuntimeException(e); |
79 |
| - } |
| 179 | + Constructor<?> constructor = converterType.getConstructor(PersistentProperty.class); |
| 180 | + try { |
| 181 | + return (PropertyValueConverter<DV, SV, P>) constructor.newInstance(property); |
| 182 | + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 183 | + throw new RuntimeException(e); // the constructor that takes a property failed |
| 184 | + } |
| 185 | + } catch (NoSuchMethodException e) {} |
80 | 186 | }
|
81 |
| - converterCache.put((Class<? extends PropertyValueConverter<DV, SV, P>>) converter.getClass(), converter); |
82 |
| - return (PropertyValueConverter<DV, SV, P>) converter; |
83 |
| - |
| 187 | + // there is no constructor that takes a property, fall-back to no-args constructor |
| 188 | + return BeanUtils.instantiateClass(converterType); |
84 | 189 | }
|
85 | 190 | }
|
0 commit comments