Skip to content

Commit 8dc1f4a

Browse files
author
Gerald Unterrainer
committed
fix
1 parent 7db61d3 commit 8dc1f4a

File tree

3 files changed

+12
-78
lines changed

3 files changed

+12
-78
lines changed

src/main/java/info/unterrainer/commons/serialization/objectmapper/ObjectMapper.java

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import static com.googlecode.jmapper.api.JMapperAPI.global;
44
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
55

6-
import java.awt.geom.Arc2D.Float;
7-
import java.lang.reflect.InvocationTargetException;
8-
import java.util.Collection;
96
import java.util.HashMap;
107
import java.util.Map;
118
import java.util.function.BiConsumer;
@@ -15,7 +12,6 @@
1512
import com.googlecode.jmapper.exceptions.JMapperException;
1613

1714
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException;
18-
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperProcessingException;
1915
import info.unterrainer.commons.serialization.objectmapper.key.MappingKey;
2016
import lombok.NonNull;
2117

@@ -37,14 +33,17 @@ public <A, B> void registerMapping(@NonNull final Class<A> sourceType, @NonNull
3733
mappings.put(new MappingKey<>(targetType, sourceType), back);
3834
}
3935

40-
public <S, T> T map(@NonNull final Class<T> targetType,
41-
@NonNull final S source) {
36+
public <S, T> T map(@NonNull final Class<T> targetType, @NonNull final S source) {
4237
return map(targetType, source, null);
4338
}
4439

4540
@SuppressWarnings("unchecked")
46-
public <S, T> T map(@NonNull final Class<T> targetType, @NonNull final S source,
47-
final T target) {
41+
public <S, T> T map(@NonNull final S source, @NonNull final T target) {
42+
return map((Class<T>) target.getClass(), source, target);
43+
}
44+
45+
@SuppressWarnings("unchecked")
46+
public <S, T> T map(@NonNull final Class<T> targetType, @NonNull final S source, final T target) {
4847
final Class<S> sourceType = (Class<S>) source.getClass();
4948
T result = mapWithJMapper(sourceType, targetType, source, target);
5049
return mapWithBiConsumer(sourceType, targetType, source, result);
@@ -60,23 +59,8 @@ private <T, S> T mapWithBiConsumer(@NonNull final Class<S> sourceType, @NonNull
6059
return target;
6160
}
6261

63-
private <T> T constructObjectWithObjectType(final Class<T> targetType) {
64-
try {
65-
return targetType.getConstructor().newInstance();
66-
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
67-
| NoSuchMethodException | SecurityException e) {
68-
throw new ObjectMapperProcessingException("Error while instantiating target object", e);
69-
}
70-
}
71-
7262
private <T, S> T mapWithJMapper(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType,
7363
@NonNull final S source, final T target) {
74-
if (IsPrimitveIncludingWrapper(sourceType) || IsPrimitveIncludingWrapper(targetType)
75-
|| IsPrimitiveCollectionOrArrayIncludingWrapper(sourceType)
76-
|| IsPrimitiveCollectionOrArrayIncludingWrapper(targetType)) {
77-
return constructObjectWithObjectType(targetType);
78-
}
79-
8064
@SuppressWarnings("unchecked")
8165
JMapper<T, S> jMapper = (JMapper<T, S>) mappers.get(new MappingKey<>(sourceType, targetType));
8266
if (jMapper == null) {
@@ -97,14 +81,4 @@ private <T, S> T mapWithJMapper(@NonNull final Class<S> sourceType, @NonNull fin
9781
}
9882

9983
}
100-
101-
private boolean IsPrimitveIncludingWrapper(final Class<?> t) {
102-
return t.isPrimitive() || t == String.class || t == Short.class || t == Integer.class || t == Long.class
103-
|| t == Float.class || t == Double.class || t == Character.class || t == Boolean.class;
104-
}
105-
106-
private boolean IsPrimitiveCollectionOrArrayIncludingWrapper(final Class<?> t) {
107-
return t.isArray() && IsPrimitveIncludingWrapper(t.getComponentType()) || Collection.class.isAssignableFrom(t)
108-
|| Map.class.isAssignableFrom(t);
109-
}
11084
}

src/test/java/info/unterrainer/commons/serialization/objectmapper/ObjectMapperMappingTests.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55

66
import java.time.LocalDate;
7-
import java.util.Arrays;
87

98
import org.junit.jupiter.api.BeforeEach;
109
import org.junit.jupiter.api.Test;
1110

1211
import info.unterrainer.commons.serialization.objectmapper.models.ComplexUser;
1312
import info.unterrainer.commons.serialization.objectmapper.models.ObjectWithArray;
14-
import info.unterrainer.commons.serialization.objectmapper.models.ObjectWithObject;
1513
import info.unterrainer.commons.serialization.objectmapper.models.OtherObjectWithArray;
1614
import info.unterrainer.commons.serialization.objectmapper.models.OtherSimpleUser;
1715
import info.unterrainer.commons.serialization.objectmapper.models.SimpleUser;
@@ -37,28 +35,6 @@ public void beforeEach() {
3735
x.setFirstName(y.getFirstName());
3836
x.setLastName(y.getSurName());
3937
});
40-
oMapper.registerMapping(SimpleUser.class, OtherSimpleUser.class, (x, y) -> {
41-
y.setFirstName(x.getFirstName());
42-
y.setSurName(x.getLastName());
43-
}, (y, x) -> {
44-
x.setFirstName(y.getFirstName());
45-
x.setLastName(y.getSurName());
46-
});
47-
oMapper.registerMapping(String[].class, ObjectWithArray.class, (x, y) -> {
48-
y.setObjects(x);
49-
}, (y, x) -> {
50-
x = Arrays.copyOf(y.getObjects(), y.getObjects().length, x.getClass());
51-
});
52-
oMapper.registerMapping(ObjectWithArray.class, OtherObjectWithArray.class, (x, y) -> {
53-
y.setObjects(Arrays.copyOf(x.getObjects(), x.getObjects().length, x.getObjects().getClass()));
54-
}, (y, x) -> {
55-
x.setObjects(Arrays.copyOf(y.getObjects(), y.getObjects().length, y.getObjects().getClass()));
56-
});
57-
oMapper.registerMapping(Integer.class, ObjectWithObject.class, (x, y) -> {
58-
y.setObject(x);
59-
}, (y, x) -> {
60-
x = Integer.parseInt(String.valueOf(y));
61-
});
6238
}
6339

6440
@Test
@@ -100,31 +76,15 @@ public void mappingOtherSimpleUserToComplexUserWorks() {
10076

10177
@Test
10278
public void mappingObjectWithArrayWorks() {
103-
String[] array = new String[] {
104-
"Danijel",
105-
"Balog",
106-
"Hallo"
107-
};
108-
ObjectWithArray oArray = oMapper.map(ObjectWithArray.class, array);
109-
assertArrayEquals(array, oArray.getObjects());
79+
ObjectWithArray sArray = new ObjectWithArray(new String[] { "Danijel", "Balog", "Hallo" });
80+
ObjectWithArray tArray = oMapper.map(ObjectWithArray.class, sArray);
81+
assertArrayEquals(sArray.getObjects(), tArray.getObjects());
11082
}
11183

11284
@Test
11385
public void mappingObjectWithArrayOfObjectsWorks() {
114-
ObjectWithArray array = new ObjectWithArray(new Object[] {
115-
"Danijel",
116-
500,
117-
"Balog",
118-
"Hallo"
119-
});
86+
ObjectWithArray array = new ObjectWithArray(new String[] { "Danijel", "gluppy", "Balog", "Hallo" });
12087
OtherObjectWithArray oArray = oMapper.map(OtherObjectWithArray.class, array);
12188
assertArrayEquals(array.getObjects(), oArray.getObjects());
12289
}
123-
124-
@Test
125-
public void mappingObjectWithinObjectWorks() {
126-
Integer i = 1234;
127-
ObjectWithObject oWO = oMapper.map(ObjectWithObject.class, i);
128-
assertEquals(i, oWO.getObject());
129-
}
13090
}

src/test/java/info/unterrainer/commons/serialization/objectmapper/models/ObjectWithArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
@AllArgsConstructor
99
@NoArgsConstructor
1010
public class ObjectWithArray {
11-
Object[] objects;
11+
String[] objects;
1212
}

0 commit comments

Comments
 (0)