|
| 1 | +package com.mservicetech.client.mapping; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | +import com.mservicetech.client.mapping.adapter.LocalDateAdapter; |
| 5 | +import com.mservicetech.client.mapping.adapter.LocalDateTimeAdapter; |
| 6 | +import org.yaml.snakeyaml.Yaml; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.lang.reflect.Array; |
| 13 | +import java.lang.reflect.Field; |
| 14 | +import java.lang.reflect.ParameterizedType; |
| 15 | +import java.lang.reflect.Type; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | + |
| 29 | +public class AnnotationBaseMapperImpl extends BaseMapperImpl implements ClientJsonMapper { |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + public AnnotationBaseMapperImpl() { |
| 34 | + init(); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Map elements from the JsonObject to the Class. |
| 40 | + * Filed details are in annotations. |
| 41 | + * |
| 42 | + * @param root - JsonObject instance. |
| 43 | + * @param clazz - Type to set the values. |
| 44 | + * @return Instance with values. |
| 45 | + */ |
| 46 | + protected <T> T mapResult(final JsonObject root, final Class<T> clazz){ |
| 47 | + if (!isSerializable(clazz) ) { |
| 48 | + throw new MappingException("The class " + clazz.getSimpleName() + " is not annotated with MappingSerializable"); |
| 49 | + } |
| 50 | + T object; |
| 51 | + try { |
| 52 | + object = clazz.newInstance(); |
| 53 | + MappingElement mappingElement; |
| 54 | + MappingObject mappingObject; |
| 55 | + MappingCollection mappingCollection; |
| 56 | + String path = null; |
| 57 | + CustomConverter customConverter; |
| 58 | + Class<?> instantation; |
| 59 | + // Object value; |
| 60 | + for (Field field : clazz.getDeclaredFields()) { |
| 61 | + field.setAccessible(true); |
| 62 | + Object value = null; |
| 63 | + if (field.isAnnotationPresent(MappingElement.class)) { |
| 64 | + mappingElement = field.getAnnotation(MappingElement.class); |
| 65 | + |
| 66 | + path = mappingElement.scanPath(); |
| 67 | + if (mappingElement.scanPath().isEmpty()) { |
| 68 | + path = field.getName(); |
| 69 | + } |
| 70 | + |
| 71 | + if (!mappingElement.forceCustomConverter()) { |
| 72 | + final String[] pathElements = path.split("\\,"); // In scanPath it is possible to define more than one places that is possible to extract the value. |
| 73 | + for (int i=0; i<pathElements.length; i++) { |
| 74 | + value = extractValue(root, pathElements[i], field.getType()); |
| 75 | + if (value != null) { |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + if (!mappingElement.customConverter().isEmpty()) { |
| 81 | + instantation = Class.forName(mappingElement.customConverter()); |
| 82 | + customConverter = (CustomConverter) instantation.newInstance(); |
| 83 | + |
| 84 | + value = customConverter.convert(root, field, path, mappingElement.dependingOn(), value, object); |
| 85 | + } |
| 86 | + } else if (field.isAnnotationPresent(MappingObject.class)) { |
| 87 | + mappingObject = field.getAnnotation(MappingObject.class); |
| 88 | + |
| 89 | + path = mappingObject.scanPath(); |
| 90 | + if (mappingObject.scanPath().isEmpty()) { |
| 91 | + path = field.getName(); |
| 92 | + } |
| 93 | + |
| 94 | + if (!mappingObject.forceCustomConverter()) { |
| 95 | + value = mapResult(root.getAsJsonObject(path), field.getType()); |
| 96 | + } |
| 97 | + if (!mappingObject.customConverter().isEmpty()) { |
| 98 | + instantation = Class.forName(mappingObject.customConverter()); |
| 99 | + customConverter = (CustomConverter) instantation.newInstance(); |
| 100 | + value = customConverter.convert(root, field, path, mappingObject.dependingOn(), value, object); |
| 101 | + } |
| 102 | + } else if (field.isAnnotationPresent(MappingCollection.class)) { |
| 103 | + mappingCollection = field.getAnnotation(MappingCollection.class); |
| 104 | + |
| 105 | + path = mappingCollection.scanPath(); |
| 106 | + if (mappingCollection.scanPath().isEmpty()) { |
| 107 | + path = field.getName(); |
| 108 | + } |
| 109 | + |
| 110 | + if (!mappingCollection.forceCustomConverter()) { |
| 111 | + Class<?> listClass; |
| 112 | + if (field.getType().isArray()) { |
| 113 | + listClass = field.getType().getComponentType(); |
| 114 | + value = createArray(root, path, listClass); |
| 115 | + } else if (Map.class.isAssignableFrom(field.getType())) { |
| 116 | + final ParameterizedType mapType = (ParameterizedType) field.getGenericType(); |
| 117 | + value = createMap(root, path, mapType); |
| 118 | + } else { |
| 119 | + final ParameterizedType listType = (ParameterizedType) field.getGenericType(); |
| 120 | + listClass = (Class<?>) listType.getActualTypeArguments()[0]; |
| 121 | + value = createList(root, path, listClass); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!mappingCollection.customConverter().isEmpty()) { |
| 126 | + instantation = Class.forName(mappingCollection.customConverter()); |
| 127 | + customConverter = (CustomConverter) instantation.newInstance(); |
| 128 | + value = customConverter.convert(root, field, path, mappingCollection.dependingOn(), value, object); |
| 129 | + } |
| 130 | + } |
| 131 | + field.set(object, value); |
| 132 | + } |
| 133 | + } catch (Exception e) { |
| 134 | + throw new MappingException("MappingError: ", e); |
| 135 | + } |
| 136 | + |
| 137 | + return object; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments