|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors |
| 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 | + * https://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 org.springframework.data.couchbase.core.convert.translation; |
| 17 | + |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.springframework.data.convert.PropertyValueConverter; |
| 24 | +import org.springframework.data.convert.ValueConversionContext; |
| 25 | +import org.springframework.data.mapping.PersistentProperty; |
| 26 | + |
| 27 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 28 | + |
| 29 | +/** |
| 30 | + * Enum properties annotated with JsonValue |
| 31 | + * |
| 32 | + * @author Michael Reiche |
| 33 | + */ |
| 34 | +public class JsonValueConverter |
| 35 | + implements PropertyValueConverter<Enum<?>, Object, ValueConversionContext<? extends PersistentProperty<?>>> { |
| 36 | + |
| 37 | + Map<Class<? extends Enum>, Method> methodCache = new HashMap(); |
| 38 | + Map<Class<? extends Enum>, Map<Object, Enum<?>>> enumConstantsCache = new HashMap(); |
| 39 | + |
| 40 | + @Override |
| 41 | + public Enum<?> read(Object value, ValueConversionContext<? extends PersistentProperty<?>> context) { |
| 42 | + Class<Enum> type = (Class<Enum>) context.getProperty().getActualType(); |
| 43 | + return getEnumConstant(type, value); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Object write(Enum<?> value, ValueConversionContext<? extends PersistentProperty<?>> context) { |
| 48 | + try { |
| 49 | + Class<? extends Enum> type = value.getClass(); |
| 50 | + return getJsonValueMethod(type).invoke(value); |
| 51 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private Enum<?> getEnumConstant(Class<Enum> type, Object value) { |
| 57 | + Map<Object, Enum<?>> enumConstants = enumConstantsCache.get(type); |
| 58 | + if (enumConstants == null) { |
| 59 | + enumConstants = new HashMap<>(); |
| 60 | + enumConstantsCache.put(type, enumConstants); |
| 61 | + Method jsonValueMethod = getJsonValueMethod(type); |
| 62 | + for (Enum<?> e : type.getEnumConstants()) { // cache all the constants while we're here |
| 63 | + try { |
| 64 | + enumConstants.put(jsonValueMethod.invoke(e), e); |
| 65 | + } catch (IllegalAccessException | InvocationTargetException ex) { |
| 66 | + throw new RuntimeException(ex); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Enum<?> theEnum = enumConstants.get(value); |
| 72 | + if (theEnum == null) { |
| 73 | + throw new RuntimeException("could not deserialize " + value + " to " + type); |
| 74 | + } |
| 75 | + return theEnum; |
| 76 | + } |
| 77 | + |
| 78 | + private Method getJsonValueMethod(Class<? extends Enum> type) { |
| 79 | + Method jsonValueMethod = methodCache.get(type); |
| 80 | + if (jsonValueMethod != null) { |
| 81 | + return jsonValueMethod; |
| 82 | + } |
| 83 | + for (Method m : type.getDeclaredMethods()) { |
| 84 | + JsonValue jsonValueAnn = m.getAnnotation(JsonValue.class); |
| 85 | + if (jsonValueAnn != null && jsonValueAnn.value()) { |
| 86 | + jsonValueMethod = m; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + if (jsonValueMethod == null) { // Is this necessary? Useful? |
| 91 | + for (Method m : type.getMethods()) { |
| 92 | + JsonValue jsonValueAnn = m.getAnnotation(JsonValue.class); |
| 93 | + if (jsonValueAnn != null && jsonValueAnn.value()) { |
| 94 | + jsonValueMethod = m; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + // if nothing was found, use toString() |
| 100 | + if (jsonValueMethod == null) { |
| 101 | + try { |
| 102 | + jsonValueMethod = type.getMethod("toString"); |
| 103 | + } catch (NoSuchMethodException e) { |
| 104 | + throw new RuntimeException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + jsonValueMethod.setAccessible(true); |
| 108 | + methodCache.put(type, jsonValueMethod); |
| 109 | + return jsonValueMethod; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments